package com.zgcy.dao;
import java.sql.Connection;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.Collection;
import java.util.HashMap;
import java.io.Writer;
/**
* 实现数据库通讯
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class DBAccess {
public final static String DS_MAIN = "WorkFlowDS";
public final static String DS_TAX = "TaxDS";
public final static String DS_SYSMNG = "test";
public final static String DS_WEB = "Web2006DS";
public DBAccess() {
}
/*获取默认ds连接
*/
public static Connection getConnection() throws Exception {
Connection conn = null;
try {
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup(
DBAccess.getDataSource(DS_SYSMNG));
conn = ds.getConnection();
} catch (NamingException ex) {
ex.printStackTrace();
throw new Exception("无法获得数据库联接");
} catch (SQLException ex1) {
ex1.printStackTrace();
throw new Exception("无法获得数据库联接");
}
return conn;
}
/*
* 执行一个查询,返回RecordSet
*/
public static ResultSet Exec(String strSQL, Connection cnct) {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = cnct.createStatement();
boolean bResult = stmt.execute(strSQL);
return stmt.getResultSet();
} catch (SQLException e) {
return null;
}
}
/**
* 获取指定ds连接
* Creation date: (2004-11-13 15:05:33)
* @return java.sql.Connection
* @param strDataSource java.lang.String
* @exception com.towery.util.db.DBException The exception description.
*/
public static Connection getConnection(String strDataSource) throws Exception
{
Connection conn = null;
try {
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup( DBAccess.getDataSource(strDataSource));
conn = ds.getConnection();
} catch (NamingException ex) {
ex.printStackTrace();
throw new Exception("无法获得数据库联接");
} catch (SQLException ex1) {
ex1.printStackTrace();
throw new Exception("无法获得数据库联接");
}
return conn;
}
/**
* 获取指定ds对应的数据
* Creation date: (2004-11-13 15:18:14)
* @return java.lang.String
* @param strDSName java.lang.String
*/
public static String getDataSource(String strDSName) {
return strDSName;
}
/**
* 获取SQL中某些列strItems组成的多维数组
* Creation date: (2004-11-13 15:20:11)
* @return java.lang.String[][]
* @param cnct java.sql.Connection
* @param strSQL java.lang.String
* @param strItems java.lang.String[]
*/
public static Collection sql(String strSQL , Connection cnct) throws
Exception {
Vector vctRtn = null;
Statement stmt = null;
ResultSet rs = null;
// System.out.println("执行SQL"+strSQL);
try {
stmt = cnct.createStatement();
boolean bResult = stmt.execute(strSQL);
vctRtn = new Vector();
if (bResult) {
rs = stmt.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
Vector vctColName = new Vector();
for (int i = 0; i < rsmd.getColumnCount(); i++) {
String strColName = rsmd.getColumnName(i + 1);
vctColName.addElement(strColName);
}
Map map;
for (; rs.next(); vctRtn.addElement(map)) {
map = new HashMap();
for (int i = 0; i < vctColName.size(); i++) {
Object value = rs.getObject(vctColName.get(i).toString());
if (value != null && value instanceof java.sql.Timestamp) {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
value = sdf.format((Timestamp)value);
}
map.put(vctColName.get(i) , value);
}
}
}
return vctRtn;
} catch (SQLException e) {
if (e.getErrorCode() == 0) {
return null;
} else {
throw new Exception(
"在执行Sql < " + strSQL + " > 时出现异常 : < " + e.getMessage()
+ ">");
}
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 执行批处理
* @param sql String[]
* @param connection Connection
* @throws Exception
*/
public static void execute(String[] sql , Connection connection) throws
Exception {
if (sql == null || sql.length < 1) {
return;
}
Statement st = null;
try {
if (connection.isClosed()) {
throw new Exception("链接已关闭!");
}
connection.setAutoCommit(false);
st = connection.createStatement();
for (int i = 0; i < sql.length; i++) {
st.addBatch(sql[i]);
}
int[] flag = st.executeBatch();
for (int i = 0; i < flag.length; i++) {
if (flag[i] == Statement.EXECUTE_FAILED) {
connection.rollback();
throw new Exception("第[" + i + "] 条sql出错,sql = " + sql[i]);
}
}
connection.commit();
} catch (SQLException se) {
se.printStackTrace();
try {
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
throw new Exception(se.getMessage());
} finally {
try {
if (st != null) {
st.close();
}
connection.setAutoCommit(true);
} catch (SQLException se) {
se.printStackTrace();
throw new Exception(se.getMessage());
}
}
}
public static boolean execute(String sql) throws Exception {
Connection conn = getConnection(DBAccess.DS_SYSMNG);
try {
return execute(sql , conn);
} catch (Exception ex) {
throw ex;
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static boolean execute(String sql , Connection conn) throws
Exception {
Statement st = null;
try {
st = conn.createStatement();
return st.execute(sql);
} catch (SQLException ex) {
throw new Exception(ex.getMessage());
} finally {
try {
if (st != null) {
没有合适的资源?快使用搜索试试~ 我知道了~
SSH增删改查+分页+图片上传
共386个文件
svn-base:188个
jar:59个
format:25个
5星 · 超过95%的资源 需积分: 9 40 下载量 116 浏览量
2012-12-28
16:46:26
上传
评论 1
收藏 33.62MB RAR 举报
温馨提示
Struts1+Spring+Hibernate增删改查+分页+图片上传+jar包齐全+数据库
资源推荐
资源详情
资源评论
收起资源包目录
SSH增删改查+分页+图片上传 (386个子文件)
DBAccess.class 10KB
ListsAction.class 4KB
UsersAction.class 4KB
Lists.class 3KB
UsersDAO.class 2KB
MD5.class 2KB
LoginFilter.class 2KB
DB.class 2KB
UsersService.class 2KB
ListsDAO.class 1KB
ListsService.class 1KB
CharacterFilter.class 1KB
Users.class 1KB
Str.class 1KB
PathServlet.class 950B
Path.class 497B
.classpath 6KB
org.eclipse.wst.jsdt.ui.superType.container 49B
uploadify.css 2KB
jquery.alerts.css 1KB
imgareaselect-default.css 775B
entries 10KB
entries 2KB
entries 1KB
entries 1KB
entries 1KB
entries 1KB
entries 953B
entries 953B
entries 923B
entries 796B
entries 796B
entries 698B
entries 517B
entries 517B
entries 516B
entries 516B
entries 352B
entries 329B
entries 265B
entries 265B
entries 210B
entries 210B
entries 206B
entries 205B
entries 205B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
hibernate3.jar 2.31MB
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar 1.57MB
jbosscache-core-3.1.0.GA.jar 1.1MB
mysql-connector-java-5.1.22-bin.jar 813KB
jboss-cache-1.4.1.GA.jar 690KB
org.springframework.context-3.0.1.RELEASE-A.jar 642KB
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar 603KB
c3p0-0.9.1.jar 594KB
javassist-3.9.0.GA.jar 583KB
sql2005jdbc.jar 570KB
commons-collections-3.1.jar 546KB
org.springframework.beans-3.0.1.RELEASE-A.jar 535KB
org.springframework.web.struts-3.0.1.RELEASE-A.jar 524KB
proxool-0.8.3.jar 465KB
antlr-2.7.6.jar 433KB
org.springframework.web.servlet-3.0.1.RELEASE-A.jar 389KB
com.springsource.org.apache.log4j-1.2.15.jar 387KB
org.springframework.jdbc-3.0.1.RELEASE-A.jar 369KB
log4j-1.2.14.jar 359KB
org.springframework.web-3.0.1.RELEASE-A.jar 352KB
org.springframework.core-3.0.1.RELEASE-A.jar 347KB
org.springframework.orm-3.0.1.RELEASE-A.jar 324KB
com.springsource.net.sf.cglib-2.2.0.jar 319KB
org.springframework.aop-3.0.1.RELEASE-A.jar 313KB
dom4j-1.6.1.jar 307KB
com.springsource.org.apache.commons.httpclient-3.1.0.jar 301KB
hibernate-annotations.jar 273KB
cglib-2.2.jar 272KB
com.springsource.org.codehaus.jackson.mapper-1.0.0.jar 264KB
共 386 条
- 1
- 2
- 3
- 4
资源评论
- junoppa2013-01-22还可以,能运行
xsl8918
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- IMG_20240425_120538.jpg
- My Complete Genome_6k Base-Pairs of Phenotype SNPs_Complete Raw Data.zip
- qt 的mqtt测试demo
- 移动应用开发教程-zip.zip
- mosquitto-2.018-install-windows-x64
- FTPServer FTP 服务器,绿色免安装,单文件
- 梦畅语音点名软件,上课点名
- 利用ADNI数据集和标签,在tensorflow框架上使用tensorlayer接口,通过架构u-net实现海马体的分割
- Kutools for Word v9.0 office word 插件
- 修复Windows 10 LTSC 2021资源占用率高
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功