Database Connection Class 2

Source: Internet
Author: User

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 ();
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.