1 13 14 package org.ejbca.util; 15 16 import java.sql.Connection ; 17 import java.sql.PreparedStatement ; 18 import java.sql.ResultSet ; 19 import java.sql.SQLException ; 20 21 import javax.sql.DataSource ; 22 23 import org.apache.log4j.Logger; 24 import org.ejbca.core.ejb.ServiceLocator; 25 import org.ejbca.core.ejb.ServiceLocatorException; 26 27 28 29 39 public class JDBCUtil { 40 41 private static final Logger log = Logger.getLogger(JDBCUtil.class); 42 43 public interface Preparer { 44 void prepare(PreparedStatement ps) throws Exception ; 45 String getInfoString(); 46 } 47 48 public static void execute(String sqlCommandTemplate, Preparer preparer, String dataSource) throws Exception { 49 if ( sqlCommandTemplate!=null ) { 50 Connection connection = null; 51 ResultSet result = null; 52 PreparedStatement ps = null; 53 try { 54 connection = ServiceLocator.getInstance().getDataSource(dataSource).getConnection(); 55 ps = connection.prepareStatement(sqlCommandTemplate); 56 preparer.prepare(ps); 57 if ( ps.execute() ) 58 result = ps.getResultSet(); 59 } finally { 60 JDBCUtil.close(connection, ps, result); 61 } 62 } 63 } 64 public static String executeSelectString(String sqlCommandTemplate, Preparer preparer, String dataSource) throws Exception { 65 String ret = null; 66 if ( sqlCommandTemplate!=null ) { 67 Connection connection = null; 68 ResultSet result = null; 69 PreparedStatement ps = null; 70 try { 71 connection = ServiceLocator.getInstance().getDataSource(dataSource).getConnection(); 72 ps = connection.prepareStatement(sqlCommandTemplate); 73 preparer.prepare(ps); 74 if ( ps.execute() ) { 75 result = ps.getResultSet(); 76 if (result.next()) { 77 ret = result.getString(1); 78 } 79 } 80 } finally { 81 JDBCUtil.close(connection, ps, result); 82 } 83 } 84 return ret; 85 } 86 87 88 96 public static DataSource getDataSource(String dsName) 97 throws ServiceLocatorException { 98 String dataSourceName = ServiceLocator.getInstance().getString(dsName); 99 return ServiceLocator.getInstance().getDataSource(dataSourceName); 100 } 101 102 110 public static Connection getDBConnection(String dsName) throws ServiceLocatorException { 111 try { 112 return getDataSource(dsName).getConnection(); 113 } catch (SQLException e) { 114 throw new ServiceLocatorException("Error while getting db connection", e); 115 } 116 } 117 118 123 public static void close(Connection con) { 124 if (con != null) 125 try { 126 con.close(); 127 } catch (SQLException e) { 128 log.warn("Could not close connection", e); 129 } 130 } 131 132 137 public static void close(ResultSet rs) { 138 if (rs != null) 139 try { 140 rs.close(); 141 } catch (SQLException e) { 142 log.warn("Could not close ResultSet", e); 143 } 144 } 145 146 151 public static void close(PreparedStatement ps) { 152 if (ps != null) 153 try { 154 ps.close(); 155 } catch (SQLException e) { 156 log.warn("Could not close PreparedStatement", e); 157 } 158 } 159 160 168 public static void close(Connection con, PreparedStatement ps, ResultSet rs) { 169 close(rs); 170 close(ps); 171 close(con); 172 } 173 180 public static boolean columnExists(Connection con, String table, String col) { 181 PreparedStatement st = null; 182 try { 183 st = con.prepareStatement("select "+col+" from "+table+" where 1=0"); 184 st.executeQuery(); 185 return true; 186 } catch (SQLException e) { 187 return false; 188 } finally { 189 JDBCUtil.close(st); 190 } 191 } 192 } 193 | Popular Tags |