Connection tools for MySQL database in JDBC Java login and additions and deletions to check and organize

Source: Internet
Author: User

Write a tool class first, there is a way to implement MySQL database connection, and close the database connection, close the resultset result set, close the PreparedStatement method. The code is as follows:

 PackageCom.swift;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException; Public classDbutil {//connect MySQL Database Tools         Public StaticConnection Getconn () {Connection conn=NULL; Try{class.forname ("Com.mysql.jdbc.Driver"); Try{conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/sw_database?user=root&password=root"); } Catch(SQLException e) {e.printstacktrace (); }            } Catch(ClassNotFoundException e) {e.printstacktrace (); }            returnConn; }        //Close database connections, SQL connections, result sets         Public Static voidCloseAll (Connection conn,preparedstatement ps,resultset rs) {if(conn!=NULL)                Try{conn.close (); } Catch(SQLException e) {e.printstacktrace (); }            if(ps!=NULL) {                Try{ps.close (); } Catch(SQLException e) {e.printstacktrace (); }            }            if(rs!=NULL) {                Try{rs.close (); } Catch(SQLException e) {e.printstacktrace (); }            }        }}

Next implement login, existing account in the database to verify the existence or not. The code is as follows:

 PackageCom.swift;Importjava.sql.Connection;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException; Public classJdbc_login { Public Static voidMain (string[] args) {User User=NewUser ("Swift", "abc123"); if(Login (user)) {System.out.println (Success); }Else{System.out.println (Failed); }            }     Public Static BooleanLogin (user user) {Connection conn=Dbutil.getconn (); PreparedStatement PS=NULL; ResultSet RS=NULL; Try{PS=conn.preparestatement ("select * from Sw_user where username=?") and password=? "); Ps.setstring (1, User.getusername ()); Ps.setstring (2, User.getpassword ()); RS=Ps.executequery (); if(Rs.next ()) {return true; }        } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace ();        } dbutil.closeall (conn, PS, RS); return false; }}

The implementation adds data to the database, inserts new data into the database table Sw_user, and the code is as follows:

 PackageCom.swift;Importjava.sql.Connection;Importjava.sql.PreparedStatement;Importjava.sql.SQLException; Public classJdbc_add { Public Static voidMain (string[] args) {User User=NewUser ("Duoduo", "abc123"); if(Add (user)) {System.out.println ("Account Registration is successful"); }Else{System.out.println ("Registration Failed"); }    }    Private Static BooleanAdd (user user) {Connection conn=Dbutil.getconn (); PreparedStatement PS=NULL; Try{PS=conn.preparestatement ("INSERT into Sw_user (Username,password) VALUES (?,?)"); Ps.setstring (1, User.getusername ()); Ps.setstring (2, User.getpassword ()); intCount=ps.executeupdate (); if(count==1) {                return true; }        } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } dbutil.closeall (conn, PS,NULL); return false; }}

Implement to delete the data in the database, the database table Sw_user Delete the existing data, the code is as follows:

 PackageCom.swift;Importjava.sql.Connection;Importjava.sql.PreparedStatement;Importjava.sql.SQLException; Public classJdbc_delete { Public Static voidMain (string[] args) {User User=NewUser ("Duoduo", "abc123"); if(jdbc_login.login (user)) {if(Delete (user)) {System.out.println ("Account Logout Successful"); }Else{System.out.println ("Logoff Failed"); }        }Else{System.out.println ("Account does not exist"); }    }    Private Static BooleanDelete (user user) {Connection conn=Dbutil.getconn (); PreparedStatement PS=NULL; Try{PS=conn.preparestatement ("Delete from Sw_user where username=?") and password=? "); Ps.setstring (1, User.getusername ()); Ps.setstring (2, User.getpassword ()); intCount=ps.executeupdate (); if(count==1) {                return true; }        } Catch(SQLException e) {e.printstacktrace (); } dbutil.closeall (conn, PS,NULL); return false; }}

To implement modifying data in the database, update the existing data in the database table Sw_user, with the following code:

 PackageCom.swift;Importjava.sql.Connection;Importjava.sql.PreparedStatement;Importjava.sql.SQLException; Public classJdbc_update { Public Static voidMain (string[] args) {User User=NewUser ("Swift", "abc123"); String SetPassword= "abc12345"; if(jdbc_login.login (user)) {if(Update (User,setpassword)) {System.out.println ("Password modified successfully"); }Else{System.out.println ("Modify Failed"); }        }Else{System.out.println ("Account does not exist"); }                }    Private Static BooleanUpdate (User user,string setpassword) {Connection conn=Dbutil.getconn (); PreparedStatement PS=NULL; Try{PS=conn.preparestatement ("Update sw_user set password=?") where Username=? "); Ps.setstring (1, SetPassword); Ps.setstring (2, User.getusername ()); intCount=ps.executeupdate (); if(count==1) {                return true; }        } Catch(SQLException e) {e.printstacktrace (); } dbutil.closeall (conn, PS,NULL); return false; }}

The use of the tool class can improve the reusability of code and improve the readability of code to make it easier to modify.

Connection tool for MySQL database in JDBC Java login and additions and deletions check and organize

Related Article

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.