//Method One, you can verify the login, but the method is not practical.
PackageCom.swift;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.ArrayList;Importjava.util.List; Public classLOGINJDBC { Public Static voidMain (string[] args) {User Userzhangsan=NewUser ("Swift", "123456"); if(Login (Userzhangsan)) {System.out.println ("Successful Landing"); }Else{System.out.println ("Login Failed"); } } Private Static BooleanLogin (User userzhangsan) {Connection conn=NULL; Statement St=NULL; ResultSet RS=NULL; Try { //1, Load driveClass.forName ("Com.mysql.jdbc.Driver"); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Try { //2, link database, using Com.mysql.jdbc.Connection package will be errorList<user> list=NewArraylist<user>(); Conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/sw_database?user=root&password=root"); //3. Create a connection statementst=conn.createstatement (); //4. Execute SQL statement to get result setRs=st.executequery ("SELECT * from Sw_user"); //5. Loop to get Database field generation Object//This method log in to the database data are taken and login data comparison, super waste resources while(Rs.next ()) {String username=rs.getstring ("username"); String Password=rs.getstring ("Password"); if(Username.equals (Userzhangsan.getusername ()) &&password.equals (Userzhangsan.getpassword ())) { return true; }Else { return false; } } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally { //Close Result set Try { if(rs!=NULL) {rs.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } //closing a connection statement Try { if(st!=NULL) {st.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } //To close a database connection Try { if(conn!=NULL) {conn.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } return false; }}
Method two uses a database query statement select * from Sw_user where username= ' Swift ' and password= ' 123456 '
Note that the single quotation mark ' is indispensable in a Java program, it is a self-contained part of the SQL statement, and the double quotation marks are Java string connectors, so understand the "'" ' respective roles
PackageCom.swift;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.ArrayList;Importjava.util.List; Public classLoginJDBC2 { Public Static voidMain (string[] args) {User Userzhangsan=NewUser ("Zhangsan", "123456"); if(Login (Userzhangsan)) {System.out.println ("Successful Landing"); }Else{System.out.println ("Login Failed"); } } Private Static BooleanLogin (User userzhangsan) {Connection conn=NULL; Statement St=NULL; ResultSet RS=NULL; Try { //1, Load driveClass.forName ("Com.mysql.jdbc.Driver"); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Try { //2, link database, using Com.mysql.jdbc.Connection package will be errorList<user> list=NewArraylist<user>(); Conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/sw_database?user=root&password=root"); //3. Create a connection statementst=conn.createstatement (); //4. Execute SQL statement to get result setRs=st.executequery ("select * from Sw_user where username= '" +userzhangsan.getusername () + "' and password= '" + Userzhangsan.getpassword () + "'"); //5. Loop to get Database field generation Object//This method log in to the database data are taken and login data comparison, super waste resources if(Rs.next ()) {return true; } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally { //Close Result set Try { if(rs!=NULL) {rs.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } //closing a connection statement Try { if(st!=NULL) {st.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } //To close a database connection Try { if(conn!=NULL) {conn.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } return false; }}
Java connection MySQL database authentication method for user name password The SQL query statement "" "function in the Java statement