Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > JDBC Code Examples

Callable statement example

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
Callable statement example public class CallableStmt { public static void main(String args[]) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:uma","kworker","kworker"); //calling a stored procedure with no input/output param /* CREATE PROCEDURE HELLOWORLD AS SELECT 'HELLOWORLD' AS HELLO */ CallableStatement cs1 = con.prepareCall("{call HelloWorld}"); ResultSet rs1 = cs1.executeQuery(); while(rs1.next()) { String one = rs1.getString("HELLO"); System.out.println(one); } //Calling a stored procedure which takes in 2 parameters for addition /* --EXECUTE ADDITION 10,25,NULL ALTER PROCEDURE ADDITION @A INT , @B INT , @C INT OUT AS SELECT @C = @A + @B */ CallableStatement cs2 = con.prepareCall("{call ADDITION(?,?,?)}"); cs2.registerOutParameter(3,java.sql.Types.INTEGER); cs2.setInt(1,10); cs2.setInt(2,25); cs2.execute(); int res = cs2.getInt(3); System.out.println(res); //Another way /* --create table test(slno int,ques varchar(100),ans text) --EXECUTE fetchRec 1 create procedure fetchRec @A int as select * from test where [email protected] */ CallableStatement cs3 = con.prepareCall("{call fetchRec(?)}"); cs3.registerOutParameter(1,java.sql.Types.INTEGER); cs3.setInt(1,5); ResultSet rs3 = cs3.executeQuery(); while(rs3.next()) { String ques = rs3.getString(2); String ans = rs3.getString(3); System.out.println(ques); System.out.println(ans); } } catch(Exception e) { e.printStackTrace(); } } }