zoukankan      html  css  js  c++  java
  • JDBC06-----数据库连接池与配置文件

    一. 池概念

    二. 连接池分类

    三. DBCP连接池

    拿到DataSrouce对象,它里面已经有很多连接了,拿来使用就好了

     1 package com.test.jdbctest.dao.test;
     2 
     3 import java.sql.Connection;
     4 import java.sql.ResultSet;
     5 import java.sql.SQLException;
     6 import java.sql.Statement;
     7 
     8 import org.apache.commons.dbcp2.BasicDataSource;
     9 
    10 import com.test.jdbctest.util.JDBCUtil;
    11 
    12 public class DBCPTest {
    13     public static void main(String args[]) {
    14         String url="jdbc:mysql://localhost:3306/test";
    15         String userName="Hermioner";
    16         String passWord="1234";
    17         String driverName="com.mysql.jdbc.Driver";
    18         
    19         BasicDataSource ds=new BasicDataSource();
    20         ds.setDriverClassName(driverName);
    21         ds.setUsername(userName);
    22         ds.setPassword(passWord);
    23         ds.setUrl(url);
    24         
    25         Connection connection=null;
    26         Statement statement=null;
    27         ResultSet rs=null;
    28         
    29         try {
    30             connection=ds.getConnection();
    31             String sql="select *from stu";
    32             statement=connection.createStatement();
    33             rs=statement.executeQuery(sql);
    34             while(rs.next()) {
    35                 String name=rs.getString("name");
    36                 System.out.println(name);
    37             }
    38         } catch (SQLException e) {
    39             // TODO Auto-generated catch block
    40             e.printStackTrace();
    41         }finally {
    42             JDBCUtil.close(connection, statement, rs);
    43         }
    44         
    45     }
    46 
    47 }
    View Code

    这样就从连接池取连接对象。

    四. 配置文件介绍

    工具类中写了4个成员变量,这样不好,不便于维护,比如测试人员不好使用,怕破坏代码。因此应该写成配置文件格式。

    新建立一个db.properties文件

    url=jdbc:mysql://localhost:3306/test
    user=Hermioner
    pwd=1234
    driverName=com.mysql.jdbc.Driver

    然后建立测试类读取配置文件中的数据

     1 public class PropertiesTest {
     2     public static void main(String args[]) throws IOException {
     3         Properties properties=new Properties();
     4         FileInputStream inputStream=new FileInputStream("resource/db.properties");
     5         properties.load(inputStream);
     6         System.out.println(properties.getProperty("user"));
     7     }
     8 }
     9 
    10 
    11 
    12 Hermioner
    View Code

    五. 配置文件改写DBCP连接池

    必须命名匹配DBCP规则

    note:手动写连接池的代码,可以参考文章:https://www.cnblogs.com/xdp-gacl/p/4002804.html

    参考文献

    https://ke.qq.com/course/339214

  • 相关阅读:
    Failed setting up proxy interface org.apache.hadoop.hbase.ipc.HRegionInterface
    关于操作权限
    什么时候才应该使用HBase?
    模块化服务规范——OSGI
    15个你可能不知道的开源云平台
    hadoop 异常记录 ERROR: org.apache.hadoop.hbase.MasterNotRunningException: Retried 7 times
    Apache nutch1.5 & Apache solr3.6
    Apache cassandra
    谈谈SAAS模式
    卸载VisualStudio插件
  • 原文地址:https://www.cnblogs.com/Hermioner/p/10235993.html
Copyright © 2011-2022 走看看