当前位置:   article > 正文

配置c3p0-config.xml或者c3p0.properties文件来实例化ComboPooledDataSource对象来连接数据库

配置c3p0-config.xml或者c3p0.properties文件来实例化ComboPooledDataSource对象来连接数据库

配置c3p0-config.xml或者c3p0.properties文件来实例化ComboPooledDataSource对象来连接数据库

1.首先先配置一个c3p0-config.xml文件,这个问价就放在src根目录之下即可

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

<!-- 自定义的c3p0-config.xml -->
<?xml version="1.0" encoding="UTF-8"?>
 
<c3p0-config>
  <default-config>   
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property>
    <property name="user">root</property>
    <property name="password">root</property>
 
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>
  </default-config>
 
  <named-config name="myOwnAdaptation">
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/school</property>
    <property name="user">root</property>
    <property name="password">root</property>
 
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>
  </named-config>
</c3p0-config>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
DataSource ds = new  ComboPooledDataSource("myOwnAdaptation ");
  • 1

参数对应使用哪个config,如果不写,表示使用默认的config,即default-config里的配置,否则使用参数指定的named-config里的配置,这种xml配置的操作即相当于调用了被实例化类对象的setter方法,根据name=”xxx“来对应类中的属性从而实例化了这个对象。

Connection conn = ds.getConnection(); 
  • 1
  • 这里先不讲解之后的增删改查操作,过几天发一篇详细的文章一一举例讲解
conn.close(); 
  • 1

-----------------------------------------------
2、使用c3p0.properties配置文件

c3p0.driverClass = com.mysql.jdbc.Driver
c3p0.jdbcUrl = jdbc:mysql://localhost:3306/happyFactory
c3p0.user = root
c3p0.password =root
c3p0.maxPoolSize = 20
c3p0.minPoolSize = 3
c3p0.maxStatements = 30
c3p0.maxIdleTime = 150
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

(1)在静态初始化中使用Properties类来加载c3p0.properties,并将读取到的property设置到DataSource上去。
代码如下:

DataSource ds = new ComboPooledDataSource();
Properties props = new Properties();
props.load(类名.class.getClassLoader().getResourceAsStream("c3p0.properties"));
ds.setDriverClass(props.getProperty("driverClass"));
ds.setJdbcUrl(props.getProperty("jdbcUrl"));
ds.setUername(props.getProperty("user"));
ds.setPassword (props.getProperty("password "));

Connection conn = ds.getConnection(); 
conn.close(); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

这里再说一下工具类的一般创建(此处是c3p0.properties文件和xml文件卸载了一起,择一即可):


```java
public class Utils {
    
    /**
     * 文件读取,只会执行一次,使用静态代码块
     */
    static {
        //读取文件,获取值
        try {
            //1.创建Properties集合类
            Properties pro = new Properties();
            //获取src路径下的文件--->ClassLoader类加载器
            ClassLoader classLoader = Utils .class.getClassLoader();
            URL resource = classLoader.getResource("c3p0.properties");;
            String path = resource.getPath();
            //2.加载文件
            pro.load(new FileReader(path));
            //3获取数据
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
            driver = pro.getProperty("driver");
            //或者
            //DataSource ds = new  ComboPooledDataSource("myOwnAdaptation ");
            //4.注册驱动
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取连接
     * @return 连接对象
     */
    public static Connection getConnection() throws SQLException {
        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
    }

    /**
     * 释放资源
     * @param rs
     * @param st
     * @param conn
     */
    public static void close(ResultSet rs, Statement st,Connection conn){
        if (rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(st != null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

bye

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/252820
推荐阅读
相关标签