我尝试从Java调用PL / SQL过程。我使用此代码从数据库中选择/插入值,但是当我尝试使用此查询时,它只是加载而没有完成。我在SQL Developer中测试了此命令,它有效。
@Override
public void save(Transactions transactions)
{
CallableStatement callStmt = null;
String query = "{CALL CON_API_PKG.createTransaction(?,?,?)}";
Connection con = null;
//PreparedStatement ps = null;
try{
setDataSource();
con = dataSource.getConnection();
callStmt = con.prepareCall(query);
callStmt.setLong(1,transactions.getProductId());
callStmt.setLong(2, transactions.getReporterId());
if(transactions.getNote().length() > 0){
callStmt.setString(3, transactions.getNote());
}else{
callStmt.setString(3, null);
}
callStmt.executeUpdate();
/* ps = con.prepareStatement(query);
ps.setLong(1, transactions.getProductId());
ps.setLong(2,transactions.getReporterId());
if(transactions.getNote().length() > 0)
{
ps.setString(3, transactions.getNote());
}
else ps.setString(3, null);
int out = ps.executeUpdate();
*/ int out = callStmt.executeUpdate();
if(out !=0){
System.out.println("Transaction saved for product_id="+transactions.getProductId());
}else System.out.println("Transaction save failed for product_id="+transactions.getProductId());
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
callStmt.close();
// ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
使用可调用语句,并使用如下方括号
CallableStatement callStmt = null;
String query = "{call CON_API_PKG.createTransaction(?,?,?)}";
callStmt = conn.prepareCall(query);
callStmt.setLong(1, 1000);
callStmt.setLong(2, "mkyong");
if(transactions.getNote().length() > 0){
callStmt.setLong(3, "system");
}else{
callStmt.setString(3, null);
}
callStmt.executeUpdate();