欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-10 13:42:28
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.Timestamp;im ......

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