How to call database function from java

Published on Author bloggerfiveLeave a comment

Remember database  function can return values but stored procedure can’t .

The following code snippet will clearly guide how to call mysql function which has three params with return type .

Aim – The java code retrives voterId from Voterlist function with given voterName ,voterAge and Address params,

 

               CallableStatement cStmt = connection.prepareCall(“{? = call voterList(?,?,?,?)}”);
        
                cStmt.registerOutParameter(1,java.sql.Types.INTEGER);            
                cStmt.setString(2, voterName);
                cStmt.setString(3, voterAge);
                cStmt.setString(4, Address);
                cStmt.execute();                        
                int voterId = cStmt.getInt(1);
            
                cStmt.close();

 

 

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *