Use JDOM to write a more flexible JDBC tool class

Source: Internet
Author: User
Knowledge point: the singleton design mode, which uses JDOM to parse XML files

In JDBC programming, some common operations, such as getting database connections, closing database connections, and curd operations, are usually encapsulated in a tool class to facilitate code utilization.

We always want to write the code only once to complete it. Or when the application environment changes

We can still use our code. The following uses three classes to implement a more flexible JDBC tool class.

It can use the xml configuration file to configure common information for database connection. When the database version or type changes, you only need to modify the xml configuration file to reuse the original code to the maximum extent.

1. jdbcinfo class: this class is actually an entity bean that encapsulates the information required for database connection, such as the driver class and Database Password.

2. jdbcconfigure class: This class uses JDOM to parse the xml configuration file and write the parsing result to the jdbcinfo instance. It is implemented in the singleton design mode.

3. jdbcutils class: specific implementation of the JDBC tool class

// Jdbcinfo. Java

// The jdbcinfo code is very simple. There are only some set and get methods.
Package CN. jvfans. JDBC. utils;
Public
Class jdbcinfo
{
Private string driverclassname = "";
Private
String user = "";
Private string Password = "";
Private string
Url = "";

Public String getdriverclassname (){
Return
Driverclassname;
}
Public void setdriverclassname (string
Driverclassname ){
This. driverclassname = driverclassname;
}

Public String getuser (){
Return user;
}
Public void
Setuser (string user ){
This. User = user;
}
Public String
GetPassword (){
Return password;
}
Public void setpassword (string
Password ){
This. Password = password;
}
Public String geturl (){

Return URL;
}
Public void seturl (string URL ){
This. url =
URL;
}

Public String tostring () {// rewrite the tostring method. Rewriting tostring is a good programming habit.

Return "jdbcinfo (driverclassname =" + this. driverclassname

+ ", User =" + this. User
+ ", Password =" + this. Password

+ ", Url =" + this. url + ")";
}
}
// Jdbcconfigure. Java

Package CN. jvfans. JDBC. utils;
// When writing Java code, it is best not to use import
*. *, But to give a specific class, it is very easy to use ide
Import java. Io. ioexception;
Import
Org. JDOM. Document; // familiar with common JDOM classes
Import
Org. JDOM. element;
Import org. JDOM. jdomexception;
Import
Org. JDOM. Input. saxbuilder;
// Read the JDBC configuration file and implement it in the single-sample design mode

Public class jdbcconfigure
{
Private Static jdbcconfigure
Instance = NULL; // template code of Singleton Mode
// Xml configuration file name, defined as a constant

Private Final Static string
Config_file_name = "jdbc-configure.xml ";
Private element rootelt = NULL;

Private jdbcinfo = new jdbcinfo ();

// The result of this method can be returned to the jdbcutils class for data transmission.

Public jdbcinfo getjdbcinfo (){
Return jdbcinfo;

}
Private jdbcconfigure () // The Singleton mode must have a private constructor.
{

Saxbuilder builder = new saxbuilder ();
Try {
Document
Doc = builder. Build (thread. currentthread (). getcontextclassloader (). getresourceasstream (config_file_name ));

This. rootelt = Doc. getrootelement ();
Initjdbcinfo ();

} Catch (jdomexception e ){
// Todo auto-generated Catch Block

E. printstacktrace ();
} Catch (ioexception e ){
// Todo
Auto-generated Catch Block
E. printstacktrace ();
}
}


Private void initjdbcinfo () // core method for reading XML files. The read results have been written to the jdbcinfo instance.

{
Element
Driverclasselt = rootelt. getchild ("Driver-class ");

Jdbcinfo. setdriverclassname (driverclasselt. gettext ());

Element
Userelt = rootelt. getchild ("user ");
Jdbcinfo. setuser (userelt. gettext ());


Element passwordelt = rootelt. getchild ("password ");

Jdbcinfo. setpassword (passwordelt. gettext ());

Element
Urlelt = rootelt. getchild ("url ");
Jdbcinfo. seturl (urlelt. gettext ());

}

Public static jdbcconfigure getinstance () // core method of Singleton Design Mode
{

If (instance = NULL)
{
Instance = new jdbcconfigure ();

}
Return instance;
}
}

// Jdbcutils. Java

Package CN. jvfans. JDBC. utils;
Import java. SQL .*;
// JDBC tool class, which is not generated by one instance. Of course, it can also be implemented in the singleton mode.

Public class jdbcutils {
Private Static jdbcinfo =
Jdbcconfigure. getinstance ()
. Getjdbcinfo (); // Java code: Do everything you can.
Static
{// The static code block is executed only once when the class is loaded.

Try {
Class. forname (jdbcinfo. getdriverclassname (); // you only need to register it once, so place it in the static code block.

} Catch (classnotfoundexception e ){
Throw
New exceptionininitializererror (E); // throw exceptionininitializererror
It indicates that an exception occurred during calculation of the initial static value or the initial value of the static variable.
}
}
Private jdbcutils ()
{// Private constructor, which cannot be generated by the outside world

}
Public static connection
Getconnection () throws sqlexception {// all methods are static and can be called by class name.

Return drivermanager. getconnection (jdbcinfo. geturl (), jdbcinfo

. Getuser (), jdbcinfo. GetPassword ());
}
Public static void
Free (resultset RS, statement stmt, connection conn) {// close the connection and release resources
If (RS
! = NULL ){
Try {
Rs. Close ();
} Catch (sqlexception e ){

// Todo auto-generated Catch Block
E. printstacktrace ();
}
Finally {
Rs = NULL;
}
}
If (stmt! = NULL ){

Try {
Stmt. Close ();
} Catch (sqlexception e ){
// Todo
Auto-generated Catch Block
E. printstacktrace ();
} Finally {

Stmt = NULL;
}
}
If (Conn! = NULL ){
Try {

Conn. Close ();
} Catch (sqlexception e ){
// Todo
Auto-generated Catch Block
E. printstacktrace ();
} Finally {

Conn = NULL;
}
}
}
// Other methods are omitted. They can be curd operations or some other practical methods.

}

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.