Tutorials to .com

Tutorials to .com » Jsp » Skills » To encapsulate the database operations Javabean

To encapsulate the database operations Javabean

Print View , by: iSee ,Total views: 12 ,Word Count: 808 ,Date: Tue, 25 Aug 2009 Time: 4:06 PM

Package database operations, the purpose is to hide the java.sql package of classes, in the encoding of the database operation to remove the core code. To eliminate direct database resources are not easy to bring the issue of release. Also reduce the amount of database operations coded.

But many User in the package, they prefer to return the result set (ResultSet object), then this package does not make sense.
1. Again the direct operation of the core database classes, with the package, with virtually no change.
2. The result set is always dependent on its use of the connection (Connection) object. So when the connection object is closed within the method, you return ResultSet not used.

If you really want to get a database query result set, put the object within a result set all the data to dump into the Map object for the element within the List.
Of course, this approach can not adapt to large amounts of data query, but if we really encounter large amounts of data query, then what package is not good, still have a direct database operations.:)))

The following is a simple database operation code Javabean

DbWrapper.java
import java.sql .*;
import java.util .*;
public class DbWrapper
(
/ / Define connection pool object as a static variable, will remain there until the working directory close.
private static DataSource ds = null;
/ / 1. To use connection pooling way of acquiring link
/ / If not do more database programs, it is recommended to use this method
/ / Related Content: In the tomcat management interface to configure connection pool
public static Connection openConnection () throws Exception
(
/ / Only need to initialize a sub -
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. Connect with jdbc driver to obtain
/ / Related Content: jsp database connection Daquan
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 the insert, update, delete statement int count = DbWrapper.executeUpdate (sql);
/ / 2. For selete statement
java.util.List list = DbWrapper.executeQuery (sql);
/ / Method 1: According to the name value, pay attention to case is the strict distinction between the
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: traversal 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 ()); / / Note that null values to determine when the
out.println (column_name + "=" + map.get (column_name) == null? "": map.get (column_name). toString ());
)
)


Java skills and code Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.