JDBC之addBatch运用实例:
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class AddBatch {
/**
* @Title: main
* @Description:
* @param:
* @return void
* @user: wangzg
* @Date:2014-7-7
* @throws
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//int[] count = statementAddBatch();
int[] count = preparedStatementAddBatch();
if(count != null){
for(int i : count){
System.out.println(i);
// SUCCESS_NO_INFO -2
// EXECUTE_FAILED -3
}
}
}
/**
*
* @Title: getConnection
* @Description:
* @param:
* @return Connection
* @user: wangzg
* @Date:2014-7-7
* @throws
*/
public static Connection getConnection() throws ClassNotFoundException, SQLException{
Connection conn = null;
//加载oracle驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//连接数据库
String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
String user ="wzg";
String password = "wzg";
conn = DriverManager.getConnection(url, user, password);
return conn;
}
/**
*
* @Title: statementAddBatch
* @Description:
* @param:
* @return int[]
* @user: wangzg
* @Date:2014-7-7
* @throws
*/
public static int[] statementAddBatch(){
Connection conn = null;
Statement stm = null;
int[] count = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
stm = conn.createStatement();
stm.addBatch("insert into t_user(user_id,user_name,user_password) values(TEST_SEQUENCE.NEXTVAL,'wzg1','wzg1')");
stm.addBatch("insert into t_user(user_id,user_name,user_password) values(TEST_SEQUENCE.NEXTVAL,'wzg2','wzg2')");
stm.addBatch("insert into t_user(user_id,user_name,user_password) values(TEST_SEQUENCE.NEXTVAL,'wzg3','wzg3')");
count = stm.executeBatch();
conn.commit();
conn.setAutoCommit(true);
stm.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
/**
*
* @Title: preparedStatementAddBatch
* @Description:
* @param:
* @return int[]
* @user: wangzg
* @Date:2014-7-8
* @throws
*/
public static int[] preparedStatementAddBatch(){
int[] count = null;
Connection conn = null;
PreparedStatement pstm = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
pstm = conn.prepareStatement("insert into t_user(user_id,user_name,user_password) values(TEST_SEQUENCE.NEXTVAL,?,?)");
pstm.setString(1, "lp1");
pstm.setString(2, "lp1");
pstm.addBatch();
pstm.setString(1, "lp2");
pstm.setString(2, "lp2");
pstm.addBatch();
pstm.setString(1, "lp3");
pstm.setString(2, "lp3");
pstm.addBatch();
count = pstm.executeBatch();
conn.commit();
conn.setAutoCommit(true);
pstm.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
}