/*
* Created on 2006-12-27
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.geo.dao;
/**
* @author Frank_Wang
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.Vector;
import org.apache.log4j.Category;
public class DatabaseManagerDirector {
private Connection conn = null;
//private ResultSet rs = null;
/**
* 由数据库连接创建的Statement对象.
*/
private Statement stmt = null;
private ResultSetMetaData rsmd = null;
private PreparedStatement pstmt = null;
final static String sDBDriver = "oracle.jdbc.driver.OracleDriver";
public DatabaseManagerDirector() throws SQLException {
connectDatabase();
}
private void connectDatabase() throws SQLException {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
this.conn=DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "dbuser", "dbuser");
//"jdbc:oracle:thin:@192.168.1.107:1521:biz", "bnet", "b1n2e3t4");
} catch (Exception e) {
System.out.println("连接失败");
}
this.stmt = conn.createStatement();
}
/**
* 取得数据库连接
* @return 返回数据的连接,为一个Connection对象.
*/
public Connection getConnection() {
return conn;
}
/**
* 关闭数据
* 库连接
* @throws SQLException
*/
public void closeConnection() throws SQLException {
if (pstmt != null)
pstmt.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
}
/*
* 功能:获得列的总数
*/
public int getColumnCount() throws SQLException {
if (rsmd == null)
throw new SQLException("ResultSet is null.");
return rsmd.getColumnCount();
}
/*
* 获得列的名称
*/
public String[] getColumnNames() throws SQLException {
if (rsmd == null)
throw new SQLException("ResultSet is null.");
String[] columnNames = new String[getColumnCount()];
for (int i = 1; i <= columnNames.length; i++)
columnNames[i - 1] = rsmd.getColumnName(i);
return columnNames;
}
/*
* 功能:清空记录集
*
*/
private void clearResult() throws SQLException {
if (stmt != null)
stmt.close();
stmt = null;
rsmd = null;
}
/*
* 返回记录集
*/
public Vector execSQL(String sqlStmt) throws SQLException {
Vector vct = new Vector();
ResultSet rs = null;
if (conn == null || conn.isClosed())
throw new SQLException("连接还未建立");
if (sqlStmt == null)
throw new SQLException("SQL-statement is null.");
stmt =
conn.createStatement();
rs = stmt.executeQuery(sqlStmt);
rsmd = rs.getMetaData();
if (rs == null) {
return null;
}
//rs.beforeFirst();
int columnCount = rsmd.getColumnCount();
String keys[] = new String[columnCount];
boolean flag = true;
if (!vct.isEmpty())
vct.removeAllElements();
while (rs.next()) {
//while (!rs.isAfterLast()) {
Hashtable rht = new Hashtable();
rht.clear();
for (int i = 1; i <= columnCount; i++) {
keys[i - 1] = rsmd.getColumnName(i);
}
for (int i = 1; i <= columnCount; i++) {
String result = rs.getString(i);
if (result == null)
result = "null";
rht.put(keys[i - 1], result);
}
if (flag) {
vct.add(keys);
flag = false;
}
vct.add(rht);
flag = false;
}
rs.close();
return vct;
}
/*
* 返回记录集
*/
public Vector execSQL_wk(String sqlStmt) throws SQLException {
Vector vct = new Vector();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
boolean flag = true;
try {
try {
//Context initCtx = new InitialContext();
//Context envCtx = (Context) initCtx.lookup("java:comp/env");
//DataSource ds = (DataSource) envCtx.lookup("jdbc/bnet");
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
this.conn=DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.1.107:1521:biz", "bnet", "b1n2e3t4");
} catch (Exception e) {
}
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlStmt);
rsmd = rs.getMetaData();
//rs.beforeFirst();
int columnCount = rsmd.getColumnCount();
String keys[] = new String[columnCount];
if (!vct.isEmpty())
vct.removeAllElements();
while (rs.next()) {
Hashtable rht = new Hashtable();
rht.clear();
for (int i = 1; i <= columnCount; i++) {
keys[i - 1] = rsmd.getColumnName(i);
}
for (int i = 1; i <= columnCount; i++) {
String result = rs.getString(i);
if (result == null)
result = "null";
rht.put(keys[i - 1], result);
}
if (flag) {
vct.add(keys);
flag = false;
}
vct.add(rht);
flag = false;
}
return vct;
} catch (Exception e) {
} finally {
try {
if (conn != null)
conn.close();
if (stmt != null)
stmt.close();
if (rs != null)
rs.close();
} catch (Exception e) {
}
}
return vct;
}
/*
* 返回单条记录
*/
public Hashtable execSql_single(String sqlStmt) throws SQLException {
Hashtable hasReturn = new Hashtable();
ResultSet rs = null;
Vector vct = new Vector();
if (conn == null || conn.isClosed())
throw new SQLException("连接还未建立");
if (sqlStmt == null)
throw new SQLException("SQL-statement is null.");
clearResult();
conn.setAutoCommit(true);
stmt =
conn.createStatement();
rs = stmt.executeQuery(sqlStmt);
rsmd = rs.getMetaData();
if (rs == null) {
return null;
}
//rs.beforeFirst();
int columnCount = rsmd.getColumnCount();
String keys[] = new String[columnCount];
boolean flag = true;
if (!vct.isEmpty())
vct.removeAllElements();
while (rs.next()) {
Hashtable rht = new Hashtable();
rht.clear();
for (int i = 1; i <= columnCount; i++) {
keys[i - 1] = rsmd.getColumnName(i);
}
for (int i = 1; i <= columnCount; i++) {
String result = rs.getString(i);
if (result == null)
result = "null";
rht.put(keys[i - 1], result);
}
if (flag) {
vct.add(keys);
flag = false;
}
vct.add(rht);
flag = false;
}
if (vct.size() > 0)
hasReturn = (Hashtable) vct.elementAt(1);
else
hasReturn = null;
rs.close();
return hasReturn;
}
/**
* 功能:对数据库进行增和删除的操作
* @param sqlStmt String
* @throws SQLException
* @return int
*/
public int exec_SQL(String sqlStmt) throws SQLException {
if (conn == null || conn.isClosed())
throw new SQLException("This connection has not been established yet.");
if (sqlStmt == null)
throw new SQLException("SQL-statement is null.");
clearResult();
conn.setAutoCommit(true);
stmt = conn.createStatement();
int numRow = 0;
try {
numRow = stmt.executeUpdate(sqlStmt);
clearResult();
} catch (SQLException ex) {
conn.rollback();
return 0;
}
return numRow;
}
//批量提交
public int execUpdate(String[] sqlStmts) throws SQLException {
if (conn == null || conn.isClosed())
throw new SQLException("连接还未建立.");
if (sqlStmts == null || sqlStmts.length == 0)
throw new SQLException("SQL-statement is null.");
clearResult();
conn.setAutoCommit(false);
int ret=0;
try {
for (int i = 0; i < sqlStmts.length; i++) {
stmt = conn.createStatement();
ret=stmt.