DBUtil.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:3k
源码类别:

搜索引擎

开发平台:

Java

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