gpt4 book ai didi

java - 在 Java 中调用 SQL Server 存储过程时遇到问题

转载 作者:行者123 更新时间:2023-12-02 00:28:52 24 4
gpt4 key购买 nike

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

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 --> CallableStatement cs = this.con.prepareCall("{?=call "+storedProcName+"("+procParams+") }");

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

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

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

如有任何帮助,我们将不胜感激!提前致谢,

最佳答案

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

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

但随后您使用 this.con,它是您当前正在执行的 StoredProc 对象的 con 字段。从未初始化过,您会得到一个 NullPointerException

关于java - 在 Java 中调用 SQL Server 存储过程时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9530444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com