2014-11-06 95 views

回答

0

如果某些操作失败,存储过程应返回错误代码,但如果存储过程本身失败,则只能选择捕获SQLException。

+0

Thanks..How处理返回错误代码? – user2156381 2014-11-06 06:09:54

0
try { 
    CallableStatement stmt=con.prepareCall("{call insertR(?,?)}"); 
    stmt.setInt(1,1011); 
    stmt.setString(2,"Amit"); 
    stmt.execute(); 
} catch(SQLException e) { 
    e.printStack(); 
} 
+0

@ user2156381关于如何处理SQLexception – 2015-03-12 17:34:36

0
this piece of code is taken from oracle [docs][1] to answer. 

public void 
    createProcedureShowSuppliers() 
    throws SQLException { 
    String createProcedure = null; 

    String queryDrop = 
     "DROP PROCEDURE IF EXISTS SHOW_SUPPLIERS"; 

    createProcedure = 
     "create procedure SHOW_SUPPLIERS() " + 
     "begin " + 
      "select SUPPLIERS.SUP_NAME, " + 
      "COFFEES.COF_NAME " + 
      "from SUPPLIERS, COFFEES " + 
      "where SUPPLIERS.SUP_ID = " + 
      "COFFEES.SUP_ID " + 
      "order by SUP_NAME; " + 
     "end"; 
    Statement stmt = null; 
    Statement stmtDrop = null; 

    try { 
     System.out.println("Calling DROP PROCEDURE"); 
     stmtDrop = con.createStatement(); 
     stmtDrop.execute(queryDrop); 
    } catch (SQLException e) { 
     JDBCTutorialUtilities.printSQLException(e); 
    } finally { 
     if (stmtDrop != null) 
     { 
      stmtDrop.close(); 
     } 
    } 

    try { 
     stmt = con.createStatement(); 
     stmt.executeUpdate(createProcedure); 
    } catch (SQLException e) { 
     JDBCTutorialUtilities.printSQLException(e); 
    } finally { 
     if (stmt != null) { stmt.close(); } 
    } 
} 


    [1]: https://docs.oracle.com/javase/tutorial/jdbc/basics/storedprocedures.html 
+0

请包括您参考的代码的一些解释,否则您的答案不是您的答案 – jhhoff02 2017-07-05 18:45:47