无法在Java中调用SQL Server存储过程

时间:2012-03-02 08:57:02

标签: java sql-server stored-procedures

下面是我编写的一个通用类,它调用服务器上的存储过程:

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

由于某些原因我在我正在尝试构建CallableStatement的行上得到NullPointerException - &gt; CallableStatement cs = this.con.prepareCall(“{?= call”+ storedProcName +“(”+ procParams +“)}”);

可调用语句在运行时应该如下所示:

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

存储过程在数据库中被称为:[dbo]。[getUnlinkedDirectdeposits]

任何帮助将不胜感激! 提前谢谢,

2 个答案:

答案 0 :(得分:2)

您使用了错误的“con”变量。在您的方法中,您正在初始化一个名为con的变量(方法的本地变量):

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

但是你使用this.con,这是你当前正在执行的con对象的StoredProc字段。由于它从未初始化,你得到{{1} }。

答案 1 :(得分:1)

您的Connection字段为空!

您在Connection中创建了一个新的StoredProc个实例,而不是将其分配到您班级的con字段。但是,在尝试创建CallableStatement时,您正在使用之前未设置的this.con