getting out parameter from mysql stored procedure in java

后端 未结 2 1794
萌比男神i
萌比男神i 2021-01-15 02:08

I am having problem retrieving OUT parameter from mysql stored procedure in java.

CALL proc_after_topic_add(\'newtest\',@result);
SELECT @result;


        
相关标签:
2条回答
  • 2021-01-15 02:33

    Error in this line

     cstmt.registerOutParameter(2, java.sql.Types.INTEGER);
    

    change like this

     String sql = "CALL proc_after_topic_add(?,?);";
     cstmt.registerOutParameter(2, java.sql.Types.INTEGER);
    
    0 讨论(0)
  • 2021-01-15 02:50
    Create a schema test and create a table employee in schema with 2 columns.  
    id and employeename and insert some data.
    
    Use this Stored Procedure.
    DELIMITER $$
    DROP PROCEDURE IF EXISTS `test`.`get_count_name1` $$
    CREATE PROCEDURE  `test`.`get_count_name1`(IN the_name VARCHAR(64),OUT     
    the_count INT)
    
    BEGIN
    SELECT COUNT(*) INTO the_count from employee where employeename=the_name;
    END$$
    DELIMITER ;
    

    Use this example. username and password are root and root for me. change as per your
    requirement. Here i am counting the occurence of employeename="deepak"

    import java.sql.*;
    public class JDBCExample {
       // JDBC driver name and database URL
       static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
       static final String DB_URL = "jdbc:mysql://localhost/test";
    
       //  Database credentials
       static final String USER = "root";
       static final String PASS = "root";
    
       public static void main(String[] args) {
       Connection conn = null;
       CallableStatement stmt = null;
       try{
          //STEP 2: Register JDBC driver
          Class.forName("com.mysql.jdbc.Driver");
    
          //STEP 3: Open a connection
          System.out.println("Connecting to database...");
          conn = DriverManager.getConnection(DB_URL,USER,PASS);
    
          //STEP 4: Execute a query
          System.out.println("Creating statement...");
          String sql = "{call get_count_name1 (?, ?)}";
          stmt = conn.prepareCall(sql);
    
          //Bind IN parameter first, then bind OUT parameter
          String name = "Deepak";
          stmt.setString(1, name); // This would set ID as 102
          // Because second parameter is OUT so register it
          stmt.registerOutParameter(2, java.sql.Types.INTEGER);
    
          //Use execute method to run stored procedure.
          System.out.println("Executing stored procedure..." );
          stmt.execute();
    
         int count=stmt.getInt(2);
         System.out.println(count);
          stmt.close();
          conn.close();
       }catch(SQLException se){
          //Handle errors for JDBC
          se.printStackTrace();
       }catch(Exception e){
          //Handle errors for Class.forName
          e.printStackTrace();
       }finally{
          //finally block used to close resources
          try{
             if(stmt!=null)
                stmt.close();
          }catch(SQLException se2){
          }// nothing we can do
          try{
             if(conn!=null)
                conn.close();
          }catch(SQLException se){
             se.printStackTrace();
          }//end finally try
       }//end try
       System.out.println("Goodbye!");
    }//end main
    }//end JDBCExample
    
    0 讨论(0)
提交回复
热议问题