Use the properties file to configure the database driver, URI, user name and password, so as to make it easier to change the database later without changing the source code;
driver=Com.mysql.jdbc.Driveruri=jdbc:mysql://localhost:3306/day10user= Rootpassword=root
1. Registration Driver
Public Static throws FileNotFoundException, IOException, ClassNotFoundException, sqlexception{2 3 Class.forName (Prop.getproperty ("Driver")); 4 return Drivermanager.getconnection (Prop.getproperty ("uri"), Prop.getproperty ("user"), Prop.getproperty ("Password") ); 5 6 7 }
2. Create a connector
Connection conn = jdbcutils.getconn (); // Connection conn = Drivermananger ("Uri,user,password");
3. Get the transmitter
statment stat = conn.createstatement ();
5. Query the statement with the transmitter and return the result set
Stat.executeupdate (SQL); Delete and change
Stat.executequery (SQL); Query statement
6. Close Resources
1 Public Static voidClose (Connection conn,statement Stat,resultset rs) {2 3 if(rs!=NULL){4 Try {5 rs.close ();6}Catch(SQLException e) {7 Throw NewRuntimeException (e);8 9}finally{Tenrs=NULL; One } A } - if(stat!=NULL){ - Try { the stat.close (); -}Catch(SQLException e) { - Throw NewRuntimeException (e); - +}finally{ -stat=NULL; + } A } at if(conn!=NULL){ - Try { - conn.close (); -}Catch(SQLException e) { - Throw NewRuntimeException (e); - in}finally{ -conn=NULL; to } + } -}
Using MySQL (6-part song)