package com.pb.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.pb.dao.DAO;
import com.pb.entity.RedAndBlueCardHead;
public class JdbcDao implements DAO {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@SuppressWarnings("unused")
@Override
public int Select() {
Connection conn = null;
PreparedStatement ps1 = null;
ResultSet rs = null;
int seq = 1;
try {
conn = dataSource.getConnection();
ComboPooledDataSource ds = (ComboPooledDataSource) dataSource;
String sql = "select REC_NUM from BHZT where TBLNAME = ?";
ps1 = (PreparedStatement) conn.prepareStatement(sql);
ps1.setString(1, "RedAndBlueCardHead");
rs = ps1.executeQuery();
while (rs.next()) {
seq = rs.getInt("REC_NUM"); // 取得数据库中的数据
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
release(conn, ps1, rs);
}
return seq;
}
@SuppressWarnings("resource")
@Override
public int GetSeq(String tableName) {
Connection conn = null;
PreparedStatement ps1 = null;
ResultSet rs = null;
int seq = 1;
try {
conn = dataSource.getConnection();
String sql = "update BHZT set REC_NUM = REC_NUM+1 where TBLNAME = ?";
ps1 = (PreparedStatement) conn.prepareStatement(sql);
ps1.setString(1, tableName);
if (ps1.executeUpdate() == 0) {
sql = "insert into BHZT (TBLNAME,REC_NUM) values(?,?)";
ps1 = (PreparedStatement) conn.prepareStatement(sql);
ps1.setString(1, tableName);
ps1.setInt(2, 1);
ps1.execute();
} else {
// System.out.println("=============select REC_NUM from BHZT where TBLNAME = ?======");
sql = "select REC_NUM from BHZT where TBLNAME = ?";
ps1 = (PreparedStatement) conn.prepareStatement(sql);
ps1.setString(1, tableName);
rs = ps1.executeQuery();
while (rs.next()) {
seq = rs.getInt("REC_NUM"); // 取得数据库中的数据
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
release(conn, ps1, rs);
}
return seq;
}
/*
* @SuppressWarnings("resource")
*
* @Override public int GetSeq(String tableName) { Connection conn = null;
* PreparedStatement ps1 = null; ResultSet rs = null; int seq = 1; try {
* conn = dataSource.getConnection(); conn.setAutoCommit(false); String sql
* = "update BHZT set REC_NUM = REC_NUM+1 where TBLNAME = ?"; ps1 =
* (PreparedStatement) conn.prepareStatement(sql); ps1.setString(1,
* tableName); if (ps1.executeUpdate() == 0) { sql =
* "insert into BHZT (TBLNAME,REC_NUM) values(?,?)"; ps1 =
* (PreparedStatement) conn.prepareStatement(sql); ps1.setString(1,
* tableName); ps1.setInt(2, 1); ps1.execute(); } else { //
* System.out.println
* ("=============select REC_NUM from BHZT where TBLNAME = ?======"); sql =
* "select REC_NUM from BHZT where TBLNAME = ?"; ps1 = (PreparedStatement)
* conn.prepareStatement(sql); ps1.setString(1, tableName); rs =
* ps1.executeQuery(); while (rs.next()) { seq = rs.getInt("REC_NUM"); //
* 取得数据库中的数据 } } conn.commit(); } catch (SQLException e) { try {
* conn.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch
* block e1.printStackTrace(); } e.printStackTrace(); } finally { try {
* conn.setAutoCommit(true); } catch (SQLException e) { e.printStackTrace();
*
* } release(conn, ps1, rs); } return seq; }
*/
@Override
public void SaveHead(RedAndBlueCardHead head, Connection conn,
PreparedStatement ps) throws SQLException {
head.OId = GetSeq("RedAndBlueCardHead");
String sql = "insert into RedAndBlueCardHead(OId,Status,Creator,CreateTime,operationType,BrandId, "
+ " RedGoodsId,CategoryId,SupplierId,GoodsId,ContractId,QRH,DictExpense,CXJFBH,PlanMoney,Continued,AClass,"
+ " BClass,Reason,ExpenseMoney,CaTegoryCode) "
+ " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; // 21个
ps = conn.prepareStatement(sql);
ps.setInt(1, head.OId);
ps.setInt(2, head.Status);
ps.setInt(3, head.Creator);
// ps.setDate(4, head.CreatorTime);
ps.setDate(4, new java.sql.Date(new java.util.Date().getTime()));
ps.setInt(5, head.OperationType);
ps.setInt(6, head.BrandId);
ps.setInt(7, head.RedGoodsId);
ps.setInt(8, head.CategoryId);
ps.setInt(9, head.SupplierId);
ps.setInt(10, head.GoodsId);
ps.setString(11, head.ContractId);
ps.setString(12, head.QRH);
ps.setInt(13, head.DictExpense);
ps.setString(14, head.CXJFBH);
ps.setDouble(15, head.PlanMoney);
ps.setInt(16, head.Continue);
ps.setInt(17, head.AClass);
ps.setInt(18, head.BClass);
ps.setString(19, head.Reason);
ps.setDouble(20, head.ExpenseMoney);
ps.setString(21, head.CaTegoryCode);
ps.execute();
}
@Override
public void SaveDetail(RedAndBlueCardHead head, Connection conn,
PreparedStatement ps) throws SQLException {
for (int i = 0; i < head.details.size(); i++) {
String sql = "insert into RedAndBlueCardDetail(OId,INX,Organization,Store,DQXSRW,BQFPE) "
+ "values(?,?,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setInt(1, head.OId);
ps.setInt(2, i + 1);
ps.setInt(3, head.details.get(i).Organization);
ps.setInt(4, head.details.get(i).Store);
ps.setDouble(5, head.details.get(i).DQXSRW);
ps.setDouble(6, head.details.get(i).BQFPE);
ps.execute();
}
}
@Override
public boolean SaveCard(RedAndBlueCardHead head) {
Connection conn = null;
PreparedStatement ps1 = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
SaveHead(head, conn, ps1);
SaveDetail(head, conn, ps1);
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
release(conn, ps1, rs);
}
}
@Override
public void release(Connection conn, Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close(); // throw new
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if (st != null) {
try {
st.close();
} catch (Exception e) {
e.printStackTrace();
}
st = null;
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
spring+c3p0.rar (70个子文件)
WebContent
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 1KB
classes
jdbc
Spring-jdbc.xml 412B
database
Spring-datasource.xml 2KB
log4j.properties 218B
com
pb
dao
DAO.class 495B
impl
JdbcDao.class 6KB
web
controller
Test.class 994B
LoginController.class 2KB
TestServlet.class 3KB
entity
Account.class 932B
RedAndBlueCardDetail.class 1KB
RedAndBlueCardHead.class 854B
utils
JsonUtils.class 1KB
applicationContext.xml 1KB
lib
spring-webmvc.jar 393KB
log4j-1.2.14.jar 359KB
spring-context-support.jar 95KB
commons-logging-1.0.4.jar 37KB
spring.jar 2.81MB
spring-agent.jar 5KB
commons-pool2-2.0.jar 105KB
spring-aspects.jar 20KB
standard.jar 384KB
spring-beans.jar 477KB
spring-orm.jar 369KB
spring-core.jar 279KB
spring-context.jar 466KB
servlet-api.jar 173KB
spring-webmvc-struts.jar 36KB
commons-dbcp-1.4.jar 157KB
spring-tx.jar 226KB
jstl.jar 20KB
spring-webmvc-portlet.jar 146KB
spring-jdbc-4.2.5.RELEASE-sources.jar 433KB
spring-tomcat-weaver.jar 9KB
commons-collections-3.2.jar 558KB
spring-jms.jar 191KB
spring-aop.jar 318KB
spring-jdbc.jar 328KB
gson-2.3.1.jar 206KB
spring-test.jar 177KB
mysql-connector-java-5.1.22-bin.jar 813KB
spring-jdbc-4.2.5.RELEASE.jar 414KB
c3p0-0.9.0.4.jar 479KB
spring-web.jar 190KB
login.jsp 579B
index.jsp 386B
showAccount.jsp 466B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 305B
org.eclipse.wst.common.component 481B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 503B
org.eclipse.jdt.core.prefs 598B
src
jdbc
Spring-jdbc.xml 412B
database
Spring-datasource.xml 2KB
log4j.properties 218B
com
pb
dao
DAO.java 697B
impl
JdbcDao.java 6KB
web
controller
LoginController.java 2KB
Test.java 516B
TestServlet.java 2KB
entity
Account.java 515B
RedAndBlueCardHead.java 797B
RedAndBlueCardDetail.java 751B
utils
JsonUtils.java 938B
applicationContext.xml 1KB
.project 1KB
.classpath 1KB
build
共 70 条
- 1
资源评论
lwj605
- 粉丝: 1
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功