1 package net.javacoding.jspider.core.storage.jdbc; 2 3 import net.javacoding.jspider.core.util.config.PropertySet; 4 import net.javacoding.jspider.core.logging.Log; 5 import net.javacoding.jspider.core.logging.LogFactory; 6 7 import java.net.URL ; 8 import java.sql.*; 9 import java.util.Properties ; 10 11 14 class DBUtil { 15 16 public static final String DRIVER = "driver"; 17 public static final String USER = "user"; 18 public static final String PASSWORD = "password"; 19 public static final String URL = "url"; 20 21 protected Connection connection; 22 23 public DBUtil(PropertySet props) { 24 connection = getConnection(props); 25 } 26 27 public static String format(String string) { 28 return "'" + string + "'"; 29 } 30 31 public static String format(boolean bool) { 32 return bool ? "1" : "0"; 33 } 34 35 public static String format(int i) { 36 return "" + i; 37 } 38 39 public static String format(URL url) { 40 return format("" + url); 41 } 42 43 public Connection getConnection() { 44 return connection; 45 } 46 47 public Connection getConnection(PropertySet props) { 48 Connection connection = null; 49 Log log = LogFactory.getLog(DBUtil.class); 50 try { 51 String driverClassName = props.getString(DRIVER, ""); 52 Class.forName(driverClassName); 53 log.debug("jdbc driver = " + driverClassName); 54 Properties p = new Properties (); 55 p.setProperty("user", props.getString(USER, "")); 56 log.debug("jdbc user = " + props.getString(USER, "")); 57 p.setProperty("password", props.getString(PASSWORD, "")); 58 connection = DriverManager.getConnection(props.getString(URL, ""), p); 59 } catch (ClassNotFoundException e) { 60 log.error("JDBC Driver Class Not Found", e); 61 throw new RuntimeException ("JDBC Driver Class Not Found"); 62 } catch (SQLException e) { 63 log.error("SQL Exception during JDBC Connect", e); 64 throw new RuntimeException ("SQL Exception during JDBC Connect"); 65 } 66 return connection; 67 } 68 69 public void safeClose(ResultSet rs, Log log) { 70 if (rs != null) { 71 try { 72 rs.close(); 73 } catch (SQLException e) { 74 log.error("error closing resultset", e); 75 } 76 } 77 } 78 79 public void safeClose(Statement st, Log log) { 80 if (st != null) { 81 try { 82 st.close(); 83 } catch (SQLException e) { 84 log.error("error closing resultset", e); 85 } 86 } 87 } 88 89 } | Popular Tags |