JDBC

JDBC Projects

JDBC Project 1

JDBC Using PreparedStatement example–Batch Update
Previous Home Next
/* This example shows you how to insert few records 
in batch process via JDBC Preparedstatement.*/

package r4r;
import java.sql.*;
import java.util.*;
public class BatchProgram
{
public static void main(String[] args) 
{
try
{
Scanner in=new Scanner(System.in);
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement stmt=con.prepareStatement("insert 
into user5 values(?,?,?,?)");
while(true)
{
System.out.println("Enter name:-");
String n=in.nextLine();
System.out.println("Enter id:-");
int i=in.nextInt();
in.nextLine();
System.out.println("Enter city:-");
String c=in.nextLine();
System.out.println("Enter sex:-");
String se=in.nextLine();
stmt.setString(1, n);
stmt.setInt(2, i);
stmt.setString(3, c);
stmt.setString(4, se);
stmt.addBatch();
System.out.println("Want to save more object,YES/NO");
String ans=in.nextLine();
if(ans.equals("no"))
break;
}
stmt.executeBatch();
System.out.println("successfully added batch");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

 

output:

Enter name:-
arpit
Enter id:-
15
Enter city:-
bombay
Enter sex:-
m
Want to save more object,YES/NO
yes
Enter name:-
amit
Enter id:-
16
Enter city:-
delhi
Enter sex:-
m
Want to save more object,YES/NO
no
successfully added batch
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: