JDBC Tool class encapsulates database connections

Source: Internet
Author: User

First, create a file db.properties that holds the database connection information.

Writing code to connect to a database

Package COM.JDBC;

Import java.io.IOException;
Import Java.io.InputStream;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.Properties;

Public class Dbutils {

/**
* User name to connect to the database
*/
static String user = null;
/**
* Password to connect to the database
*/
static String password = null;
/**
* Address of the connection database
*/
static String url = null;
/**
* Drive to connect to the database
*/
static String driver = null;

static{
//Get input stream
InputStream in = DBUtils.class.getClassLoader (). getResourceAsStream ("Db.properties");
//Get Properties Object
Properties P = new properties ();

try {
//Read the configuration file for database connection information
p.load (in);
user = P.get ("user"). toString ();
Password = p.get ("password"). toString ();
url = p.get ("url"). toString ();
Driver = p.get ("Driver"). toString ();

//Registration driver
Class.forName (driver);
} catch (IOException e) {
e.printstacktrace ();
System.out.println ("Failed to read config file!!! ");
} catch (ClassNotFoundException e) {
e.printstacktrace ();
System.out.println ("Registration driver failed!!! ");
}
}


/**
* Connect to database
* @returnjava. Sql.connection
*/
Public static Connection getconnection () {
Connection conn = null;
try {
//Connect to database
conn = drivermanager.getconnection (URL, user, password);
} catch (SQLException e) {
e.printstacktrace ();
System.out.println ("Database connection Failed!!! ");
}
return conn;
}


/**
* Close Resources
* @param connjava.sql.Connection
* @param stasjava.sql.Statement
* @param rsjava.sql.ResultSet
*/
Public static void Close (Connection conn,statement Stas,resultset rs) {
try {
if (rs! = null) rs.close ();
if (Stas! = null) stas.close ();
if (conn! = null) conn.close ();
} catch (SQLException e) {
e.printstacktrace ();
}
}

}

JDBC Tool class encapsulates database connections

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.