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(SQLException e1)
{
throw e1;
}
}
else {
//select
rs = getStatement(sql,vCondition).executeQuery();
int columnCount = rs.getMetaData().getColumnCount();
vResult = new Vector();
while(rs.next())
{
Vector vTemp = new Vector();
for(int i = 0;i< columnCount;i++)
{
String sTemp = rs.getString(i+1);
vTemp.addElement(sTemp== null ? "" : sTemp.trim());
}
vResult.addElement(vTemp);
}
rs.close();
closeUpdStmt();
}
}
catch(Exception e1)
{
throw e1;
}
finally
{
close();
}
return vResult;
}
/**
*<br>方法說明:
*<br>輸入參數:
*<br>返回類型:
*/
protected boolean isSelect(String psql) {
String sql = psql.trim().toUpperCase();
if(sql.indexOf("SELECT") != 0) return false;
return true;
}
/**
*<br>方法說明:
*<br>輸入參數:
*<br>返回類型:
*/
public static void main(String[] arg){
if(arg.length!=3){
System.out.println("use: java StatementConn url username password");
return;
}
url = arg[0];
username=arg[1];
password=arg[2];
demo();
}
/**
*<br>方法說明:演示方法
*<br>輸入參數:
*<br>返回類型:
*/
public static void demo(){
try{
StatementConn oc = new StatementConn();
String sql = "select * from TBL_USER where id>?";
Vector vCondition =new Vector();
vCondition.addElement(new Integer(3));
Vector vResult = (Vector)oc.execute(sql,vCondition);
for(int i=0;i<vResult.size();i++){
System.out.println(vResult.elementAt(i));
}
}catch(Exception e){
System.out.println(e);
}
}
}//end