In
this blog we will know how to insert records in a table using CallableStatement
of JDBC (Java Database Connectivity) in console window.
Here
we use Type-1 driver (JDBC-ODBC bridge)
Creation of dsn(database source name) for Oracle
Start-Control
panel- Administrative Tools- Data Sources (ODBC)-go to system dsn tab-click add
button-select a driver for which you want to set up data source (for Oracle-
Oracle in XE)-select it and click finish-give any name in data source name
textbox-then click ok button.
Note:
- Here Username=system, Password=pintu and Dsn name=dsn1
Ex:-
To insert record into a table by using CallableStatement
Table Creation
create
table employee(empno int,empname varchar(50),sal int)
Stored procedure
Create
or replace procedure addemp(no number,nm varchar,s number)
as
begin
insert
into employee(empno,empname,sal) values(no,nm,s);
end;
callableDemo.java
file
/*To
use CallableStatement */
import
java.sql.*;
import
java.util.*;
public
class callableDemo
{
public
static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:dsn1","system","pintu");
//Step-1
CallableStatement
cstmt=con.prepareCall("call addemp(?,?,?)");
Scanner
sc=new Scanner(System.in);
System.out.print("Enter
the Employee No: ");
int
x=sc.nextInt();
System.out.print("Enter
the Employee Name: ");
String
str=sc.next();
System.out.println("Enter
the Salary: ");
String
j=sc.next();
//Step-2
cstmt.setInt(1,x);
cstmt.setString(2,str);
cstmt.setString(3,j);
//Step
-3
cstmt.execute();
System.out.println("***Procedure
called****");
System.out.println("Record
Sucessfully Inserted");
con.close();
}
}
Compile
Javac
callableDemo.java
Java
callableDemo