JDBC_时间处理_Date_Time_Timestamp区别_随机日期生成


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Random;

/**
* 测试时间处理(java.sql.Date,Time,Timestamp)

* @author Administrator
*/
public class Demo007 {
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/testjdbc", "root", "");
for (int i = 0; i < 100; i++) {
ps = conn
.prepareStatement("insert into t_user(username,pwd,regTime,lastLoginTime)values(?,?,?,?)");
ps.setObject(1, "高斯" + i);
ps.setObject(2, 666);
int random = 1000 + new Random().nextInt(1000);
java.sql.Date date1 = new java.sql.Date(
System.currentTimeMillis() - random);
Timestamp date2 = new Timestamp(System.currentTimeMillis()
- random);
ps.setDate(3, date1);
ps.setTimestamp(4, date2);
ps.execute();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}


作者:小白qqqqq,发布于:2019/03/01
原文:https://www.cnblogs.com/qhcyp/p/10453455.html