时间类型

//-----------------Date与-Timestamp-的区别----------------------
package java_272_JDBC_时间处理_Date_Time_Timestamp区别_随机日期生成_练习;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
/**
-
测试时间处理(java.sql.Date.Time,Timestamp)
*/
public class Demo07 {
public static void main(String[] args) {
//声明
Connection conn =null;
PreparedStatement ps1 =null;
PreparedStatement ps2 =null;
try {
//加载驱动类
Class.forName(“com.mysql.jdbc.Driver”);
//连接数据库
conn = DriverManager.getConnection(
“jdbc:mysql://localhost:3306/testjdbc?useSSL=false&characterEncoding=UTF-8”,“root”,“www4152276”); //characterEncoding 字符编码//事务1 ps1 = conn.prepareStatement("insert into t_user(username,pwd,regTime,lastloginTime) values(?,?,?,?)");//注意导入的包名 ps1.setObject(1, "张三"); ps1.setObject(2, "123456"); //创建时间对象 java.sql.Date date = new java.sql.Date(System.currentTimeMillis());//传long类型数值;System.currentTimeMillis()当前时间 ps1.setDate(3, date);//只有年月日 //年月日时分秒 Timestamp stamp = new Timestamp(System.currentTimeMillis());//如果需要插入指定日期,可以使用Calendar或DateFormat字符串转换为时间 ps1.setTimestamp(4, stamp); ps1.execute();//执行 System.out.println("插入一个用户,张三"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { //关闭遵循先进后关原则;一定要分开关闭,这样出现异常不影响后面的程序执行 if(ps2!=null){ try { ps2.close();//关闭 } catch (SQLException e) { e.printStackTrace(); } } if(ps1!=null){ try { ps1.close();//关闭 } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null){ try { conn.close();//关闭 } catch (SQLException e) { e.printStackTrace(); } } }}
}
//---------------------结果------------------------

//------------------随机日期生成-------------------------------
package java_272_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)
*/
public class Demo07 {
public static void main(String[] args) {
//声明
Connection conn =null;
PreparedStatement ps1 =null;
PreparedStatement ps2 =null;
try {
//加载驱动类
Class.forName(“com.mysql.jdbc.Driver”);
//连接数据库
conn = DriverManager.getConnection(
“jdbc:mysql://localhost:3306/testjdbc?useSSL=false&characterEncoding=UTF-8”,“root”,“www4152276”); //characterEncoding 字符编码
//随机时间;1000条生成
for(int i=0;i<1000;i++){
//事务1
ps1 = conn.prepareStatement(“insert into t_user(username,pwd,regTime,lastloginTime) values(?,?,?,?)”);//注意导入的包名
ps1.setObject(1, “张三”+i);//添加i作为名字标识
ps1.setObject(2, “123456”);//定义随机数 int rand = 1000000000+new Random().nextInt(1000000000); //创建时间对象 java.sql.Date date = new java.sql.Date(System.currentTimeMillis()-rand);//传long类型数值;System.currentTimeMillis()当前时间 ps1.setDate(3, date);//只有年月日 //年月日时分秒 Timestamp stamp = new Timestamp(System.currentTimeMillis()-rand);//如果需要插入指定日期,可以使用Calendar或DateFormat字符串转换为时间 ps1.setTimestamp(4, stamp); ps1.execute();//执行 } System.out.println("插入一个用户,张三"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { //关闭遵循先进后关原则;一定要分开关闭,这样出现异常不影响后面的程序执行 if(ps2!=null){ try { ps2.close();//关闭 } catch (SQLException e) { e.printStackTrace(); } } if(ps1!=null){ try { ps1.close();//关闭 } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null){ try { conn.close();//关闭 } catch (SQLException e) { e.printStackTrace(); } } }}
}
//---------------------------结果----------------

本文介绍Java中Date, Time与Timestamp的区别,并通过示例演示如何在JDBC操作中使用这些时间类型,包括创建时间对象、设置SQL语句中的时间参数。此外,还展示了如何生成随机日期并批量插入数据库。


被折叠的 条评论
为什么被折叠?



