import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import com.neusoft.java275.dao.DBUtil;
public class Batch
{
public static void main(String[] args)
{
Connection con = null;
Statement stmt = null;
PreparedStatement prestmt=null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.137.23:1521:unieap", "scott", "tiger");
stmt = con.createStatement();
for(int i=1;i<=10;i++)
{
stmt.addBatch("insert into student(id,name) values(student_seq.nextval,'张阳_"+i+"')");
}
int[] code=stmt.executeBatch();
System.out.println("受影响的行数:"+code);
prestmt=con.prepareStatement("insert into student(id,name) values(student_seq.nextval,?)");
for(int i=11;i<=20;i++)
{
prestmt.setString(1, "张阳_"+i);
prestmt.addBatch();
}
code=prestmt.executeBatch();
System.out.println("受影响的行数:"+code);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
DBUtil.closeQuietly(stmt);
DBUtil.closeQuietly(prestmt);
DBUtil.closeQuietly(con);
}
}
}
JDBC批处理
最新推荐文章于 2025-01-21 20:44:57 发布