天天看点

JDBC六步骤以及JDBC连接池的创建(含util工具类)

针对不同的程序员用着不一样的JDBC格式,针对初学者会有点迷茫,本人就自己在新学阶段的疑惑来写这篇文章,来帮助新手程序员解惑。

话不多说,进入正题!

JDBC理论六步骤:                 

<不管什么形式都是遵循这6个步骤!>

1.开启类驱动

2..获取数据库的url、username、password

3.连接数据库

4.执行SQL语句

5.处理结果集(遍历)

6.关闭资源

本文就连接池的搭建,基本标准套路地址 →点击打开链接

public class DBProperties {

    public static void main(String[] args) {

       //1. 获取数据库的url、username、password

       Connection conn = null;    
       Properties pro = new Properties();  //properties是专门处理property文件,提取参数

    try{

       FileInputStream fis = new FileInputStream("D:/java16/oracleday07/db.properties");
       pro.load(fis);  //加载文件内容
       fis.close(); //关闭流

       // 2.连接数据库 

       String url = pro.getProperty("url");    //url

       String user =pro.getProperty("user");    //root

       String passwd =pro.getProperty("passwd");   //password

       conn = DriverManager.getConnection(url,user,passwd);  //连接数据库

       //3.执行SQL语句

       String sql = "select * from dept";    //预编译语句
       PreparedStatement st = conn.prepareStatement(sql);
       ResultSet rs = st.executeQuery();

       // 4.处理结果集(遍历)

       while(rs.next()){
           System.out.println(rs.getString(1)+","+rs.getString(2)+","+
           rs.getString(3));
       }

      //5.关闭资源

     rs.close();
     st.close();
     conn.close(); // 关闭连接时自动提交事务

     }catch (Exception e) {
       e.printStackTrace();
     }
   }
}

因为上面这种初学者方式会违背代码的设计原则之一的开闭原则

故有了JDBCutils<数据库连接工具类s>的诞生

JDBCutils<连接池>优点:

写成模板后,可以再也不用写了,哪里需要哪里搬!!!

只需要提供一个外接的接口。哪里需要用JDBC 就直接用接口就行

示例:

public class DbcpUtil {

    private static  DataSource  dataSource;
         //静态代码块----当你运行代码时就自动连接数据库
       static {
           Properties  pro = new Properties();
           InputStream   inputStream = DbcpUtil.class.getClassLoader()
                                      .getResourceAsStream("com/xdl/util/db.properties");
           try {
               pro.load(inputStream);
               dataSource = BasicDataSourceFactory.createDataSource(pro);
           } catch (IOException e) {
                e.printStackTrace();
           } catch (Exception e) {
                e.printStackTrace();
          }
      }
      //创建一个getConnection()方法,需要时直接<类.方法>就行  
       public  static Connection  getConnection(){
         try {
             return  dataSource.getConnection();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return  null;
      }
      //关闭资源!关闭资源!关闭资源!重要的事情说三遍!!!
      public  static  void  realeaseResource(Connection conn,Statement st,
         ResultSet rs){
         if(rs != null){
             try {
                 rs.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }finally{
                 rs = null;
            }
        }
         if(st != null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                st = null;
            }
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                conn = null;
            }
       }
    }
}
           

希望本文能帮助到你~