?? parentbean.java
字號(hào):
/**********************************************************
Copyright (C),2003-10-14, Beijing USTB.
All rights reserved.
Filename: ParentBean.java
Author: 劉中兵
Version 1.0
Date:2003-10-15
Description:封裝與數(shù)據(jù)庫類之上,Bean之下,用于選擇連接,釋放連接
Other:
Variable List:
1.DataBase db = new DataBase();
2.DealString ds = new DealString()//字符串處理時(shí)使用對(duì)象
Function List:
1.public ParentBean()//構(gòu)造函數(shù),初始化連接
2.public Connection getConn()//返回一個(gè)數(shù)據(jù)庫連接
3.public void createConn(int type,String url,String usr,String pwd)//取得不同數(shù)據(jù)庫的連接
4.public void closeConn()//釋放連接
5.public static void showConnNUM(boolean bool)//測(cè)試連接計(jì)數(shù)
//基本函數(shù)
6.protected ResultSet selectRecord(String sql)//查詢記錄
7.protected int insertRecord(Vector vect)//新增記錄
8.protected int updateRecord(Vector vect)//修改記錄
9.protected void deleteRecord(String sql)//刪除記錄
10.protected int executeUpdate(String sql)//執(zhí)行語句
//輔助使用函數(shù)
11.public int makeID(String table,String field1,String field2,String value1,boolean type1)//產(chǎn)生唯一編號(hào)
public int makeID(String table,String field1,String field2,String field3,String value1,String value2,boolean type1,boolean type2)
public int makeID_Add1(String table,String field1,String field2,String value1,boolean type1)//產(chǎn)生唯一編號(hào)
public int makeID_Add1(String table,String field1,String field2,String field3,String value1,String value2,boolean type1,boolean type2)
12.public int toID(String table,String field1,String field2,String value1)//將名稱轉(zhuǎn)換為編號(hào)
13.public String toName(String table,String field1,String field2,String value1)//將編號(hào)轉(zhuǎn)換為名稱
14.protected Vector addVector(String field,String value,String type)//生成4、5的域值
15.public Vector getOnePage(String sql,int page,int records)//分頁時(shí)取得一頁的數(shù)據(jù)量
16.public int setSort(String table,String field1,String field2,String wherestr,String orderstr,boolean b)//為某一個(gè)字段進(jìn)行重新排序
//查詢與更新LOB類型
17.public String QueryCLOB(String table,String wherestr,String clobfield)//查詢CLOB類型值
18.public int UpdateCLOB(String table,String wherestr,String blobfield,String blobvalue)//修改CLOB類型值
19.public String QueryBLOB(String table,String wherestr,String clobfield)//查詢BLOB類型值
20.public int UpdateBLOB(String table,String wherestr,String blobfield,String blobvalue)//修改BLOB類型值
21.public String QueryBLOB_JNDI(String table,String wherestr,String blobfield)//查詢BLOB類型值
22.public int UpdateBLOB_JNDI(String table,String wherestr,String blobfield,String blobvalue)//修改BLOB類型值
//數(shù)據(jù)庫結(jié)構(gòu)信息
23.public Hashtable getDataBaseInfo()//數(shù)據(jù)庫信息
24.public Vector getTableList()//數(shù)據(jù)表列表
25.public Vector getTableStruct(String table)//數(shù)據(jù)表的結(jié)構(gòu)
26.public Vector getResultSetData(ResultSet rs)//取得數(shù)據(jù)集內(nèi)容
//對(duì)sql語句的分布處理
27.public void prepareStatement(String sql)//創(chuàng)建申明對(duì)象
28.public void executeQuery()//執(zhí)行查詢
29.public boolean next()//轉(zhuǎn)向下一條
30.public String getObject(String field,String sqlType)//取得數(shù)據(jù)并根據(jù)數(shù)據(jù)類型轉(zhuǎn)化為字符串
31.public void setObject(int index,String value,String sqlType)//根據(jù)數(shù)據(jù)類型保存到數(shù)據(jù)庫
32.public void executeUpdate()//執(zhí)行更新
33.public void closePstm()//關(guān)閉申明對(duì)象
34.public void closeRs()//關(guān)閉游標(biāo)
1.public void createStatement();//專門使用 //
2.public void clearBatch(); //
3.public void addBatch(String sql); //
4.public int[] executeBatch(); //
0.public void closeStm();//專門使用 //
35.public Vector getDataBySql(String sql)//由sql語句得到向量紀(jì)錄
History:
***********************************************************/
package kjb.main;
import java.lang.*;
import java.util.*;
import java.sql.*;
public class ParentBean extends Object
{
//數(shù)據(jù)庫處理對(duì)象*/
protected DataBase db = new DataBase();
//字符串處理時(shí)使用對(duì)象*/
protected static DealString ds = new DealString();
public static int count=0;
private static float memcount = 0.0f;
/**當(dāng)前連接的數(shù)據(jù)庫類型*/
public static String DBType = "NULL";
public static String DBName = "";
public static String FilesPath = "";
//構(gòu)造函數(shù),初始化連接*/
public ParentBean()
{
// System.gc();
//連接池取得連接
//db.getConnPool();
//System.out.println("從連接池創(chuàng)建連接成功!");
//從類里取得連接
//db.createConn("oracle.jdbc.driver.OracleDriver","jdbc:oracle:thin:@10.0.0.94:1521:hg","oaadminuser","lancom4454");
//db.createConn("oracle.jdbc.driver.OracleDriver","jdbc:oracle:thin:@192.168.0.53:1521:jw","jw","jw");
//db.createConn(oracle.jdbc.driver.OracleDriver","jdbc:oracle:thin:@10.2.4.253:1521:jw","jw","u2s0t0B3");
//db.createConn("oracle.jdbc.driver.OracleDriver","jdbc:oracle:thin:@localhost:1521:kjb","kjb","kjb");
//從Access取得連接
//db.createConn("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:HANFENG","admin","");
//System.out.println("通過自身創(chuàng)建連接成功!");
//db.getMyConnPool();
//DBType = "Oracle";
//DBName = "jw";
db.createConn("org.gjt.mm.mysql.Driver","jdbc:mysql://localhost:3306/lib","root","");
//init();
// System.out.print("\r當(dāng)前實(shí)際連接數(shù):["+ds.toLengthStr(curnum+"",5)+"] 接受請(qǐng)求連接數(shù):["+ds.toLengthStr(sumnum+"",10)+"]");
}
public void init()
{
ResultSet rs = selectRecord("select XMMC from CODE_ZDB where ZDMC='上傳文件存放路徑'");
Statement stmt = null;
try{
if(rs.next())
{
FilesPath = rs.getString(1);
}
}catch(Exception e){System.out.println("ParentBean::init(void)運(yùn)行時(shí)出錯(cuò):"+e);}
finally{
if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("ParentBean::init(void)關(guān)閉記錄集rs時(shí)出錯(cuò)"+e);}
if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("ParentBean::init(void)關(guān)閉聲明時(shí)statement出錯(cuò)"+e);}
}
}
/**返回一個(gè)數(shù)據(jù)庫連接*/
public Connection getConn()
{
DBType = "Oracle";
DBName = "jw";
return db.conn;
}
/**取得不同數(shù)據(jù)庫的連接*/
public void createConn(int type,String ip,String port,String dbsrv,String dbname,String usr,String pwd)
{
/**
*type: 數(shù)據(jù)庫類型
* 1. Oracle url示例:"jdbc:oracle:thin:@localhost:1521:orcl"
* 2. Access url示例:"jdbc:odbc:HANFENG"
* 3. SQL Server url示例:"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"
* 4. DB2 url示例:"jdbc:db2://localhost:5000/sample"
* 5. Informix url示例:"jdbc:informix-sqli://123.45.67.89:1533/testDB:INFORMIXSERVER=myserver"
* 6. Sybase url示例:"jdbc:sybase:Tds:localhost:5007/tsdata"
* 7. MySQL url示例:"jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
* 8. PostgreSQL url示例:"jdbc:postgresql://localhost/soft"
* 9. Dbtools url示例:
* 10.Foxpro url示例:
* 11.Paradox url示例:
* 12.Excel url示例:
* 13.Text url示例:
* 14.XBase url示例:
* 15.dBase url示例:
* 16.FoxBase url示例:
* 17.SQLBase url示例:
* 18.Approach url示例:
*
*ip: 地址,如192.168.0.1,或localhost
*port: 端口號(hào)
*dbsrv: 數(shù)據(jù)庫服務(wù)器(Informix專有)
*dbname: 數(shù)據(jù)庫名,或數(shù)據(jù)源名
*
*參數(shù)用法:
* 1.type不同數(shù)據(jù)庫使用不同序號(hào);
* 2.Access只是用數(shù)據(jù)源dbname;
* 3.dbsrv只有使用Informix;
* 4.port對(duì)MySQL,PostgreSQL不用;
* 5.ip,port,dbnme除了以上2和4之外都要使用;
*/
this.DBName = dbname;
if(type==1)//Oracle
{
db.createConn("oracle.jdbc.driver.OracleDriver","jdbc:oracle:thin:@"+ip+":"+port+":"+dbname,usr,pwd);
DBType = "Oralce";
}
else if(type==2)//Access
{
db.createConn("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:"+dbname);
DBType = "Access";
}
else if(type==3)//SQL Server
{
db.createConn("com.microsoft.jdbc.sqlserver.SQLServerDriver","jdbc:microsoft:sqlserver://"+ip+":"+port+";DatabaseName="+dbname,usr,pwd);
DBType = "SQLServer";
}
else if(type==4)//DB2
{
db.createConn("com.ibm.db2.jdbc.app.DB2Driver","jdbc:db2://"+ip+":"+port+"/"+dbname,usr,pwd);
DBType = "DB2";
}
else if(type==5)//Informix
{
db.createConn("com.informix.jdbc.IfxDriver","jdbc:informix-sqli://"+ip+":"+port+"/"+dbname+":INFORMIXSERVER="+dbsrv,usr,pwd);
DBType = "Informix";
}
else if(type==6)//Sybase
{
db.createConn("com.sybase.jdbc.SybDriver","jdbc:sybase:Tds:"+ip+":"+port+"/"+dbname,usr,pwd);
DBType = "Sybase";
}
else if(type==7)//MySQL
{
db.createConn("org.gjt.mm.mysql.Driver","jdbc:mysql://"+ip+"/"+dbname+"user="+usr+"&password="+pwd+"&useUnicode=true&characterEncoding=8859_1");
DBType = "MySQL";
}
else if(type==8)//PostgreSQL
{
db.createConn("org.postgresql.Driver","jdbc:postgresql://"+ip+"/s"+dbname,usr,pwd);
DBType = "PostgreSQL";
}
else if(type==9)//Dbtools
{
DBType = "Dbtools";
}
else if(type==10)//Foxpro
{
DBType = "Foxpro";
}
else if(type==11)//Paradox
{
DBType = "Paradox";
}
else if(type==12)//Excel
{
DBType = "Excel";
}
else if(type==13)//Text
{
DBType = "Text";
}
else if(type==14)//XBase
{
DBType = "XBase";
}
else if(type==15)//dBase
{
DBType = "dBase";
}
else if(type==16)//FoxBas
{
DBType = "FoxBas";
}
else if(type==17)//SQLBase
{
DBType = "SQLBase";
}
else if(type==18)//Approach
{
DBType = "Approach";
}
else
{
DBType = "NULL";
}
}
/**釋放連接*/
public void closeConn()
{
DBType = "NULL";
db.releaseConn();
//db.releaseMyConnPool();
//System.out.println("釋放連接");
// System.out.print("\r當(dāng)前實(shí)際連接數(shù):["+ds.toLengthStr(curnum+"",5)+"] 接受請(qǐng)求連接數(shù):["+ds.toLengthStr(sumnum+"",10)+"]");
}
/**查詢記錄*/
public ResultSet selectRecord(String sql)
{
return db.QuerySQL(sql);
}
/**新增記錄*/
protected int insertRecord(Vector vect)
{
/**Vector:第1項(xiàng) 表名(String)
// 第2項(xiàng) 列名(Vector[Field(String),Value(String,CLOB,BLOB),Type("CHAR","NUM","TIME","CLOB","BLOB")])*/
//臨時(shí)變量
String sqlField = "";//形如(F1,F2)
String sqlValue = "";//形如(V1,V2)
String field = "";
String value = "";
String type = "";
for(int i=1;i<vect.size();i++)
{
//對(duì)某一個(gè)字段
Vector v_t = (Vector)vect.get(i);
field = (String)v_t.get(0);
value = (String)v_t.get(1);
if (value.indexOf("'")!=-1)
{
value = value.replaceAll("'","''");
}
type = (String)v_t.get(2);
//組合字段SQL
if(sqlField.equals(""))sqlField = "(";
else sqlField = sqlField + ",";
sqlField = sqlField + field;
//組合值SQL
if(sqlValue.equals(""))sqlValue = "(";
else sqlValue = sqlValue + ",";
if(value.equals(""))//為空時(shí)
{
sqlValue = sqlValue + "null";
}
else if(type.equals("CHAR"))//字符串
{
sqlValue = sqlValue + "'" + value + "'";
}
else if(type.equals("NUM"))//數(shù)值
{
sqlValue = sqlValue + value;
}
else if(type.equals("TIME"))//日期
{
sqlValue = sqlValue + "to_date('yyyy-MM-dd HH:mm:ss','" + value + "')";
}
else if(type.equals("CLOB"))//clob類型
{
sqlValue = sqlValue + "empty_clob()";
}
else if(type.equals("BLOB"))//blob類型
{
sqlValue = sqlValue + "empty_blob()";
}
}
sqlField = sqlField + ")";
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -