本文主要内容有C3P0数据库连接池,dbutils的使用,元数据的应用
在对数据库进行增删改查时,使用数据库连接池可以有效的提高效率,节省资源,C3P0是Apache组织提供的一个有效方式
C3P0的XML配置文件,文件名必须c3p0config.xml,路径必须与类相同
<?xml version="1.0" encoding="utf-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///estore</property>
<property name="user">estore</property>
<property name="password">estore</property>
</default-config>
</c3p0-config>
以上分别为设置JDBC,数据库名称,用户名和密码,注意文件名是固定的,必须导入C3P0的jar包
数据库连接类的书写
public class DaoUtils {
private static DataSource source = new ComboPooledDataSource();
private DaoUtils() {
}
public static DataSource getSource(){
return source;
}
public static Connection getConn(){
try {
return source.getConnection();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
以上提供了两个静态方法,分别返回数据源和连接
利用DBUtils对数据库的增删改查,需要导入Commons-dbutils.jar
插入数据
public class OrderDaoImpl implements OrderDao {
@Override
public void addOrder(Order order) {
// TODO 自动生成的方法存根
String sql = "insert into orders values (?,?,?,?,null,?)";
try {
QueryRunner runner=new QueryRunner(DaoUtils .getSource());
runner.update(sql,order.getId(),order.getMoney(),order.getReceiverinfo(),order.getPaystate(),order.getUser_id());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException(e);
}
}
删除数据
public void delOrderItem(String id) {
// TODO 自动生成的方法存根
String sql="delete from orderitem where order_id = ?";
try {
QueryRunner runner = new QueryRunner(DaoUtils .getSource());
runner.update(sql,id);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException(e);
}
}
更新修改数据
public void updateState(int id) {
// TODO 自动生成的方法存根
String sql = "update users set state = 1 where id=?";
try{
QueryRunner runner = new QueryRunner(DaoUtils .getSource());
runner.update(sql,id);
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
查找数据,查找数据可以分为查找单个数据和查找列表数据,其中分别用BeanHandler接口与BeanListHandler接口实现
BeanHandler
public Order findOrderById(String p2_Order) {
// TODO 自动生成的方法存根
String sql = "select * from orders where id = ?";
try{
QueryRunner runner = new QueryRunner(DaoUtils .getSource());
return runner.query(sql, new BeanHandler<Order>(Order.class),p2_Order);
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
BeanListHandler
public List<SaleInfo> saleList() {
// TODO 自动生成的方法存根
String sql =
" select products.id prod_id,products.name prod_name,sum(orderitem.buynum ) sale_num"+
" from orders ,orderitem ,products "+
" where "+
" orders.id=orderitem.order_id "+
" and "+
" orderitem.product_id=products.id"+
" and orders.paystate = 1"+
" group by products.id"+
" order by sale_num desc";
try{
QueryRunner runner = new QueryRunner(DaoUtils .getSource());
return runner.query(sql, new BeanListHandler<SaleInfo>(SaleInfo.class));
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
元数据
当在JSP与Servlet中传递的参数过多时,元数据配合javabean可以有效的简化书写
//封装数据较验数据
User user=new User();
BeanUtils.populate(user, request.getParameterMap());
user.setPassword(MD5Utils.md5(user.getPassword()));
需要导入commons-beanutils.jar
javaweb数据库的基本操作完成