[JAVA100例]054、使用语句预处理
时间:2011-01-09
import java.sql.*;
import java.util.*;
/**
* <p>Title: 使用语句</p>
* <p>Description: 本实例演示使用语句方式查询数据库操作。语句是一种预处理的执行方法。</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: StatementConn.java</p>
* @version 1.0
*/
public class StatementConn{
private static String url="";
private static String username="";
private static String password="";
Connection con = null;
PreparedStatement updStmt=null;//语句对象
/**
*<br>方法说明:获得数据连接
*<br>输入参数:
*<br>返回类型:Connection 连接对象
*/
public Connection conn(){
try {
//加载JDBC驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//创建数据库连接
con = DriverManager.getConnection(url, username, password);
return con;
}catch(ClassNotFoundException cnf){
System.out.println("driver not find:"+cnf);
return null;
}catch(SQLException sqle){
System.out.println("can´t connection db:"+sqle);
return null;
} catch (Exception e) {
System.out.println("Failed to load JDBC/ODBC driver.");
return null;
}
}
/**
*<br>方法说明:关闭数据库
*<br>输入参数:
*<br>返回类型:
*/
public void close()
{
try
{
con.close();
}
catch(Throwable e){
System.out.println(e);
}
con = null;
}
/**
*<br>方法说明:语句执行
*<br>输入参数:
*<br>返回类型:
*/
private PreparedStatement getStatement(String sql,Vector vCondition) throws SQLException{
try
{
int i=0;
Object temp;
updStmt=conn().prepareStatement(sql);
for (i=0;i<vCondition.size();i++){
temp=vCondition.elementAt(i);
if (temp instanceof Integer) {
updStmt.setInt(i+1,((Integer)temp).intValue());
}
else if (temp instanceof Double) {
updStmt.setDouble(i+1,((Double)temp).doubleValue());
}
else if (temp instanceof String) {
String str=(temp.toString()).trim();
updStmt.setString(i+1,str);
}
else {
updStmt.setObject(i+1,temp);
}
}
}
catch(SQLException e)
{
throw e;
}
return updStmt;
}
/**
*<br>方法说明:关闭语句对象
*<br>输入参数:
*<br>返回类型:
*/
private void closeUpdStmt()
{
try
{
if(updStmt!=null)
updStmt.close();
}
catch(Throwable e)
{
System.out.println(e);
}
updStmt=null;
}
/**
*<br>方法说明:执行SQL
*<br>输入参数:
*<br>返回类型:
*/
public Object execute(String sql,Vector vCondition) throws SQLException,Exception {
java.sql.ResultSet rs = null;
java.util.Vector vResult = null;
try
{
if(!isSelect(sql))
{
//insert,update,delete
try
{
Integer iResult=new Integer(getStatement(sql,vCondition).executeUpdate());
return iResult;
}
catch(S
|