首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
数据库
> 详细
JDBC链接数据库版本三,使用C3P0,使用jar文件两个
时间:
2016-06-21 15:34:48
阅读:
213
评论:
0
收藏:
0
[点我收藏+]
标签:
JdbcUtil类:
[java]
view plain
copy
package com.xiaohui.jdbc.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public
final
class JdbcUtil {
private
static ComboPooledDataSource dataSource;
static {
dataSource =
new ComboPooledDataSource();
}
// 取得链接
public
static Connection getMySqlConnection()
throws SQLException {
return dataSource.getConnection();
}
//
public
static DataSource getDataSource(){
return dataSource;
}
// 关闭链接
public
static
void close(Connection conn)
throws SQLException {
if (conn !=
null) {
try {
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
throw e;
}
}
}
public
static
void close(PreparedStatement pstate)
throws SQLException {
if(pstate!=
null){
pstate.close();
}
}
public
static
void close(ResultSet rs)
throws SQLException {
if(rs!=
null){
rs.close();
}
}
}
c3p0-config.xml
[html]
view plain
copy
<?
xml
version=
"1.0"
encoding=
"UTF-8"
?>
<
c3p0-config
>
<
default-config
>
<
property
name=
"driverClass"
>com.mysql.jdbc.Driver
</
property
>
<
property
name=
"user"
>root
</
property
>
<
property
name=
"password"
>root
</
property
>
<
property
name=
"jdbcUrl"
>jdbc:mysql://127.0.0.1:3306/mysql4
</
property
>
</
default-config
>
</
c3p0-config
>
分页的一个dao:
[java]
view plain
copy
package com.xiaohui.cusSys.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.xiaohui.cusSys.domain.Customer;
import com.xiaohui.cusSys.util.JdbcUtil;
public
class Dao {
private QueryRunner qr =
new QueryRunner(JdbcUtil.getDataSource());
// 根据id返回 Customer 对象
public Customer getCustomerById(
int id)
throws SQLException {
String sql =
"select * from customer where id = ?";
Customer cus = (Customer) qr.query(sql,
new BeanHandler(Customer.
class), id);
return cus;
}
// 分页返回
public List<Customer> getFyList(
int start,
int size)
throws SQLException {
List<Customer> list =
null;
String sql =
"select * from customer limit ?,?";
list = qr.query(sql,
new BeanListHandler(Customer.
class),
new Object[] {
start, size });
return list;
}
// 返回记录的总数目
public
int getAllRecordsCount()
throws SQLException {
String sql =
"select count(*) from customer";
Long temp = qr.query(sql,
new ScalarHandler());
return temp.intValue();
}
// 根据ID删除指定的记录
public
void deleteRecordById(
int id)
throws SQLException {
String sql =
"delete from customer where id = ?";
qr.update(sql, id);
}
// 根据id更新记录信息
public
void updateRecordById(Customer newCus)
throws SQLException {
String sql =
"update customer set name= ?,address= ?,tel= ?,mail= ?,birthday= ? where id= ?";
qr.update(
sql,
new Object[] { newCus.getName(), newCus.getAddress(),
newCus.getTel(), newCus.getMail(),
newCus.getBirthday(), newCus.getId() });
}
// 添加记录
public
void addRecord(Customer newCus)
throws SQLException {
String sql =
"insert into customer(name,address,tel,mail,birthday) values(?,?,?,?,?)";
qr.update(sql,
new Object[] { newCus.getName(), newCus.getAddress(),
newCus.getTel(), newCus.getMail(),
// //将java.util.Date 转换为 java.sql.Date
// new java.sql.Date( newCus.getBirthday().getTime())
newCus.getBirthday() });
}
}
jar文件:c3p0-0.9.1.2.jar (关键) mysql-connector-java-5.1.22-bin.jar(关键) 在dao中 使用到commons-dbutils-1.5.jar
顶
JDBC链接数据库版本三,使用C3P0,使用jar文件两个
标签:
原文地址:http://www.cnblogs.com/love540376/p/5603656.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
数据库进阶
2021-07-29
在 Oracle 数据库中执行 SQL 语句遇到特殊字符的转义方式
2021-07-28
Windows Logstash同步 Sqlserver 到Elasticsearch
2021-07-26
mysql数据库(11):恢复数据
2021-07-26
mysql数据库(9):常用查询的例子
2021-07-26
SQLAlchemy 多对多
2021-07-26
ClickHouse的JDBC连接
2021-07-26
Apache HBase 1.7.1 发布,分布式数据库
2021-07-26
数据库常用架构和同步工作原理
2021-07-26
MySQL数据库设计规范(仅供参考)
2021-07-26
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!