panchenxiao 2018-06-21 11:35 采纳率: 100%
浏览 801
已采纳

新手关于jsp页面连接数据库的问题

小弟想用jsp文件输入数据到mysql数据库中
这是代码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="com.mysql.jdbc.*,java.sql.Connection, java.sql.DriverManager,java.sql.PreparedStatement,java.sql.ResultSet " %>


<%
Class.forName("com.mysql.cj.jdbc.Driver"); //加载驱动
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=true","root","123456");//创建连接

String username = request.getParameter("user") ;
String password = request.getParameter("password") ;
String sql = "insert into users(username , password) values('"+username+"','"+password+"')";
out.print("
"+password+"
");
PreparedStatement user = connection.preparedStatement(sql);
user.executeUpdate();
%>


这是报错信息
An error occurred at line: [13] in the jsp file: [/zhuce.jsp]
connection cannot be resolved
10: String password = request.getParameter("password") ;
11: String sql = "insert into users(username , password) values('"+username+"','"+password+"')";
12: out.print("
"+password+"
");
13: PreparedStatement user = connection.preparedStatement(sql);
14: user.executeUpdate();
15: %>
16:
  • 写回答

8条回答 默认 最新

  • delphikkkkk 2018-06-21 11:46
    关注

    给你一个例子 照这做 不过现在都用springmvc+mybaits+spring 用框架了 有什么问题联系我

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


    QQ登录页面



    账户:

    密码:

    验证码

    登录
    注册

    //实现注册功能

    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.Connection"%>
    <%@page import="java.sql.DriverManager"%>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    request.setCharacterEncoding("utf-8");
    String name = request.getParameter("name");
    String password = request.getParameter("password");
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql:///mysql_01";
    String usename = "root";
    String usepasword ="root";
    Connection conn = DriverManager.getConnection(url , usename , usepasword);
    String sql ="insert into worker values(null,?, ?)";
    PreparedStatement ps = conn.prepareStatement(sql);
    ps.setString(1,name);
    ps.setString(2,password);
    ps.executeUpdate();
    conn.close();
    response.sendRedirect("list.jsp");
    %>

    //从数据库中获取所有用户数据

    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.Connection"%>
    <%@page import="java.sql.DriverManager"%>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

    数据列表

    ">删除 ">修改

    返回主页面

    //实现对数据的删除功能

    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.Connection"%>
    <%@page import="java.sql.DriverManager"%>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String id =request.getParameter("name");
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql:///mysql_01";
    String usename = "root";
    String usepassword ="root";
    Connection conn = DriverManager.getConnection(url,usename , usepassword);
    String sql = "delete from worker where id="+id;
    PreparedStatement ps = conn.prepareStatement(sql);

    ps.executeUpdate();
    conn.close();
    response.sendRedirect("list.jsp");
    %>

    //数据库实现对要修改的数据的查询

    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.Connection"%>
    <%@page import="java.sql.DriverManager"%>
    <%@page import="java.net.ConnectException"%>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    request.setCharacterEncoding("utf-8");
    String id = request.getParameter("id");
    String password = request.getParameter("password");
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql:///mysql_01";
    String usename ="root";
    String usepassword ="root";
    Connection conn = DriverManager.getConnection(url,usename,usepassword);
    String sql = "update worker set password=? where id=?";
    PreparedStatement ps = conn.prepareStatement(sql);
    ps.setString(1,password);
    ps.setInt(2,Integer.parseInt(id));
    ps.executeUpdate();
    conn.close();
    response.sendRedirect("list.jsp");
    %>

    //找到数据库中对应的要修改的数据,并对其进行修改

    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.Connection"%>
    <%@page import="java.sql.DriverManager"%>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

    <%
    request.setCharacterEncoding("utf-8");
    String id = request.getParameter("name");
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql:///mysql_01";
    String usename ="root";
    String usepassword ="root";
    Connection conn = DriverManager.getConnection(url,usename , usepassword);
    String sql ="select * from worker where id=?";
    PreparedStatement ps = conn.prepareStatement(sql);
    //将字符串的id转换成int型的id
    ps.setInt(1, Integer.parseInt(id));
    ResultSet rs= ps.executeQuery();
    if(rs.next()){
    %>


    ">

    密码:">
    修改

    <%
    }
    %>

    //登录功能实现从前端获取的数据与数据库的数据进行对比

    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.DriverManager"%>
    <%@page import="java.sql.Connection"%>
    <%@page pageEncoding="utf-8" %>
    <%
    String name = request.getParameter("name");
    String password = request.getParameter("password");
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/mysql_01";
    String servername = "root";
    String serverpassword = "root";
    Connection conn = DriverManager.getConnection(url , servername , serverpassword);
    String sql = "select * from worker";
    PreparedStatement ps = conn.prepareStatement(sql);
    ResultSet rs = ps.executeQuery();
    boolean flag = false;
    while(rs.next()){
    if(name!=null&&(rs.getString("name")).equals(name)){
    if(password!=null&&(rs.getString("password")).equals(password)){
    flag = true ;
    break;
    }else{
    flag = false;
    }
    }else{
    flag = false;
    }
    }

    //对flag进行判断
    if(flag){
    

    %>
    <br> window.location=&quot;login.jsp&quot;;<br>
    <%
    }else{
    %>
    <br> window.location=&quot;Client.jsp&quot;;<br>
    <%
    }
    %>

    //跳转登录成功页面

    <%@page pageEncoding="utf-8" %>
    登录成功

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化