jdbc学习笔记4

news/2024/5/19 1:38:04 标签: 数据库, mysql, java, jdbc

jdbc4_0">jdbc的学习笔记4

数据库连接池

数据库连接池的必要性

java调用drivermanger类时,每次想数据库建立连接时都会将connection加载到内存中,执行完成后再断开连接,这样的方式会导致,会消耗大量的内存和时间,数据库的连接资源没有得到好的利用,若出现很多人同时操作会导致服务器崩溃。对于每次使用完都需要断开,如果出现异常未能操作完成,回导致数据哭的内存泄漏,这样的方式不能控制连接的对象数。

数据库连接池技术:(个人理解为公共交通工具)

数据库连接建立一个连接池,预先缓冲一部分连接,当需要时取出使用完放回。
可以控制资源的数量,解决资源浪费或者系统崩溃。

c3p0

使用c3p0连接池技术创建一个数据库连接池

    @Test
    public void test () throws Exception {
//获取c3p0连接池
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver
        cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/jdbc_learn?rewriteBatchedStatements=true" );
        cpds.setUser("root");
        cpds.setPassword("111111");
        cpds.setInitialPoolSize(10);//设置连接池中初始连接数
        Connection connection = cpds.getConnection();
        System.out.println(connection);

    }

使用配置文件进行创建c3p0数据库连接池

<?xml version="1.0" encoding="ISO-8859-1"?>

<c3p0-config>
    <!-- This app is massive! -->
    <!--获取建立连接的四个基本信息-->
    <named-config name="HelloC3P0">
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="JdbcUrl">jdbc:mysql://localhost:3306/jdbc_learn</property>
        <property name="User">root</property>
        <property name="Password">111111</property>
<!--c3po,?????????????????????????-->
        <!-- 进行数据库连接处-->
        <!--当连接数不够时一次向数据库申请的数据-->
        <property name="acquireIncrement">5</property>
        <!--初始化连接数为initialpoolsize-->
        <property name="initialPoolSize">10</property>
        <!--最小连接数-->
        <property name="minPoolSize">10</property>
        <!--最大连接数-->
        <property name="maxPoolSize">100</property>
        <!-- intergalactoApp adopts a different approach to configuring statement caching -->
        <property name="maxStatements">50</property>
        <!--维护的最多的statement的个数-->
        <property name="maxStatementsPerConnection">5</property>
        <!--每个连接可以舒勇的最多的连接个数-->
    </named-config>
</c3p0-config>

代码实现创建c3p0数据库连接池

  @Test
    public void test1() throws Exception {
        ComboPooledDataSource cpds =new ComboPooledDataSource("HelloC3P0");//为配置文件中的
        Connection conn=cpds.getConnection()   ;
        System.out.println(conn);
    }

DBCP

    @Test
    public void test() throws SQLException {
        BasicDataSource source=new BasicDataSource();//创建dbcp的数据库连接池
        source.setDriverClassName("com.mysql.jdbc.Driver" );
        source.setUrl("jdbc:mysql://localhost:3306/jdbc_learn");
        source.setUsername("root");
        source.setPassword("111111");
        Connection connection = source.getConnection();
       

    }

使用配置文件dbcp.propertise

driverClassname=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc_learn
username=root
password=111111

实现连接池


    @Test
    public  void test1() throws Exception {
        Properties pro =new Properties();
        InputStream is = new FileInputStream(new File("src/dbcp.properties"));
        pro.load(is);
        BasicDataSource dataSource = BasicDataSourceFactory.createDataSource(pro);
        Connection connection = dataSource.getConnection();
    }

德鲁伊Druid

配置文件

url=jdbc:mysql://localhost:3306/jdbc_learn
username=root
password=111111
driverClassname=com.mysql.jdbc.Driver

实现代码

public class Druidtest {
    @Test
            public void test() throws Exception {
        Properties properties=new Properties();
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("src/druid.propertise");
        properties.load(is);
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        System.out.println(connection);

    }}

DButils

实现增删改操作

    @Test
    public void test() throws Exception {
        QueryRunner queryRunner = new QueryRunner();
        Connection conn = JdbcUtils.getConnection();
        String sql ="insert into Customers(name,email,birth)values(?,?,?)";

        queryRunner.update(conn,sql,"蔡徐坤","caixukong.com","1998-01-09");
    }

