1 6 7 package org.contineo.setup; 8 9 import java.sql.Connection ; 10 import java.sql.DriverManager ; 11 import java.sql.ResultSet ; 12 13 import org.apache.log4j.Level; 14 import org.apache.log4j.Logger; 15 import org.contineo.core.LoggingManager; 16 import org.contineo.core.config.RepositoryConfigurator; 17 18 25 public class DBCon { 26 27 private java.sql.Statement stmt = null; 28 private Connection dbcon; 29 private Logger logger; 30 31 37 private RepositoryConfigurator conf; 38 39 40 41 public DBCon() { 42 logger = LoggingManager.getLogger(this.getClass()); 43 conf = new RepositoryConfigurator(); 44 } 45 46 protected void doConnection() { 47 try { 48 Class.forName(conf.getAttribute("contineo", "driver")); 49 String url = conf.getAttribute("contineo", "protocol") + ":"; 50 url += conf.getAttribute("contineo", "subprotocol") + ":"; 51 url += conf.getAttribute("contineo", "dbalias"); 52 String username = conf.getAttribute("contineo", "username"); 53 String password = conf.getAttribute("contineo", "password"); 54 dbcon = DriverManager.getConnection(url, username, password); 55 dbcon.setAutoCommit(true); 56 stmt = dbcon.createStatement(); 57 } 58 catch (Exception ex) { 59 if (logger.isEnabledFor(Level.ERROR)) 60 logger.error(ex.getMessage()); 61 } 62 } 63 64 protected void rollback() { 65 try { 66 dbcon.rollback(); 67 } 68 catch (Exception ex) { 69 if (logger.isEnabledFor(Level.ERROR)) 70 logger.error("can't rollback"); 71 } 72 } 73 74 77 public boolean isConnected() { 78 try { 79 return !dbcon.isClosed(); 80 } 81 catch (Exception ex) { 82 if (logger.isEnabledFor(Level.DEBUG)) 83 logger.debug("db-connection is open:" + ex.getMessage()); 84 return false; 85 } 86 } 87 88 93 public ResultSet executeQuery(String query) { 94 ResultSet rs = null; 95 try { 96 if (!isConnected()) 97 this.doConnection(); 98 rs = stmt.executeQuery(query); 99 } 100 catch (Exception ex) { 101 if (logger.isEnabledFor(Level.ERROR)) 102 logger.error(ex.getMessage()); 103 } 104 return rs; 105 } 106 107 112 public int executeUpdate(String query) throws UpdateException { 113 int result = -1; 114 try { 115 if (!isConnected()) 116 this.doConnection(); 117 result = stmt.executeUpdate(query); 118 } 120 catch (Exception ex) { 121 if (logger.isEnabledFor(Level.ERROR)) 122 logger.error(ex.getMessage()); 123 throw new UpdateException(query + " failed."); 124 } 126 return result; 127 } 128 129 132 public boolean testConnection() { 133 boolean result = true; 134 try { 135 Class.forName(conf.getAttribute("contineo", "driver")); 136 String url = conf.getAttribute("contineo", "protocol") + ":"; 137 url += conf.getAttribute("contineo", "subprotocol") + ":"; 138 url += conf.getAttribute("contineo", "dbalias"); 139 String username = conf.getAttribute("contineo", "username"); 140 String password = conf.getAttribute("contineo", "password"); 141 dbcon = DriverManager.getConnection(url, username, password); 142 result = true; 143 dbcon.close(); 144 } 145 catch (Exception ex) { 146 if (logger.isEnabledFor(Level.ERROR)) 147 logger.error(ex.getMessage()); 148 result = false; 149 } 150 return result; 151 } 152 } 153 | Popular Tags |