java JDBC-插入1000條隨機時間

public class Demo8 { public static void main(String[] args) { Connection conn=null; PreparedStatement ps=null; try { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","dyl123"); for(int i=0;i<1000;i++) { ps=conn.prepareStatement("insert into t_user(username,pwd,regTime,lastLoginTime) values(?,?,?,?)"); ps.setObject(1, "html5"+i); ps.setObject(2, "12355"); //插入隨機數 int rand=100000000+new Random().nextInt(100000000); java.sql.Date date=new java.sql.Date(System.currentTimeMillis()-rand); ps.setDate(3, date); java.sql.Timestamp stamp=new java.sql.Timestamp(System.currentTimeMillis()-rand); ps.setTimestamp(4,stamp); ps.execute(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { try { if(null!=ps) { ps.close(); } }catch(SQLException e) { e.printStackTrace(); } try { if(null!=conn) { conn.close(); } }catch(SQLException e) { e.printStackTrace(); } } } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章