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条)

报告相同问题?

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器