日期:2014-05-16 浏览次数:20666 次
begin insert into zb values (zb_seq.nextval,'aa'); insert into mxb values (mxb_seq.nextval, zb_seq.currval, 'bb'); insert into mxb values (mxb_seq.nextval, zb_seq.currval, 'bb'); insert into mxb values (mxb_seq.nextval, zb_seq.currval, 'bb'); insert into zb values (zb_seq.nextval,'cc'); insert into mxb values (mxb_seq.nextval, zb_seq.currval, 'dd'); insert into mxb values (mxb_seq.nextval, zb_seq.currval, 'dd'); insert into mxb values (mxb_seq.nextval, zb_seq.currval, 'dd'); end;
insert into zb values (zb_seq.nextval,?); insert into mxb values (mxb_seq.nextval, zb_seq.currval, ?);
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class testBatch {
public void execBatchSql(){
Connection conn = getConnection();
try{
conn.setAutoCommit(false);
long beginTime = System.currentTimeMillis();
PreparedStatement pst = null;
for (int i=0;i<10000;i++){
pst = conn.prepareStatement("insert into zb values (zb_seq.nextval,?)");
pst.setString(1, "testzb"+i);
pst.addBatch();
pst = conn.prepareStatement("insert into mxb values (mxb_seq.nextval, zb_seq.currval, ?)");
for(int j=7;j<10;j++){
pst.setString(1, "testmxb"+i+j);
pst.addBatch();
}
if(i%1000 == 0){
pst.executeBatch();
conn.commit();
pst.clearBatch();
}
}
pst.executeBatch();
long endTime = System.currentTimeMillis();
System.out.println("用时:"+(endTime-beginTime)+"毫秒");
pst.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
public Connection getConnection(){
Connection conn = null;
try{
String driverClassName = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@10.40.6.68:1521:jszx";
String user = "fszg";
String password = "fszg";
Class cc = Class.forName(driverClassName);
Driver driver = (Driver)cc.newInstance();
DriverManager.registerDriver(driver);
conn = DriverManager.getConnection(url,user,password);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static void main(String[] agrs){
testBatch t = new testBatch();
t.execBatchSql();
}
}
create table zb ( guid number(20), name varchar2(20) ); create table mxb ( guid number(20), f_guid number(20), name varchar2(20) ); -- Create sequence create sequence zb_seq minvalue 3304000000000000001 maxvalue 9999999999999999999 start with 3304000000000000001 increment by 1 cache 20; -- Create sequence create sequence mxb_seq minvalue 3306000000000000001 maxvalue 9999999999999999999 start with 3306000000000000001 increment by 1 cache 20;
java.sql.BatchUpdateException: ORA-08002: sequence ZB_SEQ.CURRVAL is not yet defined in this session at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:459) at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:3907) at cn.com.gsoft.fszg.testBatch.execBatchSql(testBatch.java:26) at cn.com.gsoft.fszg.testBatch.main(testBatch.java:63)