2012-03-02 70 views
0

程序下面是一個通用類我寫了一個調用服務器上的存儲過程:麻煩調用SQL Server存儲在Java

public class StoredProc { 

Connection con = null; 
ResultSet rs = null; 
CallableStatement cs = null; 

public StoredProc(String jdbcResource, String storedProcName){ 

    this(jdbcResource, storedProcName, new String[0], new String[0]); 
} 

public StoredProc(String jdbcResource, String storedProcName, String[] params,String[]  paramTypes){ 

    Connection con = new databaseConnection(jdbcResource).getConnection(); 

    //Get length of parameters and sets stored procs params (?, ?, ...etc) 
    String procParams = ""; 
    int paramSize = params.length; 
    if(paramSize != 0){ 
     for(int i = 0; i < paramSize; i++){ 
      if(i == paramSize){ 
       procParams += "?"; 
      }else{ 
       procParams += "?, "; 
      } 
     } 
    } 

    try{   

     CallableStatement cs = this.con.prepareCall("{?=call "+storedProcName+" ("+procParams+")}"); 

     for(int j = 0; j < params.length; j++){ 
      if (paramTypes[j].equalsIgnoreCase("Int")) { 
       int x = 0; 
       try{ 
        x = Integer.parseInt(params[j]); 
       } catch(Exception e) {} 
       cs.setInt(j, x); 
      } else if (paramTypes[j].equalsIgnoreCase("Boolean")) { 
       boolean x = false; 
       try{ 
        x = (params[j].equalsIgnoreCase("True")) || (params[j].equalsIgnoreCase("T")) || (params[j].equalsIgnoreCase("1")) || (params[j].equalsIgnoreCase("Yes")) || (params[j].equalsIgnoreCase("Y")); 
       } catch(Exception e) {} 
       cs.setBoolean(j, x); 
      } else if (paramTypes[j].equalsIgnoreCase("String")) { 
       cs.setString(j, params[j]); 
      } 
     } 

    }catch(Exception e){ 
     System.out.println("---------------------------------------------"); 
     System.out.println("Problem constructing callableStatement: "+e); 
     System.out.println("---------------------------------------------"); 
    } 

} 

public ResultSet runQuery(){ 

    try{ 
     rs = cs.executeQuery(); 
    }catch(SQLException e){ 
     System.out.println("---------------------------------------------"); 
     System.out.println("Problem executing stored procedure: "+e); 
     System.out.println("---------------------------------------------"); 
    } 
    return rs; 

} 

public void runUpdate(){ 

    try{ 
     cs.executeUpdate(); 
    }catch(SQLException e){ 
     System.out.println("---------------------------------------------"); 
     System.out.println("Problem executing stored procedure: "+e); 
     System.out.println("---------------------------------------------"); 
    } 
} 

} //end of class 

出於某種原因,我上我想行得到一個NullPointerException構造CallableStatement - > CallableStatement cs = this.con.prepareCall(「{?= call」+ storedProcName +「(」+ procParams +「)}」);

的調用語句應該是這樣的,在運行時:

cs = this.con.prepareCall({?=call getUnlinkedDirectdeposits()}); 

的存儲過程在數據庫中調用這個:[DBO] [getUnlinkedDirectdeposits]

任何幫助,將不勝感激! 在此先感謝,

回答

2

您正在使用錯誤的「con」變量。在你的方法,你要初始化的變量(本地的方法)稱爲con

Connection con = new databaseConnection(jdbcResource).getConnection(); 

但你使用this.con,這是你目前正在執行的StoredProc對象的con領域因爲它是。從未初始化,你會得到一個NullPointerException

+0

啊我明白了,非常感謝! – Tiwaz89 2012-03-02 09:33:50

1

您的Connection字段爲空!

您在StoredProc中創建了一個新的Connection實例,而不是將其分配給您班級的con字段。但是,當試圖創建CallableStatement時,您正在使用之前未設置的this.con