package com.peng.dao;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.peng.db.C3P0Util;
import com.peng.db.DBCPUtil;
import com.peng.db.DBUtil;
import com.peng.model.Account;
public class AccountDao {
/**
* 增
*
* @param account
* @throws SQLException
*/
public void insert(Account account) throws SQLException {
Connection conn = DBUtil.getConn();
String sql = "INSERT INTO account_info (account,amount) VALUES (?,?); ";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setString(1, account.getAccount());
ptmt.setDouble(2, account.getAmount());
ptmt.execute();
}
/**
* 改
*
* @param account
* @throws SQLException
*/
public void update(Account account) throws SQLException {
Connection conn = DBUtil.getConn();
String sql = "UPDATE account_info SET account=?,amount=? WHERE id =?; ";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setString(1, account.getAccount());
ptmt.setDouble(2, account.getAmount());
ptmt.setInt(3, account.getId());
ptmt.execute();
}
/**
* 删
*
* @param account
* @throws SQLException
*/
public void delete(Account account) throws SQLException {
Connection conn = DBUtil.getConn();
String sql = "delete from account_info WHERE id =?; ";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setInt(1, account.getId());
ptmt.execute();
}
/**
* 查 根据id查找数据
* @param id
* @return
* @throws SQLException
*/
public List<Account> query(Integer id) throws SQLException {
Connection conn = DBUtil.getConn();
List<Account> list = new ArrayList<Account>();
Account account = null;
String sql = "select * from account_info WHERE id =?; ";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setInt(1, id);
ResultSet rs = ptmt.executeQuery();
while (rs.next()) {
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd
// hh:mm:ss");
// String date = sdf.format(rs.getDate("create_at"));
// System.out.println(rs.getString("account")+"
// "+rs.getDouble("amount")+" "
// +rs.getTimestamp("create_at"));
account = new Account();
account.setAccount(rs.getString("account"));
account.setAmount(rs.getDouble("amount"));
account.setId(rs.getInt("id"));
// getTimestamp能得到时分秒的时间数据
account.setCreateAt(rs.getTimestamp("create_at"));
list.add(account);
}
return list;
}
/**
* 查 根据用户名匹配查询
* @param account
* @return
* @throws SQLException
*/
public List<Account> query(Account account) throws SQLException{
List<Account> list = new ArrayList<>();
Connection conn = DBUtil.getConn();
StringBuffer sb = new StringBuffer();
sb.append("select * from account_info ");
sb.append(" where account like ?");
PreparedStatement ptmt = conn.prepareStatement(sb.toString());
ptmt.setString(1, "%"+account.getAccount()+"%");
ResultSet rs = ptmt.executeQuery();
while(rs.next()){
account = new Account();
account.setAccount(rs.getString("account"));
account.setAmount(rs.getDouble("amount"));
account.setId(rs.getInt("id"));
// getTimestamp能得到时分秒的时间数据
account.setCreateAt(rs.getTimestamp("create_at"));
list.add(account);
}
return list;
}
public Account get(Integer id) throws SQLException {
Account a = null;
Connection conn = DBUtil.getConn();
String sql = " select * from account_info " + " where id =? ";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setInt(1, id);
ResultSet rs = ptmt.executeQuery();
while (rs.next()) {
a = new Account();
a.setAccount(rs.getString("account"));
a.setAmount(rs.getDouble("amount"));
a.setId(rs.getInt("id"));
// getTimestamp能得到时分秒的时间数据
a.setCreateAt(rs.getTimestamp("create_at"));
}
return a;
}
/**
* 查 根据id值查询信息
* 利用连接池连接数据库
* @param id
* @return
* @throws SQLException
*/
public Account getByDbcp(Integer id) throws SQLException {
Account a = null;
DBCPUtil dbcp = new DBCPUtil();
Connection conn = dbcp.getConn();
String sql = " select * from account_info " + " where id =? ";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setInt(1, id);
ResultSet rs = ptmt.executeQuery();
while (rs.next()) {
a = new Account();
a.setAccount(rs.getString("account"));
a.setAmount(rs.getDouble("amount"));
a.setId(rs.getInt("id"));
// getTimestamp能得到时分秒的时间数据
a.setCreateAt(rs.getTimestamp("create_at"));
}
return a;
}
/**
* 查 利用c3p0连接池连接数据库
* @param id
* @return
* @throws SQLException
* @throws PropertyVetoException
*/
public Account getByc3p0(Integer id) throws SQLException, PropertyVetoException {
Account a = null;
C3P0Util c3p0 = new C3P0Util();
Connection conn = c3p0.getConnection();
String sql = " select * from account_info " + " where id =? ";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setInt(1, id);
ResultSet rs = ptmt.executeQuery();
while (rs.next()) {
a = new Account();
a.setAccount(rs.getString("account"));
a.setAmount(rs.getDouble("amount"));
a.setId(rs.getInt("id"));
// getTimestamp能得到时分秒的时间数据
a.setCreateAt(rs.getTimestamp("create_at"));
}
return a;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
JDBC事务管理(添加dbcp和c3p0测试.zip
共41个文件
class:13个
java:13个
jar:6个
需积分: 16 11 下载量 54 浏览量
2016-10-08
16:12:59
上传
评论
收藏 2.18MB ZIP 举报
温馨提示
在 java客户端对数据库增删改查的基础上,实现事务管理,并增加连接流,dbcp和c3p0.
资源推荐
资源详情
资源评论
收起资源包目录
JDBC事务管理(添加dbcp和c3p0测试).zip (41个子文件)
jdbc_tanscation
.project 391B
bin
c3p0.properties 2KB
c3p0-config.xml 691B
com
peng
dao
TransInfoDao.class 4KB
AccountDao.class 5KB
test
TestDao.class 1KB
TestC3p0.class 2KB
TestDbcp.class 1KB
TestDBUtil.class 2KB
action
TransAction.class 1KB
db
DBUtil.class 1KB
C3P0Util.class 1003B
DBCPUtil.class 3KB
model
Account.class 2KB
TransInfo.class 2KB
service
TransService.class 2KB
dbcp.properties 1KB
resources
c3p0.properties 2KB
c3p0-config.xml 691B
dbcp.properties 1KB
.settings
org.eclipse.jdt.core.prefs 598B
src
com
peng
dao
AccountDao.java 5KB
TransInfoDao.java 3KB
test
TestDao.java 2KB
TestDBUtil.java 909B
TestC3p0.java 1KB
TestDbcp.java 935B
action
TransAction.java 809B
db
C3P0Util.java 711B
DBCPUtil.java 3KB
DBUtil.java 791B
model
TransInfo.java 2KB
Account.java 835B
service
TransService.java 2KB
.classpath 758B
lib
c3p0-0.9.5.2.jar 486KB
commons-logging-1.2.jar 60KB
commons-dbcp2-2.1.1.jar 170KB
mysql-connector-java-5.1.39-bin.jar 966KB
mchange-commons-java-0.2.11.jar 592KB
commons-pool2-2.4.2.jar 109KB
共 41 条
- 1
资源评论
伍Wu哈Ha
- 粉丝: 468
- 资源: 40
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功