📄 jdbop.java
字号:
/**
* 数据库基本操作组件
* 封装了记录的增加、修改、删除等功能
* 数据库连接功能由其他组件完成(cn.com.huiton.ConnectionPool.ConnectionPool)
*
* 使用步骤:
* 1、创建对象;
* 2、使用 connect() 方法与数据库建立连接;
* 3、进行数据库操作;
* 4、使用 disConnect() 方法与数据库断开连接;
* 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; //预先生成的数据库操作类
public String m_errMsg = "";
public String sessionCode = null; //当前会话代码,由数据库生成
public Statement m_statement = null;
Vector stVector = new Vector(); //保存新创立的会话,在finalize中释放它
boolean autoCommit = true;
String cfgFile = "";
boolean connectDirect = false;; //直接连接标志,当使用公司、年、子系统方式直接连库时设置为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(); //以缺省参数初始化数据库对象,wt
}
/**
* Description: 构造函数。使用sessionCode获取Cerp数据库连接
* @param sessionCode:系统登陆时创建的会话唯一标识
* @param sysCode: 要联接的系统代码。如果为空,返回"sam"库
*/
public JdbOp(String sessionCode,String sysCode) throws Exception{
if (!createConnect(sessionCode,sysCode))
throw (new Exception(m_errMsg));
}
/**
* Description: 构造函数。使用sessionCode获取Cerp数据库连接,本函数可以制定一个数据库连接的配置文件
* @param sessionCode:系统登陆时创建的会话唯一标识
* @param sysCode: 要联接的系统代码。如果为空,返回"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));
}
/**
* 按照给定的参数,直接连接数据库。本方法只用于Cerp
* @param companyCode 公司代号
* @param year 年号
* @param sysCode 子系统代号
* @param isDirect 保留参数,应总是设置为true
* @throws Exception 抛出任何错误
*/
public JdbOp(String companyCode, String year, String sysCode,boolean isDirect) throws Exception {
if (!createConnect(companyCode,year,sysCode))
throw new Exception("创建数据库联接出错: " + this.m_errMsg);
connectDirect = true;
}
/**
* 插入一条记录
* @return boolean 操作成功返回 TRUE,否则返回 FALSE
* @param String table 表名
* @param String[] fields 字段名称数组
* @param String[] types 字段类型数组,对于char, varchar类型,对应值是string,其它类型可为任意值
* @param String[] values 字段明对应的值的数组
*/
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 字段名称数组
* @param String[] types 字段类型数组,对于char, varchar类型,对应值是string,其它类型可为任意值
* @param String[] values 字段明对应的值的数组
*/
public boolean update(String table,
String criteria,
String[] fields,
String[] values)
{ m_errMsg = "";
try {
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="";
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() 与数据库建立连接
*
* 返回值: void
*
* 参数:
* 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() 断开数据库连接
*
* 返回值: void
*
* 参数:
*/
public void disConnect() {
try {
if(m_conn!=null)
m_conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
/**
*Title: 创建表
*Description 本方法根据给出的参数创建数据表。使用本类中已经创建的连接conn
*@param tblName 要创建的表名
*@param FieldDef[] 表的字段定义
*@return 表创建成功或表已经存在,返回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;
}
}
//分析建表数据
if (tblName==null || tblName.length()==0) {
m_errMsg = "要创建的表名非法:" + tblName;
return false;
}
if (preDbObj.hasTable(tblName))
return true; //已经有该表,直接返回
if (fields.length==0) {
m_errMsg = "没有字段定义";
return false;
}
//创建表
int fCounter;
m_errMsg = "";
String keyStr = ""; //键值
//循环添加要创建的字段
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 + ")";
}
//执行语句
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: 创建索引
* Description: 本功能在当前连接的数据库中创建表的索引
* @param idxName 索引名
* @param tblName 表名
* @param isUnique 是否为唯一索引
* @param fields 索引字段定义
* @return 索引创建成功或索引已经存在,返回true。否则返回false
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -