1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 ImportJava.sql.ResultSet;4 Importjava.sql.SQLException;5 Importcom.mysql.jdbc.PreparedStatement;6 7 Public classDBTool {8 9 Public Static FinalString DRIVER = "Com.mysql.jdbc.Driver" ; Ten Public Static FinalString URL = "jdbc:mysql://localhost:3306/database name"; One Public Static FinalString USERNAME = "User name" ; A Public Static FinalString PSW = "Password" ; - - Public StaticConnection Getconn () { theConnection conn =NULL; - Try { - Class.forName (DRIVER); -conn =drivermanager.getconnection (URL,USERNAME,PSW); +}Catch(ClassNotFoundException e) { - e.printstacktrace (); +}Catch(SQLException e) { A e.printstacktrace (); at } - returnConn; - } - //Close Resource - Public Static voidCloseAll (ResultSet rs, PreparedStatement ps,connection conn) { - if(rs!=NULL){ in Try { - rs.close (); to}Catch(SQLException e) { + e.printstacktrace (); - } the } * if(ps!=NULL){ $ Try {Panax Notoginseng rs.close (); -}Catch(SQLException e) { the e.printstacktrace (); + } A } the if(conn!=NULL){ + Try { - conn.close (); $}Catch(SQLException e) { $ e.printstacktrace (); - } - } the } - Wuyi}
Java JDBC Connection MySQL Database