Encapsulates a Java database access management class and a java database management class.
Copy codeThe 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 ();
}
}
}