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);
}
}