In JDBC, check and add, delete, modify, and query mysql database connection tools such as Java, and jdbcmysql

Source: Internet
Author: User

In JDBC, check and add, delete, modify, and query mysql database connection tools such as Java, and jdbcmysql

First, write a tool class that implements MySQL database connection, and closes the database connection, closes the ResultSet result set, and disables PreparedStatement. The Code is as follows:

Package com. swift; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; public class DBUtil {// Connection MySQL database tool public static Connection 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 ();} return conn;} // closes the database Connection, SQL Connection, and result set public static void closeAll (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, you can log on to an existing account to check whether the account exists in the database. The Code is as follows:

Package com. swift; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; public class JDBC_Login {public static void main (String [] args) {User user = new User ("swift", "abc123"); if (login (user )) {System. out. println ("successful");} else {System. out. println ("failed") ;}} public static boolean login (User user User) {Connection conn = DBUtil. getConn (); Prepa RedStatement 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 (); rsw.ps.exe cuteQuery (); if (rs. next () {return true ;}} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();} DBUtil. closeAll (conn, ps, rs); return false ;}}

Add data to the database and insert new data to the database table sw_user. The Code is as follows:

Package com. swift; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. SQLException; public class JDBC_Add {public static void main (String [] args) {User user = new User ("duoduo", "abc123"); if (add (user )) {System. out. println ("account registration successful");} else {System. out. println ("registration failed") ;}} private static boolean add (User user User) {Connection conn = DBUtil. getConn (); PreparedStatement ps = null; tr Y {ps = conn. prepareStatement ("insert into sw_user (username, password) values (?,?) "); Ps. setString (1, user. getUsername (); ps. setString (2, user. getPassword (); int count0000ps.exe cuteUpdate (); if (count = 1) {return true ;}} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();} DBUtil. closeAll (conn, ps, null); return false ;}}

To delete data in the database, delete existing data in the database table sw_user. The Code is as follows:

Package com. swift; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. SQLException; public class JDBC_Delete {public static void main (String [] args) {User user = new User ("duoduo", "abc123"); if (JDBC_Login.login (user )) {if (delete (user) {System. out. println ("account deregistered successfully");} else {System. out. println ("logout failed") ;}} else {System. out. println ("account does not exist") ;}} private static boolean delete (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 (); int count0000ps.exe cuteUpdate (); if (count = 1) {return true ;}} catch (SQLException e) {e. printStackTrace ();} DBUtil. closeAll (conn, ps, null); return false ;}}

Modify the data in the database and update the existing data in the database table sw_user. The Code is as follows:

Package com. swift; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. SQLException; public class JDBC_Update {public static void main (String [] args) {User user = new User ("swift", "abc123"); String setPassword = "abc12345 "; if (JDBC_Login.login (user) {if (update (user, setPassword) {System. out. println ("password modified successfully");} else {System. out. println ("failed to modify") ;}} else {System. out. println (" Account does not exist ") ;}} private static boolean update (User 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 (); int count0000ps.exe cuteUpdate (); if (count = 1) {return true ;}} catch (SQLException e) {e. printStackTrace ();} DBUtil. closeAll (conn, ps, null); return false ;}}

The use of tool classes can improve code reusability and improve code readability to facilitate modification.

 

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.