자바 데이터베이스 조작 일괄 처리

5845 단어
 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 }

좋은 웹페이지 즐겨찾기