package com.wang.yjs.business.util;
import java.sql.*;
import java.util.*;
import javax.servlet.jsp.jstl.sql.*;
/**
* 通用的JDBC資料庫訪問類
* @author
* @version 1.0
* 2009年1月12
*/
public class DB {
private Connection conn;
private String sqlValue;
private List values;
/**
* 設定串連類
* @param conn 資料庫連接
*/
public void setConnection(Connection conn){
this.conn = conn;
}
/**
* 設定SQL語句
* @param values SQL語句
*/
public void setSqlValues(String sqlValue){
this.sqlValue = sqlValue;
}
/**
* 設定SQL語句的參數
* @param values SQL語句的參數
*/
public void setValues(List values){
this.values = values;
}
/**
* 執行查詢
* @return Result 資料結果集
* @throws SQLException
*/
public Result executeQuery() throws SQLException {
Result result = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
Statement stmt = null;
try{
if(values != null && values.size() > 0){
pstmt = conn.prepareStatement(sqlValue);
setValues(pstmt, values);
rs = pstmt.executeQuery();
}else{
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlValue);
}
result = ResultSupport.toResult(rs);
}finally{
closeRs(rs);
closeStmt(stmt);
closePstmt(pstmt);
sqlValue = null;
values = null;
}
return result;
}
/**
* 執行update語句
* @return 執行影響行數
* @throws SQLException
*/
public int executeUpdate() throws SQLException {
int noOfRows = 0;
PreparedStatement pstmt = null;
Statement stmt = null;
try{
if(values != null && values.size() > 0){
pstmt = conn.prepareStatement(sqlValue);
setValues(pstmt, values);
noOfRows = pstmt.executeUpdate();
}else{
stmt = conn.createStatement();
noOfRows = stmt.executeUpdate(sqlValue);
}
}finally{
closeStmt(stmt);
closePstmt(pstmt);
closeConn();
sqlValue = null;
values = null;
}
return noOfRows;
}
/**
* 設定語句的參數
* @param PreparedStatement 對象
* @param values 參數列表
* @throws SQLException
*/
private void setValues(PreparedStatement pstmt, List values) throws SQLException {
for(int i = 0; i < values.size(); i++){
Object v = values.get(i);
pstmt.setObject(i+1, v);
}
}
/**
* 關閉資料庫連接
*/
public void closeConn() {
try {
if(this.conn != null) {
this.conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 關閉 Statement
* @param stmt
*/
private void closeStmt(Statement stmt) {
try {
if(stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 關閉 PreparedStatement
* @param pstmt
*/
private void closePstmt(PreparedStatement pstmt) {
if(pstmt != null){
try{
pstmt.close();
}catch(SQLException e){
//TODO
}
}
}
/**
* 關閉 ResultSet
* @param rs
*/
private void closeRs(ResultSet rs) {
try {
if(rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
測試通用DBHelper類
package com.wang.owbHome.business.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;
import javax.servlet.jsp.jstl.sql.Result;
import junit.framework.TestCase;
public class DBTest extends TestCase {
Connection conn = null;
protected void setUp() throws Exception {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;" +
"databaseName=QQDB;user=sa;password=123456a;");
}
/**
* 測試DB無參的查詢
*/
public void testDB_executeQuery(){
DB db = new DB();
db.setConnection(conn);
String sql1 = "select * from QQUser";
db.setSqlValues(sql1);
try {
Result rs = db.executeQuery();
if(rs == null || rs.getRowCount() == 0){
System.out.println("沒有結果");
}else{
System.out.println("共有" + rs.getRowCount() +"個結果!");
}
db.closeConn();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 測試DB有參的查詢
*/
public void testDB_executeQuery_Param(){
DB db = new DB();
db.setConnection(conn);
StringBuffer sql1 = new StringBuffer();
sql1.append("select * from QQUser where OnLine = ?");
System.out.println("sql1:" + sql1.toString());
db.setSqlValues(sql1.toString());
List values = new ArrayList();
values.add(new Integer(1));
db.setValues(values);
try {
Result rs = db.executeQuery();
if(rs == null || rs.getRowCount() == 0){
System.out.println("沒有結果");
}else{
System.out.println("共有" + rs.getRowCount() +"個結果!");
}
db.closeConn();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 測試DB無參的修改
*/
public void testDB_executeUpdate(){
DB db = new DB();
db.setConnection(conn);
String sql1 = "update QQUser set OnLine = 0 where ParentId = 1 ";
db.setSqlValues(sql1);
try {
int rs = db.executeUpdate();
System.out.println("共有資料" + rs +"修改!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 測試DB有參的修改
*/
public void testDB_executeUpdate_param(){
DB db = new DB();
db.setConnection(conn);
String sql1 = "update QQUser set OnLine = 0 where ParentId = ? ";
System.out.println("sql1:" + sql1);
db.setSqlValues(sql1);
List values = new ArrayList();
values.add(new Integer(1));
db.setValues(values);
try {
int rs = db.executeUpdate();
System.out.println("共有資料" + rs +"修改!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}