1.jdbc重复代码多,所以用DBUtils
2.BasicDataSource
- 遇到数据库问题,打印出exception
import java.sql.Connection;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
public class DBUtilsText {
public static Connection con=Propertool.getConnection();
public static void main(String[] args) throws Exception {
// insert();
// update();
delete();
}
public static void delete() throws Exception
{
QueryRunner qr=new QueryRunner();
String sql="delete from product where id=?";
int a=qr.update(con,sql,10);
System.out.println(a);
DbUtils.closeQuietly(con);
}
public static void update() throws Exception {
QueryRunner qr=new QueryRunner();
String sql="UPDATE product SET pname=?,price=? WHERE id=?;";
Object[] params={"zhaosi",22222,10};
int a=qr.update(con,sql,params);
System.out.println(a);
DbUtils.closeQuietly(con);
}
public static void insert() throws Exception {
QueryRunner qr=new QueryRunner();
String sql="INSERT INTO product (pname,price)VALUES(?,?);";
Object[] params={"张三",11111};
int a=qr.update(con,sql,params);
System.out.println(a);
DbUtils.closeQuietly(con);
}
}
updete()返回值int所以不能展示结果
3.javabean封装数据,resultsethandle结果处理接口
- 一共八种处理方式
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import cn.itcast.domain.Sort;
import cn.itcast.jdbcutil.JDBCUtilsConfig;
/*
* QueryRunner数据查询操作:
* 调用QueryRunner类方法query(Connection con,String sql,ResultSetHandler r, Object..params)
* ResultSetHandler r 结果集的处理方式,传递ResultSetHandler接口实现类
* Object..params SQL语句中的?占位符
* 注意: query方法返回值,返回的是T 泛型, 具体返回值类型,跟随结果集处理方式变化
*/
public class QueryRunnerDemo1 {
private static Connection con = JDBCUtilsConfig.getConnection();
public static void main(String[] args) throws SQLException{
// arrayHandler();
// arrayListHandler();
// beanHandler();
// beanListHander();
// columnListHandler();
// scalarHandler();
// mapHandler();
// mapListHandler();
}
/*
* 结果集第八种处理方法,MapListHandler
* 将结果集每一行存储到Map集合,键:列名,值:数据
* Map集合过多,存储到List集合
*/
public static void mapListHandler()throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort";
//调用方法query,传递结果集实现类MapListHandler
//返回值List集合, 存储的是Map集合
List
4.连接池,链接和释放都会占用大量资源,创建一些链接备用,不用就放回池中
- 用户不会直接链接数据库,经过网页,Java代码,最后才是数据库
- sum规定一个接口datasource
- 常用连接池,dbcp,c3p0
- 使用dbcp需要导入两个包commons-dbcp-1.4,commons-pool-1.5.6
import java.util.ArrayList;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
public class BaseDataSourceTest {
private static BasicDataSource base=new BasicDataSource();
private static QueryRunner qr=new QueryRunner(getDataSource());
static
{
base.setDriverClassName("com.mysql.jdbc.Driver");
base.setUsername("root");
base.setPassword("0616");
base.setUrl("jdbc:mysql://localhost:3306/myabc");
base.setInitialSize(10);//初始链接
base.setMaxActive(8);//最大连接数
base.setMaxIdle(5);//最大空闲数
base.setMinIdle(1);
}
public static void main(String[] args) {
try {
String sql="select * from product";
Object[] ob=qr.query(sql, new ArrayHandler());
for (Object o :ob)
{
System.out.println(o);
}
} catch (Exception e) {
System.out.println(e);
throw new RuntimeException("连接失败");
}
}
public static DataSource getDataSource() {
return base;
}
}