繁体   English   中英

通过Java调用PL / SQL过程

[英]Call PL/SQL procedure by java

我尝试从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();
        }
    }
}

使用可调用语句,并使用如下括号

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM