Package database operation to Javabean

xiaoxiao2021-03-06  62

Endoury the resource that is easily brought by direct database operations. 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 big data, but if you really encounter a query of big data, it is not good, or you have to do direct database operations. :)))

Below is a simple database operation JavaBean code

Dbwrapper.java import java.sql. *; Import java.util. *; Public class dbwrapper {// Define the connection pool object is a static variable, which will always exist until the working directory is turned off. PRIVATE Static DataSource DS = NULL;

// 1. Get the connection with the connection pool // If not multi-database program, it is recommended to use this method // Related content: In Tomcat Management Interface Configuration Connection Pool Public Static Connection OpenConnection () THROWS Exception {// only need to initialize 1 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 JDBC driver to obtain connection // 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)}} finally {ix}} finally {if (rs! = NULL)}} finally {if (rs! = null)} (); CloseConnection (CONN);

Return list;}}

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 by name, pay attention to the case of strictly distinguishing For (int i = 0; i

// Method 2: Traverse Value for (INT I = 0; I

转载请注明原文地址:https://www.9cbs.com/read-84353.html

New Post(0)