Note that there are some explanatory texts on larger screens.

plurals
  1. POJava get output CallableStatement JDBC
    primarykey
    data
    text
    <p>I am newbie in java. I try to make a stored procedure(SP) in SQL SERVER. The SP is: </p> <pre><code>go create procedure sp_calculate @value1 int = 0, @value2 int = 0, @sum int OUTPUT, @multiply int OUTPUT as begin set nocount on set @sum = @value1 + @value2 set @multiply = @value1 * @value2 end </code></pre> <p>I make traint following <a href="https://stackoverflow.com/questions/1947754/getting-the-return-value-from-jdbc-mssql">Getting the Return Value from JDBC MSSQL</a>. So I do it in Java below: </p> <pre><code>public static void main(String[] args) { try (Connection conn = ConnectionUtil.getConnection()) { CallableStatement cst = conn.prepareCall("{ (?, ?) = call sp_calculate ( @value1 =?, @value2=? ) }"); cst.setInt(3, 4);//try some value cst.setInt(4, 10);//try some value cst.registerOutParameter(1, java.sql.Types.INTEGER); cst.registerOutParameter(2, java.sql.Types.INTEGER); cst.executeUpdate(); int sum = cst.getInt(1); int multiply = cst.getInt(2); System.out.println("sum = " + sum + " - product = " + multiply); } catch (SQLException ex) { Logger.getLogger(StoredProcedured.class.getName()).log(Level.SEVERE, null, ex); } } </code></pre> <p>But the result is <code>"com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '{'."</code> Anyone can tell me what error I create?</p> <p>Finally,I work it on out: </p> <pre><code>public static void main(String[] args) { try (Connection conn = ConnectionUtil.getConnection()) { CallableStatement cst = conn.prepareCall("{ call sp_calculate ( ?,?,?,? ) }"); cst.setInt(1, 10);//try some value cst.setInt(2, 4);//try some value cst.registerOutParameter(3, java.sql.Types.INTEGER); cst.registerOutParameter(4, java.sql.Types.INTEGER); cst.executeUpdate(); int sum = cst.getInt(3); int product = cst.getInt(4); System.out.println("sum = " + sum + " - product = " + product); } catch (SQLException ex) { Logger.getLogger(StoredProcedured.class.getName()).log(Level.SEVERE, null, ex); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload