📄 oadatasource.java
字号:
package hrms.common;
import java.sql.*;
import java.io.*;
import java.util.*;
//import weblogic.db.jdbc.*;
/** JDOC 数据库连接 bean for Oracle 8.1.7
*<br> 2002.6
*<br> 使用 Weblogic JDBC 连接池
*/
public class OADataSource
{
public Connection conn = null;
//MyRs rs = null;
//ResultSet rs=null;
boolean status = false;
public String dburl = "jdbc:weblogic:pool:OracleThinPool";
public String dbdriver = "weblogic.jdbc.pool.Driver";
public String dbuser = "water";
public String dbpass = "water";
private Driver myDriver = null;
public Statement stmt = null;
public CallableStatement cstmt=null;
/** 加载驱动
*
*/
public OADataSource()
{
try
{
this.connect();
//System.out.println("create new db con");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
/** 建立连接
*
*/
public void connect()
{
try
{
this.conn = DBConnectionManager.getInstance().getConnection();
this.status = true;
}
catch(Exception e)
{
System.out.println("Error: Method: dbconn.connect :"+e.getMessage());
}
}
/** 建立连接
*
*/
public Connection getconnect()
{
connect();
return this.conn;
}
public void close()
{
try{
if(stmt != null){stmt.close();} }catch(Exception e){
//System.out.println("Error: close stmt:"+e);
}
// try{
// if(rs != null){rs.close();}}catch(Exception e){
// //System.out.println("Error:close rs:"+e);
// }
}
public void freeConnection(){
if( this.conn != null ){
DBConnectionManager.getInstance().freeConnection(conn);
conn=null;
this.status=false;
}
}
////////////////////////////////////////////////////////////////////////////////////////
public Connection getDBConnection()throws SQLException
{
return getconnect();
}
public void releaseResource(ResultSet paramResultSet, PreparedStatement paramPreparedStatement, Connection paramConnection)
throws SQLException
{
if (paramResultSet != null) try{
paramResultSet.close();
}catch(java.sql.SQLException ex){
ex.printStackTrace();
}
if (paramPreparedStatement != null) try{
paramPreparedStatement.close();
}catch(java.sql.SQLException ex){
ex.printStackTrace();
}
freeConnection();
}
public void releaseResource2(ResultSet paramResultSet, Statement paramStatement, Connection paramConnection)
throws SQLException
{
if (paramResultSet != null) {
paramResultSet.close();
paramResultSet = null;
}
if (paramStatement != null) {
paramStatement.close();
paramStatement = null;
}
freeConnection();
}
public void releaseResource3(PreparedStatement paramPreparedStatement, Connection paramConnection)
throws SQLException
{
if (paramPreparedStatement != null) {
paramPreparedStatement.close();
paramPreparedStatement = null;
}
freeConnection();
}
public void releaseResource4(Statement paramStatement, Connection paramConnection)
throws SQLException
{
if (paramStatement != null) {
paramStatement.close();
paramStatement = null;
}
freeConnection();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/** 断开连接
*
*/
public void disconnect(){
close();
freeConnection();
}
public void disconnect_old(){
try{
if(this.stmt != null){
this.stmt.close();
}
if( this.conn != null ){
DBConnectionManager.getInstance().freeConnection(conn);
this.status=false;
}
}
catch(Exception e)
{
System.out.println("Error: Method: dbconn.disconnect :"+e.getMessage());
}
}
/** 释放连接
*
*/
protected void finalize()
{
close();
if(conn != null){
freeConnection();
System.out.println("系统自动释放 relase db con");
}
}
protected void finalize_old()
{
if( this.conn != null && this.status)
{
try{
if(this.stmt != null){
this.stmt.close();
}
DBConnectionManager.getInstance().freeConnection(conn);
this.status=false;
System.out.println("系统自动释放 relase db con");
}
catch(Exception ex){
}
}
}
/** 执行更新
* @param sql SQL语句
* @return SQL执行影响的记录数
*/
public int executeUpdate(String sql)
{
close();
int rows=0;
try
{
if (conn != null){
stmt= this.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rows=stmt.executeUpdate(gb2iso(sql));
//stmt.close();
}
}
catch (Exception e)
{
System.err.println("Error: Method: dbconn.executeUpdate :"+iso2gb(e.getMessage()));
}
return rows;
}
public CallableStatement prepareCall(String sql)
{
close();
try
{
if (conn != null){
cstmt=conn.prepareCall(sql);
//stmt.close();
}
else
{
cstmt=null;
System.out.println("!! Conn is null !!");
}
}
catch (Exception e)
{
System.err.println("Error: Method: dbconn.executeUpdate :"+e.getMessage());
}
return cstmt;
}
/** 执行查询
* @param sql SQL语句
* @return 查询结果记录集
*/
// public ResultSet executeQuery(String sql)
// {
// close();
// try
// {
// if (conn != null)
// {
// stmt= this.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
// ///this.rs=stmt.executeQuery(gb2iso(sql));
//// rs=new MyRs(stmt.executeQuery(gb2iso(sql)));
// }
// else
// {
// System.out.println("!! Conn is null !!");
//// rs=null;
// }
// }
// catch(Exception e)
// {
// System.out.println("-- Error --");
// System.out.println(sql);
// e.printStackTrace();
// System.out.println("-- Error --");
// System.out.println("Error: Method: dbconn.executeQuery :"+e.getMessage());
// }
// return this.rs;
// }
/** 字符集转换,ISO8859-1 -> gb2312.用于解决中文乱码
* @param str 待转换的字符串
* @return 已转换的字符串
*/
public static String iso2gb(String str)//解决 中文乱码
{
byte[] tmpbyte=null;
if (str == null)
{
return "";
}
try
{
tmpbyte=str.getBytes("ISO8859_1");
}
catch(UnsupportedEncodingException e)
{
System.out.println("Error: Method: dbconn.gb2iso :"+e.getMessage());
}
try
{
str=new String(tmpbyte,"GBK");
}
catch(UnsupportedEncodingException e)
{
System.out.println("Error: Method: dbconn.gb2iso :"+e.getMessage());
}
if (str.equals(null))
{
str ="";
}
return str;
}
/** 字符集转换,gb2312 -> ISO8859-1.用于解决中文乱码
* @param str 待转换的字符串
* @return 已转换的字符串
*/
public static String gb2iso(String str)//解决 中文乱码
{
byte[] tmpbyte=null;
if (str == null)
{
str ="";
}
try
{
tmpbyte=str.getBytes("GBK");
}
catch(UnsupportedEncodingException e)
{
System.out.println("Error: Method: dbconn.gb2iso :"+e.getMessage());
}
try
{
str=new String(tmpbyte,"ISO8859_1");
}
catch(UnsupportedEncodingException e)
{
System.out.println("Error: Method: dbconn.gb2iso :"+e.getMessage());
}
return str;
}
/** 取得数据库连接参数
*
*/
public void getprop()
{
Properties prop =new Properties();
try
{
InputStream is = new FileInputStream("/java/db.properties");
prop.load(is);
if (is != null) is.close();
}
catch (Exception e)
{
System.out.println("Open DATABASE property file error!");
setprop();
}
String dburl=prop.getProperty("dburl",this.dburl);
String dbuser=prop.getProperty("dbuser",this.dbuser);
String dbpass=prop.getProperty("dbpass",this.dbpass);
this.dburl=dburl;
this.dbpass=dbpass;
this.dbuser=dbuser;
}
/** 设置数据库连接参数文件
*
*/
public boolean setprop()
{
Properties prop= new Properties();
prop.put("dburl",this.dburl);
// prop.put("dbuser",this.dbuser);
// prop.put("dbpass",this.dbpass);
boolean flag=false;
FileOutputStream outfile;
try
{
outfile = new FileOutputStream("/java/db.properties");
prop.store(outfile, gb2iso("数据库连接参数"));
outfile.close();
outfile.flush();
outfile.close();
flag=true;
}
catch(Exception e)
{
e.printStackTrace();
}
return flag;
}
/** 用于测试的程序
*
*/
// public static void main(String[] args)
// {
// System.out.println("Hello World!");
// ResultSet rs=null ;
// OADataSource dbconn= new OADataSource();
// try
// {
//
// dbconn.connect();
// dbconn.executeUpdate(" update employee set password ='haha' where id='EL02001048'");
// rs = dbconn.executeQuery("select * from window_prop where 1=2");
// while (rs.next())
// {
// System.out.println(rs.getString("name"));
// }
// rs.close();
// dbconn.disconnect();
// }
// catch (Exception e)
// {
// e.printStackTrace();
//
// }
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -