1 28 29 package com.caucho.jslib; 30 31 import com.caucho.util.Exit; 32 import com.caucho.util.ExitListener; 33 34 import javax.naming.InitialContext ; 35 import javax.sql.DataSource ; 36 import java.sql.Connection ; 37 import java.sql.PreparedStatement ; 38 import java.sql.ResultSet ; 39 import java.sql.SQLException ; 40 import java.sql.Statement ; 41 42 55 public class Database { 56 private DataSource dataSource; 57 private Connection conn; 58 private Statement stmt; 59 private ResultSet rs; 60 61 66 public Database(String name) 67 throws Exception 68 { 69 InitialContext cxt = null; 70 71 try { 72 cxt = new InitialContext (); 73 } catch (Exception e) { 74 } 75 76 try { 77 if (cxt != null) 78 dataSource = (DataSource ) cxt.lookup(name); 79 } catch (Exception e) { 80 } 81 82 try { 83 if (dataSource == null && cxt != null) 84 dataSource = (DataSource ) cxt.lookup("java:comp/env/" + name); 85 } catch (Exception e) { 86 } 87 88 try { 89 if (dataSource != null && cxt != null) 90 dataSource = (DataSource ) cxt.lookup("java:comp/env/jdbc/" + name); 91 } catch (Exception e) { 92 } 93 94 if (dataSource == null) 95 throw new SQLException ("no data source: " + name); 96 97 Exit.addExit(exitHandler, this); 99 } 100 101 104 public ResultSet query(String sql) 105 throws SQLException 106 { 107 if (rs != null) { 108 ResultSet rs = this.rs; 109 this.rs = null; 110 rs.close(); 111 } 112 113 Statement stmt = getStatement(); 114 115 rs = stmt.executeQuery(sql); 116 117 return rs; 118 } 119 120 123 public int update(String sql) 124 throws SQLException 125 { 126 if (rs != null) { 127 ResultSet rs = this.rs; 128 this.rs = null; 129 rs.close(); 130 } 131 132 Connection conn = dataSource.getConnection(); 133 134 try { 135 Statement stmt = conn.createStatement(); 136 137 int count = stmt.executeUpdate(sql); 138 139 stmt.close(); 140 141 return count; 142 } finally { 143 conn.close(); 144 } 145 } 146 147 151 public DataSource getDataSource() 152 throws SQLException 153 { 154 return dataSource; 155 } 156 157 161 public Connection getConnection() 162 throws SQLException 163 { 164 if (conn == null) { 165 conn = dataSource.getConnection(); 166 } 167 168 return conn; 169 } 170 171 174 public void commit() 175 throws SQLException 176 { 177 if (this.conn != null) { 178 Connection conn = this.conn; 179 this.conn = null; 180 this.stmt = null; 181 this.rs = null; 182 conn.close(); 183 } 184 } 185 186 190 public Statement getStatement() 191 throws SQLException 192 { 193 if (stmt == null) { 194 Connection conn = getConnection(); 195 196 stmt = conn.createStatement(); 197 } 198 199 return stmt; 200 } 201 202 206 public PreparedStatement prepare(String sql) 207 throws SQLException 208 { 209 Connection conn = getConnection(); 210 211 return conn.prepareStatement(sql); 212 } 213 214 218 public void close() 219 throws SQLException 220 { 221 try { 222 if (rs != null) { 223 ResultSet rs = this.rs; 224 this.rs = null; 225 rs.close(); 226 } 227 228 if (stmt != null) { 229 Statement stmt = this.stmt; 230 this.stmt = null; 231 stmt.close(); 232 } 233 } finally { 234 this.stmt = null; 235 this.rs = null; 236 if (conn != null) { 237 Connection conn = this.conn; 238 this.conn = null; 239 conn.close(); 240 } 241 } 242 } 243 244 248 private static ExitListener exitHandler = new ExitListener() { 249 public void handleExit(Object o) 250 { 251 Database db = (Database) o; 252 253 try { 254 db.close(); 255 } catch (SQLException e) { 256 } 257 } 258 }; 259 } 260 | Popular Tags |