JAva JDBC-1000個のランダム時間を挿入

1508 ワード

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();
        }
    }
}
}