JDBC工具类与数据库建立连接

1.daoConfig.properties

userDaoClass=cn.itcast.jdbc.dao.impl.UserDaoJdbcImpl
#userDaoClass=cn.itcast.jdbc.dao.impl.UserDaoHibernateImpl

-----------------------------------------------------------------------------------------------------------

public final class JdbcUtils {
 private static DataSource myDataSource = null;

 private JdbcUtils() {}

 static {
  try {
   // 注册驱动
   Class.forName("com.mysql.jdbc.Driver");// 推荐
   // DriverManager.registerDriver(new com.mysql.jdbc.Driver());//2
   // System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver[:...:...]");//注册方式3可以注册多个驱动用“:”分隔。
   Properties prop = new Properties();
   // 读取配置文件
   InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
   prop.load(is);
   myDataSource = BasicDataSourceFactory.createDataSource(prop);
  } catch (Exception e) {
   throw new ExceptionInInitializerError(e);
  }
 }

 public static Connection getConnection() throws SQLException {

  return myDataSource.getConnection();
 }

 public static void free(ResultSet rs, Statement st, Connection conn) {
  try {
   if (rs != null)
    rs.close();
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   try {
    if (st != null)
     st.close();
   } catch (SQLException e) {
    e.printStackTrace();
   } finally {
    try {
     if (conn != null)
      conn.close();
     // myDataSource.free(conn);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  }
 }
}

-----------------------------------------------------------------------------------------------

使用单例模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点。

public final class JdbcUtilsSing {
 private String url="jdbc:mysql://localhost:3306/jdbc";
 private String user="root";
 private String password="064417";
 private static JdbcUtilsSing instance=null;
// private static JdbcUtilsSing instance=new JdbcUtilsSing();
 private JdbcUtilsSing(){
  
 }
 public static JdbcUtilsSing getInstance(){
  if(instance==null){
   synchronized(JdbcUtilsSing.class){
    if(instance==null){
     instance=new JdbcUtilsSing();
    }
   }
  }
  return instance;
 }
 static{
  try {
//   注册驱动
   Class.forName("com.mysql.jdbc.Driver");//推荐
//   DriverManager.registerDriver(new com.mysql.jdbc.Driver());//2
//   System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver[:...:...]");//注册方式3可以注册多个驱动用“:”分隔。
  } catch (ClassNotFoundException e) {
   throw new ExceptionInInitializerError(e);
  }
 }
 public Connection getConnection() throws SQLException{
  return DriverManager.getConnection(url, user, password);
 }
 public void free(ResultSet rs,Statement st,Connection conn){
  try {
   if(rs!=null)
    rs.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }finally{
   try {
    if(st!=null)
    st.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }finally{
    try {
     if(conn!=null)
     conn.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
  }
 }
}

posted @ 2011-12-17 22:14  残星  阅读(2649)  评论(0编辑  收藏  举报