zoukankan      html  css  js  c++  java
  • 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();
        }
       }
      }
     }
    }

  • 相关阅读:
    上拉、下拉无限滚动组件-pc端
    elementui 表格应用1:多选+搜索框+保持选中状态
    uniapp初识笔记3
    uniapp 动态获取接口域名
    uniapp初识笔记2
    uniapp上传图片、视频到oss
    uniapp初识笔记
    vue登录页作为modal全局使用
    vue hash模式下微信分享,实现参数传递
    Vscode以管理员身份运行powershell
  • 原文地址:https://www.cnblogs.com/mingforyou/p/2291406.html
Copyright © 2011-2022 走看看