java 数据库操作使用批处理

 1 package JDBCDEMOS;
 2 
 3 //import java.net.ConnectException;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.InputStream;
 7 import java.sql.DriverManager;
 8 import java.sql.PreparedStatement;
 9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11 import java.sql.Connection;
12 import java.sql.Statement;
13 
14 import java.sql.Clob;
15 
16 //import com.mysql.jdbc.Statement;
17 
18 public class JDBCDEMO1 {
19     public static final String DBDRIVER = "com.mysql.jdbc.Driver";
20     public static final String DBURL = "jdbc:mysql://localhost:3306/test";
21     public static final String DBUSER = "root";
22     public static final String DBPASS = "123123";
23 
24     public static void main(String[] args) throws Exception {
25         Connection conn=null;
26         PreparedStatement pstmt=null;
27         String sql="INSERT INTO user(name,age)"+"VALUES(?,?)";
28         Class.forName(DBDRIVER);
29         conn=DriverManager.getConnection(DBURL, DBUSER, DBPASS);
30         pstmt=conn.prepareStatement(sql);
31         for(int i=0;i<10;i++){
32             pstmt.setString(1, "赵小蒙 - "+i);
33             pstmt.setInt(2, 20+i);
34             pstmt.addBatch();
35         }
36         int trmp[]=pstmt.executeBatch();
37         System.out.println("更新了"+trmp.length+"条数据。。");
38         pstmt.close();
39         conn.close();
40     }
41 }

 

你可能感兴趣的:(java 数据库操作使用批处理)