What is Parameterized Batch Update in JDBC? Explain with an example?
Answers
import java. sql . Connection;
import java. sql . Driver Manager;
import java. sql. Prepared Statement;
import java. sql . Statement;
public class Parameterized Batch Update {
public static void main ( String args [ ] ) throws Exception {
String mysql Url = " jdbc : my sql " ;
answering=true & answering Source=great Job % 2 F question Page" ) ;
System. out. print ln ( " Connection established");
Statement stmt = con. create Statement ( ) ;
con. set Auto Commit ( false ) ;
Prepared Statement pstmt = con. prepare Statement ( " INSERT INTO Sales VALUES ") ;
pstmt. set String ( 1, " Key Board " ) ;
pstmt. set String ( 2, " Amith ");
pstmt. set String ( 3, "January");
pstmt. setInt ( 4, 1000 ) ;
pstmt. set String ( 5, " Hyderabad " ) ;
pstmt. add Batch ( ) ;
pstmt. set String ( 1, " Earphones " ) ;
pstmt. set String ( 2, " Sumith ") ;
pstmt. set String ( 3, " March " ) ;
pstmt. set Int ( 4, 500 ) ;
pstmt. set String ( 5, " Vishakha patnam " );
pstmt. add Batch ( ) ;
pstmt. set String ( 1, " Mouse " ) ;
pstmt. set String ( 2, " Sudha " ) ;
pstmt. set String ( 3 , " September " ) ;
pstmt. setInt ( 4, 500 ) ;
pstmt. set String ( 5, " Vishakha patnam " );
pstmt. add Batch ( ) ;
stmt. execute Batch ( ) ;
con.commit ( ) ;
System. out. prinln ( " Records inserted " );
}
}