Package com. yanek. test;
Import java. SQL .*;
Import javax. naming .*;
Import javax. SQL .*;
/**
* Database tools
* 1. query DataSource to obtain the database connection.
* 2. Release the connection resource.
*/
Public class DBHandle {
/**
* Getting database connections
* 1. initialize the Context
* 2. Search for DataSource In the Context. The name is specified in advance. Here: "java: comp/env/jdbc/bbs"
* 3. If DataSource is not empty, a connection is retrieved from DataSource.
*/
Public synchronized static Connection getConn () throws Exception {
Connection conn = null;
Return conn;
}
/**
* Release connection-related resources in order
* 1. Release the result set first.
* 2. Release the statement handle.
* 3. Finally, release the connection.
*/
Public static void closeresource (statement St, resultset RS, connection CNN ){
Try {
If (RS! = NULL ){
Rs. Close ();
}
}
Catch (exception ES ){
Es. printStackTrace ();
}
Try {
If (st! = Null ){
St. close ();
}
}
Catch (Exception es ){
Es. printStackTrace ();
}
Try {
If (CNN! = NULL ){
CNN. Close ();
CNN = NULL;
}
}
Catch (exception ES ){
Es. printstacktrace ();
}
}
/**
* Use drivermanager to obtain database connections
*/
Public static Connection getConnection () throws Exception {
String driver = "com. mysql. jdbc. Driver ";
String urlString = "jdbc: mysql: // localhost/test ";
String user = "root ";
String pwd = "root ";
Class. forName (driver );
Connection cnn = DriverManager. getConnection (urlString, user, pwd );
Return cnn;
}
Public static void main (String [] args ){
Try {
System. Out. println ("Conn:" + dbhandle. getconnection ());
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
Package com. Yanek. test;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. statement;
/**
* Database tool Class 1: Search for datasource to obtain database connection 2. Release connection resources
*/
Public class dbsource {
Public static connection getconnection (final string poolname)
Throws sqlexception {
Try {
Return DriverManager. getConnection (poolName );
} Catch (Exception e ){
Return null;
}
}
/**
* Release connection-related resources. Please note the Order 1. Release result set 2 first, then statement handle 3, and finally release the connection.
*/
Public static void closeResource (Statement st, ResultSet rs, Connection cnn ){
Try {
If (rs! = Null ){
Rs. close ();
}
} Catch (Exception es ){
Es. printStackTrace ();
}
Try {
If (st! = Null ){
St. close ();
}
} Catch (Exception es ){
Es. printStackTrace ();
}
Try {
If (cnn! = Null ){
Cnn. close ();
Cnn = null;
}
} Catch (Exception es ){
Es. printStackTrace ();
}
}
/**
* Use DriverManager to obtain database connections
*/
Public static Connection getConnection () throws Exception {
String driver = "com. mysql. jdbc. Driver ";
String urlString = "jdbc: mysql: // localhost/test ";
String user = "root ";
String pwd = "root ";
Class. forName (driver );
Connection cnn = DriverManager. getConnection (urlString, user, pwd );
Return cnn;
}
Public static void main (String [] args ){
Try {
System. out. println ("conn:" + DBSource. getConnection ());
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}