JSP之连接数据库


1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="Check.jsp" method="post"> 11 姓名:<input type="text" name="uname"></br> 12 密码:<input type="password" name="upwd"></br> 13 <input type="submit" value="登录"> 14 </form> 15 </body> 16 </html>
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@page import="dn.studyjavaweb.LoginDemo" %>
 4 <!DOCTYPE html>
 5 <html>
 6 <head>
 7 <meta charset="UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11     <%
12                 //接收login.jsp传来的数据
13         request.setCharacterEncoding("UTF-8");
14         String uname=request.getParameter("uname");
15         String upwd=request.getParameter("upwd");
16 
17                 //创建LoginDemo对象
18         LoginDemo log=new LoginDemo();
19                 
20                 //调用loginCheck函数
21         int flag=log.loginCheck(uname, upwd);
22                 
23                 //判断登录状态
24         if(flag==0){
25             out.print("用户名或密码错误!");
26         }else if(flag==1){
27             out.print("登陆成功!");
28         }else{
29             out.print("系统错误!");
30         }
31     %>
32 </body>
33 </html>        
 1 package dn.studyjavaweb;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 
 9 public class LoginDemo {
10     public int loginCheck(String uname,String upwd) {
11         final String URL="jdbc:mysql://localhost:3306/javawebpro?useSSL=false&serverTimezone=UTC";
12         final String NAME="root";
13         final String PWD="123456";
14         Connection conn=null;
15         PreparedStatement pstmt=null;
16         ResultSet count=null;
17         try {
18             //加载驱动
19             Class.forName("com.mysql.cj.jdbc.Driver");
20             //建立连接
21             conn=DriverManager.getConnection(URL, NAME, PWD);
22             //创建preparedStatement需要把SQL语句写在前面
23             String sql="select * from user where username=? and userpassword=?";
24             //创建preparedStatement,将SQL预编译
25             pstmt=conn.prepareStatement(sql);
26             //替换占位符
27             pstmt.setString(1, uname);
28             pstmt.setString(2, upwd);
29             //执行SQL语句
30             count=pstmt.executeQuery();
31             //结果判断
32             if(count.next()==true) {
33                 return 1;
34             }else {
35                 return 0;
36             }
37         } catch (ClassNotFoundException e) {
38             e.printStackTrace();
39             return -1;
40         }catch (SQLException e) {
41             e.printStackTrace();
42             return -1;
43         }catch (Exception e) {
44             e.printStackTrace();
45             return -1;
46         }finally {
47                 try {
48                     //关闭结果、连接等,先开后关
49                     if(count!=null)count.close();
50                     if(pstmt!=null)pstmt.close();
51                     if(conn!=null)conn.close();
52                 } catch (SQLException e) {
53                     e.printStackTrace();
54                 }catch (Exception e) {
55                     e.printStackTrace();
56                 }
57         }
58     }
59 
60 }

上述是我用Eclipse代码敲的代码

下面是我遇到的问题:

1.Class.forName("com.mysql.cj.jdbc.Driver");报错

错误1:将"com.mysql.cj.jdbc.Driver"写成"com.mysql.jdbc.Driver"。我的MySQL是8.0版本的,驱动由"com.mysql.jdbc.Driver"变成了"com.mysql.cj.jdbc.Driver"。

错误2:将驱动加载错误,在Eclipse中应该将驱动粘贴到WEB-INF中的lib下

2.conn=DriverManager.getConnection(URL, NAME, PWD);报错

错误1:URL写错

错误2:NAME和PWD与uname和upwd弄混淆

3.count=pstmt.executeQuery(); 出现No value specified for parameter 1

因为有占位符,所以先set它们的值在执行SQL语句

猜你喜欢

转载自www.cnblogs.com/foodiedragon/p/11311691.html