其他分享
首页 > 其他分享> > JDCB-连接

JDCB-连接

作者:互联网

1、利用数据库连接池

import con.alibaba.drid.pool.DruidDataSourceFactory
public class TestDruid{
    public static void main(String[] args) throws Excepion{
        Properties pro = new Properties();
        pro.load(TestDruid.class.getClassLoad().getResourceAsStream("druid.properties"));
        DataSource ds = DruidDataSourceFactory.createDataSource(pro);
        Connection conn = ds.getConnection();

    }

}

2、利用DBUtil实现数据的增删改查

构造器QueryRunner提供了增删改查的方法,不需要在自编码;

public class queryrunTest {
    public void testInsert() throws Exception{

        QueryRunner runner = new QueryRunner();
        Connection conn = JDBCUtils.getConnection3;
        String sql = "insert into customers(name,email,birth) values (?,?,?)";
        //单独的插入一条数据,不许要传入连接,如果是一个事务中的一条操作,需要传入连接
        int count = runner.update(conn,sql,"何成飞","he@123.com","1992-09-23");
        System.out.println("添加了" + count + "条记录");
        JDBCUtils.closeResource(conn,null);
    }
}

标签:JDCB,pro,public,QueryRunner,void,class,连接,conn
来源: https://blog.csdn.net/weixin_45063957/article/details/117217829