📄 jdbcquery.java
字号:
/*==============================================================================
|| @公司名称: In-net Corp.
|| @作者姓名: Viking.Liu
|| @生成时间: 2007-1-15
|| @单元名称: JQuery.java
|| @单元作用: 执行SQL语句和查询语句的类.可保存结果集.连接来自JConPool对象的连接池
|| @注意事项:
=============================================================================*/
package com.liuhaomiao.VkJDBC;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import oracle.jdbc.driver.OracleTypes;
import sun.jdbc.rowset.CachedRowSet;
public class JDBCQuery
{
private JDBCConPool jpool_m = null;
private String strSQL_m = ""; // SQL语句
private CachedRowSet set_m = null; // 结果集保存 rowset
private Statement stmt_m = null; //为executeBatch执行准备
/***************************************************************************
* @函数名称: JdbcQuery()
* @函数用途: 构造函数:
* @参数列表: strCnfgFile[保存配置的文件名]
* @注意事项:
**************************************************************************/
public JDBCQuery(JDBCConPool jpool)
{
jpool_m = jpool;
}
/***************************************************************************
* @函数名称: JdbcQuery()
* @函数用途: 构造函数:
* @参数列表: strCnfgFile[保存配置的文件名]
* @注意事项:
**************************************************************************/
public JDBCQuery()
{
jpool_m = null;
}
/***************************************************************************
* @throws SQLException
* @函数名称: Open()
* @函数用途: 查询有返回的集合 [select], 结果集保存在set_m中
* @参数列表:
* @注意事项:
**************************************************************************/
public void Open() throws SQLException
{
Connection con = null;
Statement stmt = null;
try
{
con = getConnection();
if (con != null)
{
stmt = con.createStatement();
if (stmt != null)
{
if (set_m == null)
{
set_m = new CachedRowSet();
}
set_m.populate(stmt.executeQuery(strSQL_m));
if (set_m.size() > 0)
{
set_m.first();
}
}
}
}
finally
{
try
{
stmt.close();
}
catch (Exception e)
{
}
try
{
con.close();
}
catch (Exception e)
{
}
}
}
/***************************************************************************
* @throws SQLException
* @函数名称: Execute()
* @函数用途: 执行无返回的SQL语句 [insert/delete]
* @参数列表:
* @注意事项:
**************************************************************************/
public void Execute() throws SQLException
{
Connection con = null;
Statement stmt = null;
try
{
con = getConnection();
if (con != null)
{
stmt = con.createStatement();
if (stmt != null)
{
stmt.executeUpdate(strSQL_m);
}
}
}
finally
{
try
{
stmt.close();
}
catch (Exception e)
{
}
try
{
con.close();
}
catch (Exception e)
{
}
}
}
/****************************************************************************
* @函数名称: getConnection()
* @函数用途: 从连接池JConPool中获取连接
* @参数列表:
* @注意事项:
***************************************************************************/
private Connection getConnection() throws SQLException
{
Connection conRet = jpool_m.GetConnetion();
return conRet;
}
/***************************************************************************
* @函数名称: Close()
* @函数用途: 关闭所有数据库连接:
* @参数列表:
* @注意事项:
**************************************************************************/
public void Close()
{
try
{
set_m.close();
}
catch (Exception e)
{
//e.printStackTrace();
}
}
/***************************************************************************
* @函数名称: setSQL
* @函数用途: 设置SQL语句
* @参数列表:
* @注意事项:
**************************************************************************/
public void setSQL(String strSQL)
{
strSQL_m = strSQL;
}
/****************************************************************************
* @throws SQLException
* @函数名称: addBatch
* @函数用途: 增加批操作的语句
* @参数列表:int [] =
* @注意事项:
***************************************************************************/
public void addBatch(String strSQL) throws SQLException
{
if (stmt_m == null)
{
Connection con = getConnection();
if (con != null)
{
stmt_m = con.createStatement();
}
}
//增加执行语句
if (stmt_m != null)
{
stmt_m.addBatch(strSQL);
}
}
/****************************************************************************
* @throws SQLException
* @函数名称: executeBatch()
* @函数用途: 执行批处理
* @参数列表:
* @注意事项:
***************************************************************************/
public int[] executeBatch() throws SQLException
{
int[] listRet = null;
//获取执行者
if (stmt_m == null)
{
Connection con = getConnection();
if (con != null)
{
stmt_m = con.createStatement();
}
}
//执行批处理
if (stmt_m != null)
{
try
{
listRet = stmt_m.executeBatch();
}
finally
{
try
{
stmt_m.close();
}
catch (Exception e)
{
}
}
}
return listRet;
}
/***************************************************************************
* @throws SQLException
* @函数名称: ExecuteProc
* @函数用途: 执行存储过程
* @参数列表: nIndex[参数的序号] strPara[参数值.都用String]
* @注意事项: 重载本函数是为给hibernate中调用存储过程提供便利
**************************************************************************/
public void ExecuteProc(Connection con, String strProcName,
ArrayList listPara) throws SQLException
{
//生成执行存储过程所需要的SQL语句
//Connection con = null;
CallableStatement proc = null;
//设置SQL语句
String strSQL = "{ call " + strProcName + " (";
for (int i = 0; i < listPara.size(); i++)
{
if (i < listPara.size() - 1)
{
strSQL = strSQL + "?,";
}
else
{
strSQL = strSQL + "?";
}
}
strSQL = strSQL + ") }";
//执行存储过程
try
{
//con = getConnection();
if (con != null)
{
proc = con.prepareCall(strSQL);
for (int i = 0; i < listPara.size(); i++)
{
String strPara = (String) listPara.get(i);
proc.setString(i + 1, strPara);
}
proc.execute();
}
}
finally
{
try
{
proc.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
try
{
con.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
/***************************************************************************
* @throws SQLException
* @函数名称: ExecuteProc
* @函数用途: 执行存储过程
* @参数列表: nIndex[参数的序号] strPara[参数值.都用String]
* @注意事项: edit by lhm 2007-03-19:
**************************************************************************/
public void ExecuteProc(String strProcName, ArrayList listPara)
throws SQLException
{
//生成执行存储过程所需要的SQL语句
Connection con = null;
CallableStatement proc = null;
//设置SQL语句
String strSQL = "{ call " + strProcName + " (";
for (int i = 0; i < listPara.size(); i++)
{
if (i < listPara.size() - 1)
{
strSQL = strSQL + "?,";
}
else
{
strSQL = strSQL + "?";
}
}
strSQL = strSQL + ") }";
//执行存储过程
try
{
con = getConnection();
if (con != null)
{
proc = con.prepareCall(strSQL);
for (int i = 0; i < listPara.size(); i++)
{
String strPara = (String) listPara.get(i);
proc.setString(i + 1, strPara);
}
proc.execute();
}
}
finally
{
try
{
proc.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
try
{
con.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
/***************************************************************************
* @throws SQLException
* @函数名称: ExecuteFunc
* @函数用途: 执行存储函数
* @参数列表: nIndex[参数的序号] strPara[参数值.都用String]
* @注意事项:
**************************************************************************/
public String ExecuteFunc(String strProcName, ArrayList listPara)
throws SQLException
{
String strRet = "";
Connection con = null;
CallableStatement proc = null;
// 生成执行存储过程所需要的SQL语句
String strSQL = "{ ?=call " + strProcName + " (";
for (int i = 0; i < listPara.size(); i++)
{
if (i < listPara.size() - 1)
{
strSQL = strSQL + "?,";
}
else
{
strSQL = strSQL + "?";
}
}
strSQL = strSQL + ") }";
//执行存储函数并返回数据
try
{
con = getConnection();
if (con != null)
{
proc = con.prepareCall(strSQL);
proc.registerOutParameter(1, Types.VARCHAR);
for (int i = 0; i < listPara.size(); i++)
{
String strPara = (String) listPara.get(i);
proc.setString(i + 2, strPara);
}
proc.execute();
strRet = proc.getString(1);
}
}
finally
{
try
{
proc.close();
}
catch (Exception e)
{
e.printStackTrace();
}
try
{
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return strRet;
}
/***************************************************************************
* @throws SQLException
* @函数名称: ExecuteFunc
* @函数用途: 执行存储函数
* @参数列表: nIndex[参数的序号] strPara[参数值.都用String]
* @注意事项: 重载本函数是为给hibernate中调用存储过程提供便利
**************************************************************************/
public String ExecuteFunc(Connection con, String strProcName,
ArrayList listPara) throws SQLException
{
String strRet = "";
//Connection con = null;
CallableStatement proc = null;
// 生成执行存储过程所需要的SQL语句
String strSQL = "{ ?=call " + strProcName + " (";
for (int i = 0; i < listPara.size(); i++)
{
if (i < listPara.size() - 1)
{
strSQL = strSQL + "?,";
}
else
{
strSQL = strSQL + "?";
}
}
strSQL = strSQL + ") }";
//执行存储函数并返回数据
try
{
//con = getConnection();
if (con != null)
{
proc = con.prepareCall(strSQL);
proc.registerOutParameter(1, Types.VARCHAR);
for (int i = 0; i < listPara.size(); i++)
{
String strPara = (String) listPara.get(i);
proc.setString(i + 2, strPara);
}
proc.execute();
strRet = proc.getString(1);
}
}
finally
{
try
{
proc.close();
}
catch (Exception e)
{
e.printStackTrace();
}
try
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -