Sometimes the framework may not be used directly to operate the database quickly, or it is too troublesome to use the framework. If there is a tool class that can directly operate the database, you can directly download the following code: privatestaticLoggerloggerLogger. getLogger (DBHelper. class. getName (); *** java-only connection definition constant
Sometimes the framework may not be used directly to operate the database quickly, or it is too troublesome to use the framework. If there is a tool class that directly operates the database, you can directly import the product and write the following code: private static Logger logger = Logger. getLogger (DBHelper. class. getName ();/*** a java-only connection definition constant
Sometimes
Using the framework may not be quick to directly operate the database,
In other words, it is too troublesome to use the framework, and there is a good tool class for directly operating the database, so you can directly get the goods and write the following code:
Private static Logger logger = Logger. getLogger (DBHelper. class. getName ());
/**
* Java-only connection definition constants are used to store configurations.
*/
Public static String DRIVER = null;
Public static String URL = null;
Public static String USER = null;
Public static String PASS = null;
Connection conn = null;
PreparedStatement pstmt = null;
Statement stmt = null;
ResultSet rs = null;
// Obtain the data connection information.
Static {
Properties pops = new Properties ();
InputStream inStream;
Try {
InStream = DBCommandUtils. class. getResourceAsStream ("/jdbc. properties ");
Pops. load (inStream );
} Catch (FileNotFoundException e ){
Logger. error (e. getMessage ());
} Catch (IOException e ){
Logger. error (e. getMessage ());
}
URL = pops. getProperty ("datasource. url ");
USER = pops. getProperty ("datasource. username ");
PASS = pops. getProperty ("datasource. password ");
DRIVER = pops. getProperty ("datasource. driverClassName ");
}
/**
* Obtain the database connection.
*/
Public Connection getConn (){
Try {
If (DRIVER = null | USER = null | PASS = null | URL = null ){
Properties pops = new Properties ();
InputStream inStream;
Try {
InStream = DBCommandUtils. class
. GetResourceAsStream ("/jdbc. properties ");
Pops. load (inStream );
} Catch (FileNotFoundException e ){
Logger. error (e. getMessage ());
} Catch (IOException e ){
Logger. error (e. getMessage ());
}
URL = pops. getProperty ("datasource. url ");
USER = pops. getProperty ("datasource. username ");
PASS = pops. getProperty ("datasource. password ");
DRIVER = pops. getProperty ("datasource. driverClassName ");
}
// Obtain the link.
Class. forName (DRIVER );
Conn = (Connection) DriverManager. getConnection (URL, USER, PASS );
Return conn;
} Catch (Exception e ){
Logger. error ("failed to get the link! "+ E. getLocalizedMessage ());
Return null;
}
}
/**
* The add, delete, and modify operations to be executed. No query is performed (note the use of parameters)
*/
Public int executeSQL (String preparedSql, String [] param ){
Int count = 0;
/**
* Executed operations
*/
Try {
If (conn = null ){
Conn = getConn (); // get the connection
}
Pstmt = conn. prepareStatement (preparedSql); // The SQL statement to be executed
If (param! = Null ){
For (int I = 0; I <param. length; I ++ ){
Pstmt. setString (1 + I, param [I]); // set parameters for precompiled SQL statements
}
}
Count = pstmt.exe cuteUpdate (); // execute an SQL statement
} Catch (SQLException e ){
Logger. error (e. getMessage (); // handle SQLException exceptions
Return-1;
}
Return count; // return result
}
/**
* The add, delete, and modify operations to be executed. No query is performed (note the use of parameters)
*/
Public int executeSQL (String preparedSql ){
Int count = 0;
/**
* Executed operations
*/
Try {
If (conn = null ){
Conn = getConn (); // get the connection
}
Pstmt = conn. prepareStatement (preparedSql); // The SQL statement to be executed
Count = pstmt.exe cuteUpdate (); // execute an SQL statement
} Catch (SQLException e ){
Logger. error (e. getMessage (); // handle SQLException exceptions
Return-1;
}
Return count; // return result
}
/**
* The add, delete, and modify operations to be executed. No query is performed (note the use of parameters)
*/
Public int [] executeSQLs (String [] sqls ){
Int [] count = null;
/**
* Executed operations
*/
Try {
If (conn = null ){
Conn = getConn (); // get the connection
}
Stmt = conn. createStatement ();
For (String SQL: sqls ){
Stmt. addBatch (SQL );
}
Count = stmt.exe cuteBatch (); // execute an SQL statement
} Catch (SQLException e ){
Logger. error (e. getMessage (); // handle SQLException exceptions
Return null;
}
Return count; // return result
}
/**
* Complex operations to be performed
*/
Public boolean executeAllSQL (String preparedSql ){
Boolean result = false;
Try {
If (conn = null ){
Conn = getConn (); // get the connection
}
Pstmt = conn. prepareStatement (preparedSql); // The SQL statement to be executed
Result = pstmt.exe cute ();
} Catch (SQLException e ){
Logger. error (e. getMessage (); // handle SQLException exceptions
}
Return result; // return result
}
/**
* Use PreparedStatement to query data
*
* @ Param SQL
* @ Param params
* Parameter list
* @ Return: do not close the connection to the result set.
*/
Public ResultSet selectSQL (String SQL, String [] param ){
Try {
If (conn = null ){
Conn = getConn (); // get the connection
}
Pstmt = conn. prepareStatement (SQL); // execute the SQL statement
For (int I = 0; I <param. length; I ++ ){
Pstmt. setString (I + 1, param [I]);
}
Rs = pstmt.exe cuteQuery (); // execution result
} Catch (SQLException e1 ){
Logger. error (e1.getMessage ());
}
Return rs;
}
/**
* Use statement for query
*
* @ Param SQL
* Executed SQL statements
* @ Return cannot close the connection.
*/
Public ResultSet selectSQL (String SQL ){
Try {
If (conn = null ){
Conn = getConn (); // get the connection
}
Pstmt = conn. prepareStatement (SQL );
Rs = pstmt.exe cuteQuery ();
} Catch (SQLException e1 ){
Logger. error (e1.getMessage ());
}
Return rs;
}
/**
* Close all interfaces (note the parameters in brackets)
*/
Public void closeAll (){
If (stmt! = Null ){
Try {
Stmt. close ();
Stmt = null;
} Catch (Exception e ){
Logger. error (e. getMessage ());
}
}
// Determine whether or not to close the service. If you want to close the service, close it and assign it a null value (null ).
If (pstmt! = Null ){
Try {
Pstmt. close ();
Pstmt = null;
} Catch (SQLException e ){
Logger. error (e. getMessage (); // Exception Handling
}
}
If (rs! = Null ){
Try {
Rs. close ();
Rs = null;
} Catch (SQLException e ){
Logger. error (e. getMessage (); // Exception Handling
}
}
If (conn! = Null ){
Try {
Conn. close ();
Conn = null;
} Catch (SQLException e ){
Logger. error (e. getMessage (); // Exception Handling
}
}
}
/**
* Check database connection
*
* @ Param manager
* @ Return true: Unable to connect; false: normal
*/
Public boolean checkCon (DBCommandUtils manager ){
Boolean result = false;
Try {
Result = getConn (). isClosed ();
} Catch (SQLException e ){
Logger. error (e. getMessage ());
}
Return result;
}
/**
* Write a test class to test the database.
*/
Public static void main (String [] args ){
DBCommandUtils manager = new DBCommandUtils ();
Try {
System. out. println (manager. getConn (). isClosed ());
} Catch (SQLException e ){
Logger. error (e. getMessage (); // throw an exception
}
}
This stuff can be used directly. In fact, it's still quite good...
Naturally, it is not comfortable to use the relational framework.
However: sometimes it is quite comfortable to use this framework.