Jdbc - c3p0工具

Jdbc - c3p0工具,第1张

Jdbc - c3p0工具
package 一些常用的工具类示例;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class c3p0数据链接池的使用 extends 映射的使用{
	
	public void query(HttpServletRequest request,HttpServletResponse response) {
		try {
				//链接 从c3p0-config.xml找配置 
				//new无参的构造方法加载默认配置
				//new 	ComboPooledDataSource("join");
				ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();
				// *** 作
				QueryRunner queryRunner=new QueryRunner(comboPooledDataSource);
				
				//增
				String insert="insert into user values(?,?,?)";
				queryRunner.update(insert,"user","268268","@qq.com");
				//删
				String delete="delete from user where name=user";
				queryRunner.update(delete,"user");
				//改
				String update="update user set name=?,password=? where name=?";
				Object[] user = {"client",123123,"user"};
				queryRunner.update(update,user);
				
				//查
				
				String query="select * from fresh where name=?";
				users users=queryRunner.query(query,new BeanHandler(users.class),"missed");
				
				
				String selectList="select * from fresh"; //查询全部数据
				List list = queryRunner.query(selectList, new BeanListHandler(users.class));
				
				 
				String countall="select count(*) from user";
				int count=queryRunner.query(countall,new ScalarHandler<>());
				
				
				String map="select * from user where name=?";
				Map maps=queryRunner.query(map,new MapHandler(),"user"); 
				
				
				 String sql = "select * from product where pid = ?;";
				 Object[] obj = queryRunner.query(sql, new ArrayHandler(), 12);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5438117.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存