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