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 帮我做下照片上的PLC题
  • ¥15 labview2022 使用modbus报缺少依赖?
  • ¥15 谷歌地图是不是不开通结算功能,api会使用不了哦
  • ¥15 unity腾讯云对象存储机型适配
  • ¥15 求全国交通咨询模拟代码,要求如下,可以完全在dev c++运行
  • ¥15 根据要求修改程序编码
  • ¥15 用 Python 做一个用 Excel 表导入的答题系统
  • ¥15 使用微信开发者工具实现一个“婚博会”小程序
  • ¥15 ros的rviz仿真机器人
  • ¥15 关于#linux#的问题(输入输出错误):出现这个界面接着我重新装系统,又让修电脑的师傅帮我扫描硬盘(没有问题)用着用着又卡死(相关搜索:固态硬盘)