queryRunner和数据池
来源:互联网 发布:德温特专利数据库链接 编辑:程序博客网 时间:2024/04/30 09:00
1、 新建工程,导入mysql、c3p0、dbUtil的jar包(c3p0有2个)和c3p0的配置文件
2、 设置好配置文件连接数据库的相关信息
<?xmlversion="1.0"encoding="UTF-8"?>
<c3p0-config>
<named-configname="test">
<!-- 数据库用户名和密码 -->
<propertyname="user">root</property>
<propertyname="password">root</property>
<!-- 数据库路径和加载的驱动 -->
<propertyname="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<propertyname="driverClass">com.mysql.jdbc.Driver</property>
<!-- 数据池的相关设置 --> <property name="acquireIncrement">5</property>
<propertyname="initialPoolSize">10</property>
<propertyname="minPoolSize">5</property>
<propertyname="maxPoolSize">10</property>
<propertyname="maxStatements">200</property>
<propertyname="maxStatementsPerConnection">500</property>
</named-config>
</c3p0-config>
3、 装载dbUtil,实例一个ComboPooledDataSource,并创建一个获取连接的方法getConnection和一个获取连接资源的方法
public classJdbcs {
private static ComboPooledDataSource ds =null;
static {
ds = new ComboPooledDataSource("test");
}
public static ConnectiongetConnection() throwsSQLException {
return ds.getConnection();
}
public static DataSourcegetDataSourse(){
return ds;
}
}
4、 创建一个对数据库操纵的类Demo
public classDemo {
public void add(){
QueryRunnerqr=newQueryRunner(Jdbcs.getDataSourse());
Stringsql="insert into uservalues(?,?,?,?)";
Objectparams[]={"11","123","name01","school01"};
try {
qr.update(sql,params);
}catch(SQLException e) {
System.out.println("添加数据出错!");
}
}
public void delete(){
QueryRunnerqr=newQueryRunner(Jdbcs.getDataSourse());
Stringsql="delete from user where uid=?";
try {
qr.update(sql,"11");
}catch(SQLException e) {
System.out.println("删除数据出错!");
}
}
public void update(){
QueryRunnerqr=newQueryRunner(Jdbcs.getDataSourse());
Stringsql="update user set sname=? whereuid=?";
Objectparams[]={"zhangsan","11"};
try {
qr.update(sql,"11");
}catch(SQLException e) {
System.out.println("更新数据出错!");
}
}
public void select(){
QueryRunnerqr=newQueryRunner(Jdbcs.getDataSourse());
Stringsql="select sname from user whereuid=?";
try {
Useru=qr.query(sql, "11", new BeanHandler(User.class));
System.out.println(u.getSname());
}catch(SQLException e) {
System.out.println("更新数据出错!");
}
}
小伙伴们,赶快将这个方法加载到自己的项目中吧
- queryRunner和数据池
- QueryRunner
- QueryRunner
- QueryRunner
- queryRUNNER
- DBUtil用法之ResultSetHandler 和 QueryRunner总结
- jsp---DBUtil用法之ResultSetHandler 和 QueryRunner
- DBUtil用法之ResultSetHandler 和 QueryRunner总结
- DBUtiles中的简单使用(QueryRunner和ResultSetHandler的手动实现)
- jsp---DBUtil用法之ResultSetHandler 和 QueryRunner总结
- jsp---DBUtil用法之ResultSetHandler 和 QueryRunner总结
- JNDI技术简介和Apache—DBUtils框架(QueryRunner类)
- c3p0和QueryRunner的结合使用,让开发更加简便
- c3p0和QueryRunner的结合使用,让开发更加简便
- c3p0和QueryRunner的结合使用,让开发更加简便
- c3p0和QueryRunner的结合使用,让开发更加简便
- c3p0和QueryRunner的结合使用,让开发更加简便
- c3p0和QueryRunner的结合使用,让开发更加简便
- 使用 python matplotlib 画矩形
- 寻找二叉树中和为某一值的路径
- 【每周至少一篇 160811(上周补传)】泛型_Java
- Android按钮的淡入淡出
- 数字金额大写转换
- queryRunner和数据池
- 9个 SSH常用命令选项
- Android View框架总结(三)View工作原理
- 图的连通性问题
- uva140
- java实现一个学生的一张考试成绩单
- 虚拟键盘遮挡popwindow的控件
- centos命令行常用快捷键
- Android Studio SHA1码查看方法