The principle is to store a single piece of data with map and put it in the list.
In this way, you can call list = Rs. selectrs (sqlstr );
For (INT I = 0; I <list. size; I ++)
{
(MAP) list. Get (I). Get ("username ");
}
In this way, you do not need to write database connections every time, and you can implement I control through complicated page format output.
The following are the specific implementation classes that can be used in combination with the struts and webwork2 labels. I have tested them. No problem.
I also see that some people write their own classes to replace map.
Package com;
Import conn. dbconnmanager;
Import java. SQL .*;
Import java. util .*;
Public class resultgather
{
Private string SQL;
Public resultgather ()
{
}
Public resultgather (string sqlcom)
{
This. SQL = sqlcom;
}
Public list selectrs (string sqlstr)
{
This. SQL = sqlstr;
Return selectrs ();
}
Public list selectrs ()
{
List rsall = new arraylist ();
Map rstree;
Dbconnmanager conn = NULL;
Connection con = NULL;
Statement stmt = NULL;
Resultset rs = NULL;
Try {
Conn = dbconnmanager. getinstance ();
Con = conn. getconnection ("MSSQL ");
Stmt = con. createstatement ();
Rs = stmt.exe cutequery (SQL );
Resultsetmetadata rsmd = Rs. getmetadata (); // retrieve the database column name. I think the name is better than 1, 2, and 3 ..
Int numberofcolumns = rsmd. getcolumncount ();
While (Rs. Next ())
{
Rstree = new hashmap (numberofcolumns); // pay attention to the new
For (INT r = 1; r <numberofcolumns + 1; r ++)
{
Rstree. Put (rsmd. getcolumnname (R), RS. GetObject (R ));
}
Rsall. Add (rstree );
}
} Catch (Java. Lang. Exception ex ){
Ex. printstacktrace ();
} Finally {
Try {
If (RS! = NULL)
Rs. Close ();
If (stmt! = NULL)
Stmt. Close ();
If (Conn! = NULL)
Conn. releaseconnection ("MSSQL", con );
} Catch (exception e ){
}
}
Return rsall;
}
}
Encapsulate database operations to hide classes in the Java. SQL package and remove the core database operation code from coding. To prevent the resources that are easily caused by direct database operations from being released
Problem. It also reduces the encoding of database operations.
However, many users prefer the resultset object when encapsulating it, so this encapsulation is meaningless.
1. It is also a direct operation of core database classes, with almost no changes before encapsulation.
2. The result set always depends on the connection object it uses. Therefore, when the connection object is closed in the method, the resultset you returned is useless.
If you really want to obtain the result set of the query database, all the data in the result set object will be dumped to the list object with map as the element.
Of course, this method cannot adapt to queries of large data volumes. However, if you encounter a query of large data volumes, it is not good to use any encapsulation. You still have to perform database operations directly .:)))
The following is a simple code for operating the JavaBean database:
Dbwrapper. Java
Import java. SQL .*;
Import java. util .*;
Public class dbwrapper
{
// Define the connection pool object as a static variable until the working directory is closed.
Private Static datasource DS = NULL;
// 1. Use the connection pool to obtain the connection
// This method is recommended if it is not a multi-database program
// Related content: configure the connection pool on the Tomcat Management Interface
Public static connection openconnection () throws exception
{
// Only needs to be initialized once
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. Use the JDBC driver to obtain the connection
// Related content: JSP database connection Overview
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.exe cuteupdate (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.exe cutequery (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 ;}
}
Example:
// 1. For insert, update, and delete statements, int COUNT = dbwrapper.exe cuteupdate (SQL );
// 2. For the selete statement
Java. util. List list = dbwrapper.exe cutequery (SQL );
// Method 1: The value is based on the name. Note that the case sensitivity is strictly different.
For (INT I = 0; I <list. Size (); 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 <list. Size (); I ++)
{
Java. util. Map map = (Java. util. Map) list. Get (I );
For (Java. util. iterator it = map. keyset (). iterator (); it. hasnext ();)
{
String column_name = it. Next (). tostring (); // pay attention to null when determining the value
Out. println (column_name + "=" + map. Get (column_name) = NULL? "": Map. Get (column_name). tostring ());
}
}