?? jdbop.java
字號:
/**
* 數(shù)據(jù)庫基本操作組件
* 封裝了記錄的增加、修改、刪除等功能
* 數(shù)據(jù)庫連接功能由其他組件完成(cn.com.huiton.ConnectionPool.ConnectionPool)
*
* 使用步驟:
* 1、創(chuàng)建對象;
* 2、使用 connect() 方法與數(shù)據(jù)庫建立連接;
* 3、進行數(shù)據(jù)庫操作;
* 4、使用 disConnect() 方法與數(shù)據(jù)庫斷開連接;
* 5、銷毀對象。
*
* @@author=吳劍、王濤
*/
package com.huiton.pub.dbx;
import java.sql.*;
import java.util.Vector;
import java.util.HashMap;
import java.util.*;
import com.huiton.mainframe.util.tracer.*;
public class JdbOp
{
public Connection m_conn;
public JdbObj preDbObj = null; //預先生成的數(shù)據(jù)庫操作類
public String m_errMsg = "";
public String sessionCode = null; //當前會話代碼,由數(shù)據(jù)庫生成
public Statement m_statement = null;
Vector stVector = new Vector(); //保存新創(chuàng)立的會話,在finalize中釋放它
boolean autoCommit = true;
String cfgFile = "";
boolean connectDirect = false;; //直接連接標志,當使用公司、年、子系統(tǒng)方式直接連庫時設置為true
public static final int DBNAME_SQLSERVER = 0;
public static final int DBNAME_ORACLE = 0;
public static final int DBNAME_SYBASE = 0;
public JdbOp() throws Exception{
/*connect();*/
createConnect(); //以缺省參數(shù)初始化數(shù)據(jù)庫對象,wt
}
/**
* Description: 構造函數(shù)。使用sessionCode獲取Cerp數(shù)據(jù)庫連接
* @param sessionCode:系統(tǒng)登陸時創(chuàng)建的會話唯一標識
* @param sysCode: 要聯(lián)接的系統(tǒng)代碼。如果為空,返回"sam"庫
*/
public JdbOp(String sessionCode,String sysCode) throws Exception{
if (!createConnect(sessionCode,sysCode))
throw (new Exception(m_errMsg));
}
/**
* Description: 構造函數(shù)。使用sessionCode獲取Cerp數(shù)據(jù)庫連接,本函數(shù)可以制定一個數(shù)據(jù)庫連接的配置文件
* @param sessionCode:系統(tǒng)登陸時創(chuàng)建的會話唯一標識
* @param sysCode: 要聯(lián)接的系統(tǒng)代碼。如果為空,返回"sam"庫
* @param cfgFile: 配置文件名
*/
public JdbOp(String sessionCode,String sysCode, String cfgFile) throws Exception{
if (cfgFile!=null && !cfgFile.equalsIgnoreCase(""))
this.cfgFile = cfgFile;
if (!createConnect(sessionCode,sysCode))
throw (new Exception(m_errMsg));
}
/**
* 按照給定的參數(shù),直接連接數(shù)據(jù)庫。本方法只用于Cerp
* @param companyCode 公司代號
* @param year 年號
* @param sysCode 子系統(tǒng)代號
* @param isDirect 保留參數(shù),應總是設置為true
* @throws Exception 拋出任何錯誤
*/
public JdbOp(String companyCode, String year, String sysCode,boolean isDirect) throws Exception {
if (!createConnect(companyCode,year,sysCode))
throw new Exception("創(chuàng)建數(shù)據(jù)庫聯(lián)接出錯: " + this.m_errMsg);
connectDirect = true;
}
/**
* 插入一條記錄
* @return boolean 操作成功返回 TRUE,否則返回 FALSE
* @param String table 表名
* @param String[] fields 字段名稱數(shù)組
* @param String[] types 字段類型數(shù)組,對于char, varchar類型,對應值是string,其它類型可為任意值
* @param String[] values 字段明對應的值的數(shù)組
*/
public boolean insert(String table,
String[] fields,
String[] values) throws SQLException, Exception
{
m_errMsg = "";
if(fields.length!=values.length)
throw new Exception("The numerber of fields and values must be identical.");
if(m_conn==null) connect();
Statement statement = m_conn.createStatement();
String strFields="", strValues="";
for(int i=0;i<fields.length;i++) {
strFields += fields[i] + ", ";
strValues += "'" + values[i] + "', ";
}
strFields = strFields.substring(0, strFields.length() - 2);
strValues = strValues.substring(0, strValues.length() - 2);
String mySql = "INSERT INTO " + table + "("
+ strFields + ") VALUES(" + strValues + ")";
// System.out.println(mySql);
statement.execute(mySql);
return true;
}
/**
* 更新一條記錄
* @return boolean 操作成功返回 TRUE,否則返回 FALSE
* @param String criteria 篩選條件
* @param String table 表名
* @param String[] fields 字段名稱數(shù)組
* @param String[] types 字段類型數(shù)組,對于char, varchar類型,對應值是string,其它類型可為任意值
* @param String[] values 字段明對應的值的數(shù)組
*/
public boolean update(String table,
String criteria,
String[] fields,
String[] values)
{ m_errMsg = "";
try {
if(fields.length!=values.length)
if(m_conn==null) connect();
Statement statement = m_conn.createStatement();
String strFields="";
for(int i=0;i<fields.length;i++) {
strFields += fields[i] + " = '" + values[i] + "', ";
}
strFields = strFields.substring(0, strFields.length() - 2);
if(criteria.equals("")) criteria = " 1=1 ";
System.out.println("UPDATE " + table + " SET " + strFields + " WHERE " + criteria);
statement.execute("UPDATE " + table + " SET " + strFields + " WHERE " + criteria);
return true;
}
catch(Exception e) {
// disConnect();
m_errMsg =this.getClass().getName() + ".update(): " + e;
System.out.println(e.toString());
return false;
}
}
/**
* 刪除一條記錄
* @return boolean 操作成功返回 TRUE,否則返回 FALSE
* @param String table 表名
* @param String criteria 篩選條件
*/
public boolean delete(String table, String criteria)
{m_errMsg = "";
try {
if(m_conn==null) connect();
Statement statement = m_conn.createStatement();
statement.execute("DELETE FROM " + table + " WHERE " + criteria);
return true;
}
catch(Exception e) {
// disConnect();
System.out.println(e.toString());
return false;
}
}
/**
* void connect() 與數(shù)據(jù)庫建立連接
*
* 返回值: void
*
* 參數(shù):
* modify by wt: 2001.7.12. remove 連接池
*/
public void connect() {
if(m_conn!=null) return;
try {
/*
ConnectionPool pool = new ConnectionPool();
if(pool.getDriver()==null) {
pool.setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
pool.setURL("jdbc:odbc:EJBTest");
pool.setUsername("cerp95");
pool.setPassword("ttbq");
pool.setSize(5);
pool.initializePool();
}
conn = pool.getConnection();
*/
System.out.println("-----in JdbOp:");
createConnect();
}
catch(Exception e) {
disConnect();
e.printStackTrace();
}
}
/**
* void disConnect() 斷開數(shù)據(jù)庫連接
*
* 返回值: void
*
* 參數(shù):
*/
public void disConnect() {
try {
if(m_conn!=null)
m_conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
/**
*Title: 創(chuàng)建表
*Description 本方法根據(jù)給出的參數(shù)創(chuàng)建數(shù)據(jù)表。使用本類中已經(jīng)創(chuàng)建的連接conn
*@param tblName 要創(chuàng)建的表名
*@param FieldDef[] 表的字段定義
*@return 表創(chuàng)建成功或表已經(jīng)存在,返回true。否則返回false
*/
public boolean createTbl(String tblName, FieldDef[] fields) {
m_errMsg = "";
String mySql = "Create table " + tblName + " (";
try{
if (preDbObj == null) {//連接還沒有建立,以缺省方式建立之
if (!createConnect()){
m_errMsg = preDbObj.m_errMsg;
return false;
}
}
//分析建表數(shù)據(jù)
if (tblName==null || tblName.length()==0) {
m_errMsg = "要創(chuàng)建的表名非法:" + tblName;
return false;
}
if (preDbObj.hasTable(tblName))
return true; //已經(jīng)有該表,直接返回
if (fields.length==0) {
m_errMsg = "沒有字段定義";
return false;
}
//創(chuàng)建表
int fCounter;
m_errMsg = "";
String keyStr = ""; //鍵值
//循環(huán)添加要創(chuàng)建的字段
for (fCounter = 0; fCounter < fields.length; fCounter++){
if (fCounter > 0)
mySql += ","; //拼下一句前加分隔符
mySql += fields[fCounter].colName + " ";
mySql += fields[fCounter].colType;
if (fields[fCounter].colDft!=null && fields[fCounter].colDft.length() > 0)
mySql += " default " + fields[fCounter].colDft; //有缺省值
else if (!fields[fCounter].nullable) //不允許為空
mySql += " not null";
//添加鍵值
if (fields[fCounter].isPriKey) {
if (keyStr.length() == 0)
keyStr = ", primary key(" + fields[fCounter].colName;
else
keyStr += "," + fields[fCounter].colName;
}
}
//完成語句
if (keyStr.length() > 0)
{//有鍵值定義
keyStr += ")";
mySql += keyStr + ")";
}
//執(zhí)行語句
Statement statement = preDbObj.conn.createStatement();
//System.out.println(mySql);
statement.execute(mySql);
return true;
}catch (Exception e) {
m_errMsg = e.getMessage() + "--\n" + mySql;
System.out.println(m_errMsg);
return false;
}
}
/**
* Title: 創(chuàng)建索引
* Description: 本功能在當前連接的數(shù)據(jù)庫中創(chuàng)建表的索引
* @param idxName 索引名
* @param tblName 表名
* @param isUnique 是否為唯一索引
* @param fields 索引字段定義
* @return 索引創(chuàng)建成功或索引已經(jīng)存在,返回true。否則返回false
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -