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

新手关于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" %>
    登录成功

    已采纳该答案
    打赏 评论
  • lkj2016 2018-06-21 11:47

    Class.forName("com.mysql.cj.jdbc.Driver");
    我弄的都是com.mysql.jdbc.Driver
    是版本不一样吗
    preparestatement ps
    直接ps.setstring(插入位置,插入数据);
    conn为null

    打赏 评论
  • delphikkkkk 2018-06-21 11:51

    String sql = "insert into users(username , password) values('"+username+"','"+password+"')"; 有问题

    这样改
    String sql = "insert into users(username , password) values("'+username',"+",'password'"+")";

    打赏 评论
  • delphikkkkk 2018-06-21 11:55

    不好意思 说错了 是连接串有问题
    Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=true","root","123456");//创建连接

    打赏 评论
  • delphikkkkk 2018-06-21 12:06

    6、MySQL数据库
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicod
    e=true&characterEncoding=8859_1"
    //myDB为数据库名
    Connection conn= DriverManager.getConnection(url);

    打赏 评论
  • licjd 2018-06-21 12:55

    报错信息提示是数据库连接的问题,无法正常连接
    Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=true","root","123456");//创建连接
    你把 ?后面的设置信息去掉看看 (感觉不是这个问题,试试),
    还有就是账号和密码确定无误吗? 注:直接root-123456进入数据库成功吗?

    打赏 评论
  • delphikkkkk 2018-06-21 14:34

    这样试试 Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","123456");

    打赏 评论
  • delphikkkkk 2018-06-21 14:41

    Class.forName("com.mysql.jdbc.Driver"); 用这个

    打赏 评论

相关推荐 更多相似问题