package com.stevendatabase;
import java.io.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.logging.*;
public class DatabaseAccessImpl implements DatabaseAccessInterface {
private static Log log = LogFactory.getLog(DatabaseAccessImpl.class);
//可写入插入,删除,修改
public void executeSQL(String sqlStatement) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBConnectionManager.getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement(sqlStatement);
statement.execute();
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//return;
}
//设置预编译--增删改--
public void executeSQL(String sqlStatement, Object parameters[])
throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBConnectionManager.getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement(sqlStatement);
// 获取执行数据
if (parameters != null && parameters.length > 0) {
for (int i = 0; i < parameters.length; i++) {
statement.setObject(i + 1, parameters[i]);
}
}
statement.execute();
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
System.out.println("这个是 executeSQL(String sqlStatement, Object parameters[])");
//return;
}
public void executeSQL(String sqlStatement, List parameters)
throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBConnectionManager.getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement(sqlStatement);
// 获取执行数据
int listsize = parameters.size();
for (int i = 0; i < listsize; i++) {
statement.setObject(i + 1, parameters.get(i));
}
statement.execute();
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
System.out.println("这个是 executeSQL(String sqlStatement, Object parameters[])");
//return;
}
public void executeSQL(String[] sqlStatement) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
if (sqlStatement == null || sqlStatement.length == 0) {
throw new SQLException();
}
try {
connection = DBConnectionManager.getConnection();
connection.setAutoCommit(false);
int slength = sqlStatement.length;// 事务中sql语句的个数
for (int i = 0; i < slength; i++) {
statement = connection.prepareStatement(sqlStatement[i]);
statement.execute();
}
connection.commit();
} catch (SQLException ex) {
connection.rollback();
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
connection.rollback();
ex.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//return;
}
public void executeSQL(String[] sqlStatement, List parameters)
throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
if (sqlStatement == null || sqlStatement.length == 0) {
throw new SQLException();
}
try {
connection = DBConnectionManager.getConnection();
connection.setAutoCommit(false);
int slength = sqlStatement.length;// 事务中sql语句的个数
for (int i = 0; i < slength; i++) {
statement = connection.prepareStatement(sqlStatement[i]);
if (parameters != null) {
Object[] pm = (Object[]) parameters.get(i);
// 获取执行数据
int pmsize = 0;// 每条sql数据对应的参数个数
if (pm != null) {
pmsize = pm.length;
}
for (int j = 0; j < pmsize; j++) {
statement.setObject(j + 1, pm[j]);
}
}
statement.execute();
}
connection.commit();
} catch (SQLException ex) {
connection.rollback();
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
connection.rollback();
ex.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//return;
}
public Vector executeQuerySQL(String sqlStatement) throws SQLException {
Vector resultVector = new Vector();
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
connection = DBConnectionManager.getConnection(); // 获取数据库连接对象
connection.setAutoCommit(false);
statement = connection.prepareStatement(sqlStatement); // 获取statement对象
statement.execute();
rs = statement.getResultSet(); // 获取查询记录集
HashMap rowItem;
for (; rs.next(); resultVector.add(rowItem)) {
rowItem = new HashMap();
int columnCount = rs.getMetaData().getColumnCount();
for (int i = 1; i <= columnCount; i++) {
Object cell = rs.getObject(i);
if (cell == null) {
cell = "";
} else if (cell instanceof Blob) {
try {
InputStreamReader in = new InputStreamReader(
((Blob) cell).getBinaryStream());
char bufferString[] = new char[5000];
int readCharCount = in.read(bufferString);
StringBuffer bolbStringBuffer = new StringBuffer("");
for (; readCharCount == 5000; readCharCount = in
.read(bufferString)) {
bolbStringBuffer.append(bufferString, 0,
readCharCount);
}
if (readCharCount != -1) {
bolbStringBuffer.append(bufferString, 0,
readCharCount);
}
cell = bolbStringBuffer.toString();
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
rowItem.put(rs.getMetaData().getColumnName(i), cell);
}
}
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return resultVector;
}
public Vector executeQuerySQL(String sqlStatement, Object parameters[])
throws SQLException {
Vector resultVector = new Vector();
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
connection = DBConnectionManager.getConnection(); // 获取数据库连接对象
connection.setAutoCommit(false);
statement = connection.prepareStatement(sqlStatement); // 获取statement对象
// 获取查询参数
if (parameters != null && parameters.length > 0) {
for (int i = 0; i < parameters.length; i++) {
statement.setObject(i + 1, parameters[i]);
}
}
statement.execute();
rs = statement.getResultSet(); // 获取查询记录集
HashMa
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
















收起资源包目录






































共 24 条
- 1
资源评论

- fengyuleidian32015-05-21用于入门,不错

jcy2sls
- 粉丝: 10
- 资源: 8
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
