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 new LoginDemo (); . 19 20 is // call the function loginCheck 21 is int In Flag = log .loginCheck (the uname, upwd); 22 is 23 is // Analyzing logged 24 IF (In Flag == 0 ) { 25 Out.print ( " User name or password is wrong! " ); 26 } the else IF (Flag == 1 ) { 27 out.print ( " successful landing! " ); 28 } the else { 29 Out.print ( " System Error! " ); 30 } 31 is %> 32 </ body > 33 is </ 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 is Final String the PWD = "123456" ; 14 Connection Conn = null ; 15 the PreparedStatement pstmt = null ; 16 the ResultSet COUNT = null ; . 17 the try { 18 is // load the driver . 19 the Class.forName ( "com.mysql.cj.jdbc.Driver " ); 20 // establish a connection 21 conn = DriverManager.getConnection (the URL of, NAME, PWD); 22 // create the SQL statement preparedStatement need EDITORIAL 23 SQL = String "? The SELECT * from the User and the userPassword the WHERE username = =?" ; 24- // create preparedStatement, the SQL pre-compiler 25 pstmt = conn.prepareStatement (SQL); 26 // replace the placeholder 27 pstmt.setString ( . 1 , the uname); 28 pstmt.setString (2 , upwd); 29 // execute SQL statements 30 COUNT = pstmt.executeQuery (); 31 is // result determination 32 IF (count.next () == to true ) { 33 is 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 the try { 48 // close the results, etc. are connected, the first switch 49 IF (COUNT =! Null count.close ()); 50 IF (! Pstmt = null ) pstmt.close (); 51 is IF (Conn! = null ) conn.Close (); 52 is } the catch (SQLException E) { 53 is e.printStackTrace (); 54 is } the catch (Exception E) { 55 e.printStackTrace (); 56 is } 57 is } 58 } 59 60 }
The above is my knock with Eclipse Code Code
Here is my problem:
1.Class.forName("com.mysql.cj.jdbc.Driver");报错
Error 1: "com.mysql.cj.jdbc.Driver" written "com.mysql.jdbc.Driver". My version of MySQL 8.0 is driven by "com.mysql.jdbc.Driver" became "com.mysql.cj.jdbc.Driver".
Error 2: load driving error, it should be driven in Eclipse pasted to the WEB-INF in lib
2.conn=DriverManager.getConnection(URL, NAME, PWD);报错
Error 1: URL wrong
Error 2: NAME and PWD confuse with uname and upwd
3.count=pstmt.executeQuery(); 出现No value specified for parameter 1
Because there are placeholders, so their values in the first set to execute SQL statements