1 30 31 package com.caucho.eswrap.com.caucho.sql; 32 33 import com.caucho.es.ESException; 34 import com.caucho.sql.DBPool; 35 import com.caucho.util.Exit; 36 import com.caucho.util.ExitListener; 37 38 import javax.naming.Context ; 39 import javax.naming.InitialContext ; 40 import java.sql.Connection ; 41 import java.sql.ResultSet ; 42 import java.sql.SQLException ; 43 import java.sql.Statement ; 44 import java.util.ArrayList ; 45 46 public class DBPoolEcmaWrap { 47 public static DBPool call(String name) 48 throws Exception 49 { 50 Context cxt = (Context ) new InitialContext ().lookup("java:comp/env"); 51 52 return (DBPool) cxt.lookup(name); 53 } 54 55 public static Connection getConnection(DBPool pool) 56 throws SQLException 57 { 58 Connection conn = pool.getConnection(); 59 60 Exit.addExit(exitConnection, conn); 62 63 return conn; 64 } 65 66 public static Statement createStatement(DBPool pool) 67 throws SQLException 68 { 69 Connection conn = getConnection(pool); 70 71 Statement stmt = conn.createStatement(); 72 73 Exit.addExit(exitStatement, stmt); 75 76 return stmt; 77 } 78 79 public static int executeUpdate(DBPool pool, String msg) 80 throws SQLException 81 { 82 Connection conn = pool.getConnection(); 83 84 try { 85 Statement stmt = conn.createStatement(); 86 87 try { 88 return stmt.executeUpdate(msg); 89 } finally { 90 stmt.close(); 91 } 92 } finally { 93 conn.close(); 94 } 95 } 96 97 public static Object executeQuery(DBPool pool, String msg) 98 throws ESException, SQLException 99 { 100 Connection conn = pool.getConnection(); 101 102 try { 103 Statement stmt = conn.createStatement(); 104 ArrayList array = new ArrayList (); 105 106 try { 107 ResultSet rs = stmt.executeQuery(msg); 108 109 try { 110 int i = 0; 111 while (rs.next()) { 112 } 114 return array.toArray(new Object [array.size()]); 115 } finally { 116 rs.close(); 117 } 118 } finally { 119 stmt.close(); 120 } 121 } finally { 122 conn.close(); 123 } 124 } 125 126 private static ExitListener exitConnection = new ExitListener() { 127 public void handleExit(Object o) 128 { 129 Connection conn = (Connection ) o; 130 131 try { 132 conn.close(); 133 } catch (SQLException e) { 134 } 135 } 136 }; 137 138 private static ExitListener exitStatement = new ExitListener() { 139 public void handleExit(Object o) 140 { 141 Statement stmt = (Statement ) o; 142 143 try { 144 stmt.close(); 145 } catch (SQLException e) { 146 } 147 } 148 }; 149 } 150 | Popular Tags |