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