Callable statements in Java Programming Languages
In this examples, we are going to development callable statement in java Programming language, it's very important because we use the JDBCODBC driver in this program. If you any query about that, feel free to comment below.
Before, we are going to make the program, just click on every Advertisements, available on this page, Dear visitor , students, follower's you know java is not a simple programming language, I spend my whole time to develop easy example for better understanding java Programming. I want nothing to you, so do a few click on the Advertisements.
Now, Let's Go to Make the Program.
import java.sql.*;
//import javax.sql.*;
public class CallableStmt
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:uma","sa","123456");
//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 30,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,40);
cs2.setInt(2,55);
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 slno=@A
*/
CallableStatement cs3 = con.prepareCall("{call fetchRec(?)}");
cs3.registerOutParameter(1,java.sql.Types.INTEGER);
cs3.setInt(1,2);
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();
}
}
}
Compile And Run this Program...
If this information is important to you , and you want to , I will continue writing some more details of Java, Android Programming Language, so click on the advertising, available on this page. This is motivate me for writing some more useful stuff, Thank's
Comments
Post a Comment