1 package demo; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.SQLException; ... ...
1 package demo; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.SQLException; 7 8 public class Insert { 9 //聲明一個Connection連接對象 10 public Connection conn = null ; 11 //聲明一個PreparedStatemente語句對象 12 public PreparedStatement psta = null ; 13 //聲明一個SQL字元串 14 public String sql = null ; 15 //聲明驅動字元串 16 public String driver = "com.mysql.jdbc.Driver" ; 17 //聲明連接字元串 18 public String url = "jdbc:mysql://rm-uf6394r31pw7th97lo.mysql.rds.aliyuncs.com:3306/foton_bas5_ip" ; 19 //聲明資料庫用戶名 20 public String user = "ekingwin" ; 21 //聲明資料庫密碼 22 public String pwd = "Ekingwin123" ; 23 public void jdbc(){ 24 //載入資料庫驅動 25 try { 26 Class.forName(driver) ; 27 } catch (ClassNotFoundException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 System.out.println("資料庫驅動載入失敗!!!"); 31 } 32 33 //連接資料庫 34 try { 35 conn = DriverManager.getConnection(url, user, pwd) ; 36 } catch (SQLException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 System.out.println("資料庫連接失敗!!!"); 40 } 41 try { 42 //添加數據信息 43 for(int i=1;i<=1000;i++){ 44 String sql = "INSERT INTO bas_foton_purchase (id,year,companycode,companydesc,factorycode" 45 + ",factorydesc,onemateial,onemateialname,twomateial,twomateialname," 46 + "threemateial,threemateialname,januaryamount,februaryamount,marchamount" 47 + ",aprilamount,mayamount,juneamount,julyamount,augustamount,seqtemberamount" 48 + ",octoberamount,novemberamount,decemberamount,amount,empuid) VALUES(?,?,?,?,?,?,?,?,?,?,?" 49 + ",?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 50 51 52 53 psta = conn.prepareStatement(sql); 54 psta.setInt(1,i); 55 psta.setString(2, "year"); 56 psta.setString(3, "245252"); 57 psta.setString(4, "245252"); 58 psta.setString(5, "245252"); 59 psta.setString(6, "245252"); 60 psta.setString(7, "245252"); 61 psta.setString(8, "245252"); 62 psta.setString(9, "245252"); 63 psta.setString(10, "245252"); 64 psta.setString(11, "245252"); 65 psta.setString(12, "245252"); 66 psta.setString(13, "245252"); 67 psta.setString(14, "245252"); 68 psta.setString(15, "245252"); 69 psta.setString(16, "245252"); 70 psta.setString(17, "245252"); 71 psta.setString(18, "245252"); 72 psta.setString(19, "245252"); 73 psta.setString(20, "245252"); 74 psta.setString(21, "245252"); 75 psta.setString(22, "245252"); 76 psta.setString(23, "245252"); 77 psta.setString(24, "245252"); 78 psta.setString(25, "245252"); 79 psta.setString(26, "lllllllllll"); 80 81 82 //通過語句對象實現添加操作,該方法返回一個影響幾行數據的整形值 83 int num = psta.executeUpdate(); 84 85 if(num>0){ 86 System.out.println("數據操作成功!!!!"+i); 87 } 88 else{ 89 System.out.println("數據操作失敗!!!!!"); 90 } 91 } 92 93 94 //通過語句對象實現添加操作,該方法返回一個影響幾行數據的整形值 95 96 } catch (SQLException e) { 97 // TODO Auto-generated catch block 98 e.printStackTrace(); 99 System.out.println("資料庫操作數據失敗。。。。"); 100 } 101 102 //4、關閉資料庫操作對象 103 try { 104 psta.close(); 105 conn.close(); 106 } catch (SQLException e) { 107 // TODO Auto-generated catch block 108 e.printStackTrace(); 109 } 110 } 111 112 public static void main(String[] args) { 113 // TODO Auto-generated method stub 114 Insert test = new Insert(); 115 test.jdbc(); 116 } 117 118 }