我有一个存储过程.. 我想在java中调用该过程。 但是,结果没有出来..错误
显示java.lang.NullPointerException
如何在java中调用informix存储过程。 请帮我.. 这是我的代码示例..
存储过程
create procedure tryBaru_Procedure(v_name varchar(50),v_city varchar(20),out v_id int)
select id
into v_id
from tryBaru
where name=v_name;
update tryBaru
set city=v_city
where id=v_id;
end procedure;
爪哇
public class TryBaru_Controller {
Connection conn;
DBConnection dbConn = new DBConnection();
public tryBaru p(tryBaru tryje) throws Exception
{
conn = dbConn.getConnection();
//prepare call store procedure
CallableStatement cs = conn.prepareCall("{ call tryBaru_Procedure(?,?,?) }");
cs.setString(1, tryje.getName());
cs.setString(2, tryje.getCity());
cs.registerOutParameter(3, Types.INTEGER);
cs.executeQuery();
tryje.setId(cs.getInt(3));
cs.close();
conn.close();
return tryje;
}
}
主要
public static void main(String[] args){
// TODO Auto-generated method stub
TryBaru_Controller tbc = new TryBaru_Controller();
tryBaru tb = new tryBaru();
String name1 = "Faridah";
String city1 = "Johor";
tb.setName(name1);
tb.setCity(city1);
try {
tbc.p(tb);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(tb.getName());
System.out.println(tb.getCity());
System.out.println(tb.getId());
}
答案 0 :(得分:1)
你没有包括堆栈跟踪,所以很难说发生了什么。
从您的评论中,您似乎怀疑是Informix JDBC驱动程序。我用JDBC 4.10.JC2DE尝试了类似的代码,它似乎工作。有Jython代码:
"""
create procedure tryBaru_Procedure(v_name varchar(50),v_city varchar(20),out v_id int)
let v_id = 10;
end procedure;
"""
def test_call(db_url, usr, passwd):
try:
print("\n\n%s\n--------------\ncall..." % (db_url))
db = DriverManager.getConnection(db_url, usr, passwd)
proc = db.prepareCall("{ call tryBaru_Procedure(?, ?, ?) }");
proc.setString(1, 'v_name')
proc.setString(2, 'v_city')
proc.registerOutParameter(3, Types.VARCHAR)
proc.executeUpdate();
v_id = proc.getInt(3)
print('v_id: %s' % (v_id))
db.close()
except:
print("there were errors!")
s = traceback.format_exc()
sys.stderr.write("%s\n" % (s))
按预期打印10
。
所以也许您使用旧版本的Informix JDBC驱动程序会出现一些错误,但我认为dbConn
或conn
变量可能存在问题。您确定已连接到数据库吗?
编辑:
如果您只想拥有一个连接(Singleton),请尝试以下方法:
public Connection getConnection() throws Exception {
/* Define the database to be used, the driver,
* the connection url, and the username and
* password to connect the database
*/
if (connection == null) {
driver = "com.informix.jdbc.IfxDriver";
Class.forName(driver);
connectionURL = "jdbc:informix-sqli://127.0.0.1:9090/barubuat:informixserver=lo_informix1210;user=informix;password=abc123";
connection = DriverManager.getConnection(connectionURL);
}
return connection;
}