Encapsulates a Java database access management class

Source: Internet
Author: User

CopyCode The Code is as follows: Package com. Groundhog. codingmouse;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
/**
* Database Management
* @ Author codingmouse
* 2009.2.20
*/
Public final class dbmanager {
/**
* Database connection object
*/
Private connection dbconnection = NULL;
/**
* DATABASE Command Execution object
*/
Private preparedstatement prestatement = NULL;
/**
* Result set object
*/
Private resultset rsset = NULL;
/**
* Database driver version
*/
Private Static string driverversion = NULL;
/**
* Database Server login username and password string constant (default value: Both
Is 'sa ')
*/
Private Static string databaseuser = "sa ";
Private Static string databasepassword = "sa ";
/**
* Database-driven complete class name String constant
*/
Private Static final string
Driver_class_sqlserver2000 =
"Com. Microsoft. JDBC. sqlserver. sqlserverdriver"; // SQL
Server 2000 direct connection
Private Static final string
Driver_class_sqlserver2005 =
"Com. Microsoft. sqlserver. JDBC. sqlserverdriver"; // SQL
Server 2005 Direct Connection
Private Static final string
Driver_class_bridgeconnect = "Sun. JDBC. ODBC. jdbcodbcdriver ";
// ODBC bridge connection
/**
* Database connection string constant
*/
Private Static final string
Database_url_sqlserver2000 =
"JDBC: Microsoft: sqlserver: // 127.0.0.1: 1433; databasename = stud
B "; // SQL Server 2000 direct connection
Private Static final string
Database_url_sqlserver2005 =
"JDBC: sqlserver: // 127.0.0.1: 1433; databasename = studb ";
// SQL Server 2005 Direct Connection
Private Static final string
Database_url_bridgeconnect = "JDBC: ODBC: studbsource ";
// ODBC bridge connection
/**
* Defines the static instance variables of the class (applies to applications in the single-instance [component] mode)
*/
Private Static dbmanager connectionmanager = NULL;
/**
* Default structure of privatization (applies to applications in the single-instance [component] mode to prevent direct classes
Use the New Keyword for instantiation)
*/
Private dbmanager (){
Super ();
}
/**
* Methods for obtaining database connection management instances (single-instance [component] Mode Applications)
* @ Param version: Database driver version. Value: (version =
2000 | version = 2005 | version = ODBC)
* @ Param User: username used to log on to the Database Server
* @ Param password: Database Server logon Password
* @ Return database connection management object
* @ Throws exception parameter error exception
*/
Public static dbmanager getinstance (
String version,
String user,
String password)
Throws exception {
If (! (Version = "2000" | version = "2005"
| Version = "ODBC ")){
Throw new exception ("database driver version number
Incorrect. The value can only be "2000/2005/ODBC "! ");
}
// Save the database driver version
Driverversion = version;
If (user = NULL | user. Equals ("")){
Throw new exception ("Database Server login
The user name cannot be blank! ");
}
// Save the username and password for logging on to the Database Server
Databaseuser = user;
Databasepassword = password;
// The application Singleton [Parts] mode ensures that the class has only one instance
If (connectionmanager = NULL ){
Connectionmanager = new dbmanager ();
}
// Return the instance of the class.
Return connectionmanager;
}
/**
* Methods for obtaining database connections
* @ Return database connection object
*/
Private connection getconnection (){
Try {
Class. forname (
Driverversion =
"2000"
?
Driver_class_sqlserver2000
: (Driverversion =
"2005"
?
Driver_class_sqlserver2005
:
Driver_class_bridgeconnect ));
This. dbconnection =
Drivermanager. getconnection (
Driverversion =
"2000"
?
Database_url_sqlserver2000
: (Driverversion =
"2005"
?
Database_url_sqlserver2005
:
Database_url_bridgeconnect ),
Databaseuser,
Databasepassword );
} Catch (classnotfoundexception ex ){
System. Err. println ("SQL Server not found
"+ Driverversion +" database driver class: "+ ex. getmessage ());
// Output exception stack information on the console
// Ex. printstacktrace ();
} Catch (exception ex ){
System. Err. println ("An error occurred while obtaining the database connection.
Error: "+ ex. getmessage ());
// Output exception stack information on the console
// Ex. printstacktrace ();
}
// Return the database connection object
Return this. dbconnection;
}
/**
* Method for obtaining the database command execution object
* @ Param SQL the SQL command to be executed to assemble the statement string
* @ Return DATABASE Command Execution object
*/
Private preparedstatement getpreparedstatement
(String SQL ){
Try {
// Create a database based on the obtained database connection object
Command Execution object
This. prestatement = getconnection
(). Preparestatement (SQL );
} Catch (exception ex ){
System. Err. println ("Get DATABASE Command Execution
Row object error: "+ ex. getmessage ());
// Output exception stack information on the console
// Ex. printstacktrace ();
}
// Return the database command execution object
Return this. prestatement;
}
/**
* Execute the update Statement (insert | update | delete)
* @ Param SQL the SQL command to be executed to assemble the statement string
* @ Return: Number of affected rows
*/
Public int executeupdate (string SQL ){
Try {
// Empty the original content of the result set object
This. rsset = NULL;
// Execute the statement and return the number of affected rows
Return this. getpreparedstatement
(Sqlcmd.exe cuteupdate ();
} Catch (sqlexception e ){
System. Err. println ("data update error:" +
E. getmessage ());
Return 0;
} Finally {
// Closes database connection resources
Closedbresource ();
}
}
/**
* Execute the query statement (select)
* @ Param SQL the SQL command to be executed to assemble the statement string
* @ Return the result set object after the query
*/
Public resultset executequery (string SQL ){
Try {
// Empty the original content of the result set object
This. rsset = NULL;
// Execute an SQL statement to obtain the result set
This. rsset =
This.getpreparedstatement(sqlcmd.exe cutequery ();
} Catch (sqlexception e ){
System. Err. println ("Data Query error:" +
E. getmessage ());
}
// Return result set object
Return this. rsset;
}
/**
* Obtain the number of records in the returned result set after the specified SQL statement is executed.
* @ Param SQL the SQL command to be executed to assemble the statement string
* @ Return the number of records obtained from the query results
*/
Public int getresultsetcount (string SQL ){
// Save the counter variable for the number of records returned after the specified SQL statement is executed
Int COUNT = 0;
Try {
// Empty the original content of the result set object
This. rsset = NULL;
// Execute an SQL statement to obtain the result set
This. rsset = This. getpreparedstatement
(Sqlcmd.exe cutequery ();
// Traverse the result set and accumulate the counter
While (this. rsset. Next ()){
Count ++;
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return count;
}
/**
* Shut down database connection resources (including result set objects, command execution objects, and connections)
Object)
*/
Public void closedbresource (){
Try {
Closeresultset ();
Closepreparedstatement ();
Closeconnection ();
} Catch (sqlexception sqlex ){
System. Err. println (sqlex. getmessage
());
// Output exception stack information on the console
// Sqlex. printstacktrace ();
}
}
/**
* Method for disabling a result set object
* @ Throws sqlexception
*/
Private void closeresultset () throws sqlexception {
Try {
If (this. rsset! = NULL ){
This. rsset. Close ();
This. rsset = NULL;
}
} Catch (sqlexception sqlex ){
Throw new sqlexception ("Disable result set pair
Error: "+ sqlex. getmessage ());
// Output exception stack information on the console
// Sqlex. printstacktrace ();
}
}
/**
* Method for disabling DATABASE Command Execution objects
* @ Throws sqlexception
*/
Private void closepreparedstatement () throws
Sqlexception {
Try {
If (this. prestatement! = NULL ){
This. prestatement. Close ();
This. prestatement = NULL;
}
} Catch (sqlexception sqlex ){
Throw new sqlexception ("Shut down database life
The execution object is incorrect: "+ sqlex. getmessage ());
// Output exception stack information on the console
// Sqlex. printstacktrace ();
}
}
/**
* Method for disabling database connection
* @ Throws sqlexception
*/
Private void closeconnection () throws sqlexception {
Try {
If (this. dbconnection! = NULL &&(!
This. dbconnection. isclosed ())){
This. dbconnection. Close ();
}
} Catch (sqlexception sqlex ){
Throw new sqlexception ("Disable database connection
Error: "+ sqlex. getmessage ());
// Output exception stack information on the console
// Sqlex. printstacktrace ();
}
}
}
Related Article

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.