Database links and database connection tools
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Public class DBUtils {
Private static final String URL = "jdbc: mysql: // localhost: 3306/jdbc? CharacterEncoding = UTF-8 ";
Private static final String UER_NAME = "root ";
Private static final String PDW = "123123 ";
Private static DBUtils me = new DBUtils ();
Public static DBUtils getInstance (){
Return me;
}
// To make the singleton mode the only way to obtain this object, set the non-argument constructor of this class to private
Private DBUtils (){}
/*
* Database connection
*/
Public Connection getConn (){
Connection conn = null;
Try {
Class. forName ("com. mysql. jdbc. Driver ");
Conn = DriverManager. getConnection (URL, UER_NAME, PDW );
} Catch (ClassNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return conn;
}
/*
* Release resources
*/
Public void releaseRes (Connection conn, PreparedStatement pstmt, ResultSet rs ){
Try {
If (conn! = Null) conn. close ();
If (pstmt! = Null) pstmt. close ();
If (rs! = Null) rs. close ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}