连接数据库(java驱动连接和连接池连接)

使用java驱动连接数据库:

       String url ="jdbc:mysql://localhost:3306/zhongruan";

       String username ="root";

       String password ="123456";

       //配置环境,jar一个架包

       //JDBC JAVA DATABASE CONICITIVITY

      

       //1.加载驱动:反射去加载驱动

       Class.forName("com.mysql.jdbc.Driver");

      

       //2.建立连接

       Connection con=DriverManager.getConnection(url,username,password);

      

       //3.创建sql对象,创建你自己需要的crud

       Scanner scan = new Scanner(System.in);

 

       System.out.println("请输入姓名");

       String name = scan.next();

       System.out.println("请输入密码");

       String pwd = scan.next();

 

       String sql="select * from  user where name='"+name+"' and  password='"+pwd+"'";

 

       //4.创建编译对象

       Statement st=con.createStatement();

      

       ResultSet rs = null; //结果集

      

       //5.执行sql语句,返回一个结果

       rs = st.executeQuery(sql);

       if(rs.next()){

           System.out.println("成功");

       }

       else{

           System.out.println("失败");

       }

      

       //6.关闭连接

       con.close();

       st.close();

       rs.close();

使用连接池连接数据库:

    static String  url="jdbc:mysql://localhost:3306/zhongruan ";

    static String  DBusername="root";

    static String  DBpassword="123456";

    static String  driverClassName="com.mysql.jdbc.Driver";

   

    /**数据库连接池DBCP

     * 核心的类BasicDataSource

     */

    //实例化DBCP的核心组件

    static BasicDataSource basicDataSource = new BasicDataSource();

   

    static{

       //初始化连接池

       basicDataSource.setDriverClassName(driverClassName);

       basicDataSource.setUrl(url);

       basicDataSource.setUsername(DBusername);

       basicDataSource.setPassword(DBpassword);

       //设置连接池的初始连接数量

       basicDataSource.setInitialSize(20);

       //设置最大的连接数

       basicDataSource.setMaxActive(200);

       //设置最大的超时时间

       basicDataSource.setMaxWait(5000);

    }

   

    /**优点:

     * 1.大大的缩短连接的创建时间

     * 2.简化编程的模型

     * 3.可以让我们的资源受控

     */

一般采用连接池连接,最终方案如下:

这是属性文件db.properties(放在src)

 

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/zhongruan

jdbc.user=root

jdbc.password=123456

jdbc.initSize=20

jdbc.maxActive=200

 

 

这是封装类:

public class Demo{

    /**

     * 1,使用连接池

     * 2,连接池的信息配置到属性文件中

     * 3,通过连接池进行管理

     */

    //实例化Dbcp核心组件

    private  static  BasicDataSource basicDataSource=new BasicDataSource();

    private  Demo(){}

    static{

       //创建

       Properties properties= new Properties();

       //动态获取指向路径db.properties文件的输入流

       InputStream inStream=Demo.class.getClassLoader().getResourceAsStream("db.properties");

      

       //加载我们的这个db.properties的属性文件

       try {

           properties.load(inStream);

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

      

       String driverClass= properties.getProperty("jdbc.driverClass");

       String url=properties.getProperty("jdbc.url");

       String user=properties.getProperty("jdbc.user");

       String password= properties.getProperty("jdbc.password");

       String initSize= properties.getProperty("jdbc.initSize");

      

       int  initalSize=0;

       if (initSize!=null) {

           initalSize=Integer.parseInt(initSize);

       }

       String maxSize=properties.getProperty("jdbc.maxActive");

       int maxActive=0;

       if (maxSize!=null) {

           maxActive=Integer.parseInt(maxSize);

       }

       //初始化连接池

       basicDataSource.setDriverClassName(driverClass);

       basicDataSource.setUrl(url);

       basicDataSource.setUsername(user);

       basicDataSource.setPassword(password);

       //设置连接池的初始连接数量

       basicDataSource.setInitialSize(initalSize);

       //设置最大的连接数

       basicDataSource.setMaxActive(maxActive);

       //设置最大的超时时间

       basicDataSource.setMaxWait(5000);     

             

             

    }

  //关闭连接(几种情况)

    public static void closePre(Connection con,PreparedStatement pst) throws SQLException{

       if (con!=null) {

           con.close();

       }

       if (pst!=null) {

           pst.close();

       }

    }

 

   

    public static void closeAll(Connection con,Statement st,ResultSet rs) throws SQLException{

       if (con!=null) {

           con.close();

       }

       if (st!=null) {

           st.close();

       }

       if (rs!=null) {

           rs.close();

       }

    }

    //获取连接

    public static  Connection getcon() throws SQLException{

       return basicDataSource.getConnection();

      

    }

   

}

猜你喜欢

转载自blog.csdn.net/Ace_2/article/details/81128408
今日推荐