实现查询操作

    @Test//返回一条记录使用beanhandler
    public void  test2() throws Exception {
        QueryRunner runner=new QueryRunner();
        Connection connection = JdbcUtils.getConnection();
        String sql="select id,name,email,birth from customers where=?";
        BeanHandler<Customertest> handler=new BeanHandler<>(Customertest.class);
        Customertest query = runner.query(connection, sql, handler, 21);
        System.out.println(query);

    }
    @Test、、返回多条记录使用beanlisthandler
    public void  test3() throws Exception {
        QueryRunner runner=new QueryRunner();
        Connection connection = JdbcUtils.getConnection();
        String sql="select id,name,email,birth from customers where<?";
        BeanListHandler<Customertest> handler=new BeanListHandler<>(Customertest.class);

        List<Customertest> query = runner.query(connection, sql, handler, 21);
        query.forEach(System.out::println);

    }
    //返回特殊值使用scalarhandler

总结

完成jdbc至此完,略显粗糙
剑谱最终页,无爱即是神


http://www.niftyadmin.cn/n/764779.html

相关文章

P1880 [NOI1995] 石子合并

[NOI1995] 石子合并 - 洛谷 /** Description: To iterate is human, to recurse divine.* Autor: Recursion* Date: 2022-04-02 23:00:48* LastEditTime: 2022-04-05 10:38:03*/ #include <bits/stdc.h> #define LL long long using namespace std; const int maxn 1e…

javaweb学习笔记(html常用标签,css样式,JavaScript基础)数据库约束)

JAVAWEB学习笔记 HTML 通过标签显示在网页的各个部分来显示内容 <!DOCTYPE html><!--声明约束--> <html lang"zh_CN"> <head><!--头部文件&#xff0c;一般包括标题&#xff0c;css样式&#xff0c;js代码--><meta charset"…

P1775 石子合并(弱化版)(区间dp模板)

石子合并&#xff08;弱化版&#xff09; - 洛谷 /** Description: To iterate is human, to recurse divine.* Autor: Recursion* Date: 2022-04-05 10:48:41* LastEditTime: 2022-04-05 11:52:33*/ #include <bits/stdc.h> #define LL long long using namespace std…

javaweb学习笔记2(jquery的使用,以及常用的方法,选择器,过滤器)

javaweb学习笔记2 javascript正则表达式 regfxp对象 方式1&#xff1a; var puttnew RegExp("e");//表示要求字符串中必须包含字符串evar str"abcde";alert(putt.test(str)); 方式2&#xff1a;var putt/e/;var str "abcde";alert(putt.test(…

P4702 取石子

取石子 - 洛谷 /** Description: To iterate is human, to recurse divine.* Autor: Recursion* Date: 2022-04-05 12:18:55* LastEditTime: 2022-04-05 12:21:26*/ #include <bits/stdc.h> #define LL long long using namespace std; const int maxn 1e6 10; const…

javaweb学习笔记3(tomcat安装,idea建立一个web工程,servlet常见方法和工具类)

javaweb学习笔记3 javaweb概念 javaweb指所有通过java语言编写的通过浏览器访问的程序的综合 javaweb是基于请求和响应来开发 请求&#xff1a; 是指客户端个服务器发送数据&#xff0c;request 响应&#xff1a;服务器给客户端回传数据为response web资源的分类 按实现技术…

P1095 [NOIP2007 普及组] 守望者的逃离

[NOIP2007 普及组] 守望者的逃离 - 洛谷 /** Description: To iterate is human, to recurse divine.* Autor: Recursion* Date: 2022-04-05 16:11:33* LastEditTime: 2022-04-05 16:46:04*/ #include <bits/stdc.h> #define LL long long using namespace std; const …

P1451 求细胞数量

求细胞数量 - 洛谷 /** Description: To iterate is human, to recurse divine.* Autor: Recursion* Date: 2022-04-05 20:25:15* LastEditTime: 2022-04-05 20:31:43*/ #include <bits/stdc.h> #define LL long long using namespace std; const int maxn 1e6 10; c…