栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

jdbc学习笔记4

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jdbc学习笔记4

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数据库连接池




    
    
    
        com.mysql.jdbc.Driver
        jdbc:mysql://localhost:3306/jdbc_learn
        root
        111111

        
        
        5
        
        10
        
        10
        
        100
        
        50
        
        5
        
    

代码实现创建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 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 handler=new BeanListHandler<>(Customertest.class);

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

    }
    //返回特殊值使用scalarhandler
总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/704739.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号