Java connection MySQL database authentication method for user name password The SQL query statement "" "function in the Java statement

Source: Internet
Author: User

//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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.