/*CREATE TABLE `emp` (
`empno` int(4) NOT NULL AUTO_INCREMENT,
`ename` varchar(25) DEFAULT NULL,
`sal` double(10,2) DEFAULT NULL,
`hiredate` date DEFAULT NULL,
`deptno` int(4) DEFAULT NULL,
PRIMARY KEY (`empno`)
) ENGINE=InnoDB AUTO_INCREMENT=206 DEFAULT CHARSET=latin1
*/
import java.sql.*;
import java.util.*;
import java.io.*;
public class BatchUpdateEx1 {
public static void main(String s[]) throws Exception {
Driver d= (Driver) ( Class.forName(
"com.mysql.jdbc.Driver").newInstance());
Properties p=new Properties ();
p.put("user","root");
p.put("password","admin");
Connection con=d.connect("jdbc:mysql://localhost:3306/test", p);
Statement st=con.createStatement();
//statement1:empno,ename,sal
//st.addBatch("insert into emp(empno,sal,deptno) values("+s[0]+",1000,10)");
st.addBatch("insert into emp(empno,ename,sal,deptno) values(102,'Ashwini',4000,30)");
//statement2
st.addBatch("update emp set sal=2000 where empno=20");
//statement3
st.addBatch("insert into emp(empno,sal,deptno) values(202,1000,10)");
//statement4
st.addBatch("insert into emp(empno,sal,deptno) values(203,1000,10)");
try {
int[] counts=st.executeBatch();
System.out.println("Batch Executed Successfully");
for (int i=0;i<counts.length;i++){
System.out.println("Number of records effected by statement"+(i+1)+": "+counts[i]);
}//for
}//try
catch(BatchUpdateException e){
System.out.println("Batch terminated with an abnormal condition");
int[] counts=e.getUpdateCounts();
System.out.println("Batch terminated at statement"+ (counts.length+1));
for (int i=0;i<counts.length;i++) {
System.out.println("Number of records effected by the statement"+ (i+1)+": "+counts[i]);
}//for
}//catch
con.close();
}//main
}//class
/*
* Batch terminated at statement5
Number of records effected by the statement1: 1
Number of records effected by the statement2: 1
Number of records effected by the statement3: -3
Number of records effected by the statement4: -3
*/
`empno` int(4) NOT NULL AUTO_INCREMENT,
`ename` varchar(25) DEFAULT NULL,
`sal` double(10,2) DEFAULT NULL,
`hiredate` date DEFAULT NULL,
`deptno` int(4) DEFAULT NULL,
PRIMARY KEY (`empno`)
) ENGINE=InnoDB AUTO_INCREMENT=206 DEFAULT CHARSET=latin1
*/
import java.sql.*;
import java.util.*;
import java.io.*;
public class BatchUpdateEx1 {
public static void main(String s[]) throws Exception {
Driver d= (Driver) ( Class.forName(
"com.mysql.jdbc.Driver").newInstance());
Properties p=new Properties ();
p.put("user","root");
p.put("password","admin");
Connection con=d.connect("jdbc:mysql://localhost:3306/test", p);
Statement st=con.createStatement();
//statement1:empno,ename,sal
//st.addBatch("insert into emp(empno,sal,deptno) values("+s[0]+",1000,10)");
st.addBatch("insert into emp(empno,ename,sal,deptno) values(102,'Ashwini',4000,30)");
//statement2
st.addBatch("update emp set sal=2000 where empno=20");
//statement3
st.addBatch("insert into emp(empno,sal,deptno) values(202,1000,10)");
//statement4
st.addBatch("insert into emp(empno,sal,deptno) values(203,1000,10)");
try {
int[] counts=st.executeBatch();
System.out.println("Batch Executed Successfully");
for (int i=0;i<counts.length;i++){
System.out.println("Number of records effected by statement"+(i+1)+": "+counts[i]);
}//for
}//try
catch(BatchUpdateException e){
System.out.println("Batch terminated with an abnormal condition");
int[] counts=e.getUpdateCounts();
System.out.println("Batch terminated at statement"+ (counts.length+1));
for (int i=0;i<counts.length;i++) {
System.out.println("Number of records effected by the statement"+ (i+1)+": "+counts[i]);
}//for
}//catch
con.close();
}//main
}//class
/*
* Batch terminated at statement5
Number of records effected by the statement1: 1
Number of records effected by the statement2: 1
Number of records effected by the statement3: -3
Number of records effected by the statement4: -3
*/
No comments:
Post a Comment