Package database operations, the purpose is to hide classes in the java.sql package, remove the core database operation code in the encoding. The resource has not released the problem of easy brought by eliminating direct database operation. At the same time, it also reduces the amount of encoding of the database operation. But many netizens are in the package, but I like to return the result set (Resultset object), then this package is meaningless. 1. It is also a direct operation of the core database class, which has little change in prior to the package. 2. Result sets always depend on the connection object it uses. Therefore, when the connection object is turned off within the method, the resultSet you returned is useless. If you really want to get the result set of the query database, all the data within the result set object, dump all of the List objects in Map as an element. Of course, this way, it is not possible to adapt to the query of large data, but if you really encounter a query of big data, it is not good, or you have to be directly database operation. :)) The following is a simple database operation. JavaBean code
Dbwrapper.javaimport java.sql. *;
Import java.util. *;
Public Class DBWrapper
{
// Defines the connection pool object as a static variable, which will always exist until the working directory is turned off.
PRIVATE Static DataSource DS = NULL;
// 1. Get a connection with the connection pool
// If you are not doing a multi-database program, it is recommended to use this method.
// Related content: Configure the connection pool in the Tomcat management interface
Public Static Connection OpenConnection () THROWS EXCEPTION
{
// only need to initialize 1 time
IF (ds == null)
{
Context INITCONTEXT = New InitialContext ();
Context envcontext = (context) INITCONTEXT.LOOKUP ("Java: / Comp / ENV");
DataSource DS = (Datasource) ENVCONTEXT.LOOKUP ("JDBC / MyDataSource");
}
Return DS.GetConnection ();
}
// 2. Drive to connect with JDBC
// Related content: JSP database connection
Public Static Connection OpenConnection
String driver,
String URL,
String username,
String Password)
Throws Exception
{
Class.Forname (driver) .newinstance ();
Return DriverManager.getConnection (URL, Username, Password);
}
Public Static Void CloseConnection (Connection Conn) THROWS EXCEPTION
{
IF (CONN! = NULL)
{
CONN.CLOSE ();
}
}
Public Static int ExecuteUpdate (String SQL) THROWS EXCEPTION
{
INT count = 0;
Connection conn = NULL;
Statement Stmt = NULL;
Try
{
CONN = OpenConnection ();
STMT = conn.createstatement ();
Count = stmt.executeUpdate (SQL);
}
Catch (Exception E)
{
Throw e;
}
Finally
{
CloseConnection (CONN);
Return count;
}
Public Static List ExecuteQuery (String SQL) THROWS EXCEPTION
{
List list = new arraylist ();
Connection conn = NULL;
Statement Stmt = NULL;
ResultSet RS = NULL;
Try
{
CONN = OpenConnection ();
STMT = conn.createstatement ();
RS = stmt.executequery (SQL);
ResultSetmetaData RSMD = rs.getMetadata ();
While (rs.next ())
{
Map map = new hashmap ();
For (int i = 1; i <= rsmd.getcolumncount (); i )
{
Map.Put (RSMD.GetColumnName (i), RS.GetObject (i));
}
List.Add (Map);
}
Catch (Exception E)
{
E.PrintStackTrace ();
}
Finally
{
IF (RS! = null) rs.close ();
CloseConnection (CONN);
}
Return List;}
}
Use example:
/ / 1. For INSERT, UPDATE, DELETE statement int count = dbwrapper.executeUpdate (SQL);
// 2. For Selete statement
Java.util.List List = dbwrapper.executeQuery (SQL);
// Method 1: Value according to the name, pay attention to the case is strictly distinguished
For (int i = 0; i { Java.util.map map = (java.util.map) list.get (i); Out.println (MAG.GET ("Column_name"). TOSTRING ()); } // Method 2: Traverse the value For (int i = 0; i { Java.util.map map = (java.util.map) list.get (i); For (java.util.Itemrator it = map.keyset (). Itrator (); it.hasnext ();) { String column_name = it.next (). Tostring ()); note NULL judgment when value Out.println (Column_Name "=" map.get (colorn_name) == NULL? "": map.get (color (column_name) .tostring ()); } }