JDBC Operation steps
/ * * JDBC Operation steps * */ Public classTest01 { Public Static void Main(string[] args) {Try{//1. Loading and registering driversClass.forName ("Com.mysql.jdbc.Driver");//drivermanager.registerdriver (New Com.mysql.jdbc.Driver ());System. out. println ("Registration driver success!" ");//2. Getting a database connection connectionString url="Jdbc:mysql://localhost:3306/test"; String user="Root"; String password="123456"; Connection conn=drivermanager.getconnection (URL, user, password); System. out. println ("Connection to the database was successful!" ");//3. Creating statement and executing SQL statementsStatement stmt=conn.createstatement (); String sql="INSERT into user values (null, ' Jiang lose ', ' 111111 ', ' 2012-1-11 ')";intNum=stmt.executeupdate (SQL);//4. Processing execution Results if(num>0) {System. out. println ("Insert success!" "); }Else{System. out. println ("Insert failed!" "); }//5. Closing ResourcesStmt.close (); Conn.close (); }Catch(ClassNotFoundException e) {System. out. println ("The driver didn't find it!" "); E.printstacktrace (); }Catch(SQLException e) {System. out. println ("Database connection failed!" "); E.printstacktrace (); } }}
JDBC Implements query operations
ImportJava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;ImportJava.sql.SQLException;ImportJava.sql.Statement;ImportJava.util.Date;/ * * JDBC Implements query operation */ Public class Test02 {Connection conn =NULL; Statement stmt =NULL; ResultSet rs =NULL;//query user information Public void queryusers() {Try{//1. Loading and registering driversClass.forName ("Com.mysql.jdbc.Driver");//2. Getting a database connectionconn = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test","Root","123456");//3. Creating statement objects and executing SQL statementsstmt = Conn.createstatement (); String sql ="SELECT * from User";//Call the ExecuteQuery () method to perform a query operation that returns a resultset result set that contains the results of the queryrs = stmt.executequery (SQL);//4. Manipulating ResultSet result sets while(Rs.next ()) {//Move the pointer forward one line to determine if there is a record intid = rs.getint ("id");//Get value based on column nameString name = Rs.getstring ("Name"); String Password = rs.getstring (3);//Get values based on the index of the columnJava.sql.Date Birthday=rs.getdate (5); System.out.println ("Number:"+ ID +", Name:"+ name +", Password:"+ password+"Birthday:"+birthday); } }Catch(ClassNotFoundException e) {E.printstacktrace (); }Catch(SQLException e) {E.printstacktrace (); }finally{//5. Closing Resources Try{Rs.close (); Stmt.close (); Conn.close (); }Catch(SQLException e) {E.printstacktrace (); } } }//query number of users Public void Queryusercount() {Try{//1. Loading and registering driversClass.forName ("Com.mysql.jdbc.Driver");//2. Getting a database connectionconn = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test","Root","123456");//3. Creating statement objects and executing SQL statementsstmt = Conn.createstatement (); String sql ="SELECT count (ID) from user";//Call the ExecuteQuery () method to perform a query operation that returns a resultset result set that contains the results of the queryrs = stmt.executequery (SQL);//4. Manipulating ResultSet result sets intCount=0;if(Rs.next ()) {//Move the pointer forward one line to determine if there is a recordCount=rs.getint (1); } System.out.println ("Total"+count+"a user!" "); }Catch(ClassNotFoundException e) {E.printstacktrace (); }Catch(SQLException e) {E.printstacktrace (); }finally{//5. Closing Resources Try{Rs.close (); Stmt.close (); Conn.close (); }Catch(SQLException e) {E.printstacktrace (); } } } Public Static void Main(string[] args) {NewTest02 (). Queryusercount (); }}
To save database connection information in a file
import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties;/ * * Save database connection information in file */ Public class Test03 { Public Static voidMain (string[] args) throws Exception {Properties p=NewProperties (); P.load (NewFileInputStream ("Jdbc.properties")); Class.forName ("Com.mysql.jdbc.Driver"); Connection Conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", p); System.out.println ("Connection to the database was successful!" "); }}
Java Learning Note (50)-JDBC Java database connection