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 }