PackageDao;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importentity. UserInfo; Public classJDBC {Connection conn; PreparedStatement PS; ResultSet rs; /*** Write a method to connect to the database*/ PublicConnection getconnection () {String URL= "Jdbc:mysql://localhost: Port/Database"; String UserName= "Root"; String Password= "Password"; Try{class.forname ("Com.mysql.jdbc.Driver"); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockSYSTEM.OUT.PRINTLN ("Driver not found!") "); E.printstacktrace (); } Try{conn=drivermanager.getconnection (URL, userName, password); if(conn!=NULL) {System.out.println ("Connection Successful"); } } Catch(SQLException e) {//TODO auto-generated Catch blockSYSTEM.OUT.PRINTLN ("Connection fail"); E.printstacktrace (); } returnConn; } /*** Write a method to query database statements*/ Public voidQuerysql () {//1. Execute static SQL statements. Typically implemented through statement instances. //2. Execute dynamic SQL statements. Typically implemented through PreparedStatement instances. //3. Execute the database stored procedure. Typically implemented through CallableStatement instances. SYSTEM.OUT.PRINTLN ("Query"); UserInfo u;//j.connection ();String sql= "SELECT * from UserInfo"; Try{conn=getconnection ();//connecting to a databasePs=conn.preparestatement (SQL);//2. Create satement and set parametersRs=ps.executequery (SQL);//3. Execute SQL statements//4. Working with result sets while(Rs.next ()) {u=NewUserInfo (); U.setuserid (Rs.getint ("UserId")); U.setusername (Rs.getstring ("UserName")); U.setpassword (Rs.getstring ("Password")); U.setremark (Rs.getstring ("Remark")); System.out.println ("Uesrname" +u.getusername ()); } System.out.println (Rs.next ()); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{ //Freeing Resources Try{rs.close (); Ps.close (); Conn.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }}} @SuppressWarnings ("NULL") Public intAdd (UserInfo user) {UserInfo u=NewUserInfo (); intRow=0;//j.connection ();String sql= "INSERT into UserInfo (Username,password) VALUES (?,?)"; Try{conn=getconnection ();//connecting to a databasePs=conn.preparestatement (SQL);//2. Create satement and set parameters//Rs=ps.executequery (); //3. Execute SQL statement, tense to find language//There are several fields in the SQL sentence, and the following must have a few fieldsPs.setstring (1, User.getusername ()); Ps.setstring (2, User.getpassword ()); //4. Working with result setsrow=ps.executeupdate (); System.out.println (Row+user.getusername () +User.getpassword ()); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{ Try{ps.close (); Conn.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } returnRow; } /*** @return Modify Data*/ Public intUpdate (UserInfo user) {UserInfo u; intRow=0;//j.connection ();String sql= "Update userInfo set username=?,password=? where userid=? "; Try{conn=getconnection ();//connecting to a databasePs=conn.preparestatement (SQL);//2. Create satement and set parameters//rs=ps.executequery (SQL); //3. Execute SQL statement, tense to find language//There are several fields in the SQL sentence, and the following must have a few fieldsPs.setstring (1, User.getusername ()); Ps.setstring (2, User.getpassword ()); Ps.setint (3, User.getuserid ()); //4. Working with result setsrow=ps.executeupdate (); System.out.println (row); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{ //Freeing Resources Try {//rs.close ();Ps.close (); Conn.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } returnRow; } /*** @return Delete operation*/ Public intDelete (UserInfo user) {UserInfo u; intRow=0;//j.connection ();String sql= "Delete from UserInfo where userid=?"; Try{conn=getconnection ();//connecting to a databasePs=conn.preparestatement (SQL);//2. Create satement and set parameters//rs=ps.executequery (SQL); //3. Execute SQL statement, tense to find language//There are several fields in the SQL sentence, and the following must have a few fieldsPs.setint (1, User.getuserid ()); //4. Working with result setsrow=ps.executeupdate (); System.out.println (row); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{ //Releasing Resources "Remember to release the SQL when you're done." Try {//rs.close ();Ps.close (); Conn.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } returnRow; } Public Static voidMain (string[] args) {JDBC J=NewJDBC ();//j.getconnection ();J.querysql ();//display a Lookup method in the console//UserInfo u=new UserInfo ();//U.setuserid (5);//u.setusername ("cool");//U.setpassword ("123abc");//j.update (u);////Display the modification method in the console }}
From:http://blog.csdn.net/qianquan003/article/details/23364381?utm_source=tuicool&utm_medium=referral
Java connection to local MySQL database for additional pruning and checking operations