JDBC中关于DATE_TIME_TIMESTAMP的使用

来源:互联网 发布:chmod u x mysql.sh 编辑:程序博客网 时间:2024/06/05 15:50

            JDBCTIME_DATE_TIMESTAMP的使用


我们知道Java中对时间日期的处理是以1970年为开始的。并且处理的单位是毫秒。

sql库中,我们知道有三个表示日期的类。Date,sql,TimeStamp;Date只能表示年月日。Time

可以表示时分秒。而TimeStamp可以表示年月日时分秒。

   现在我们来看一个例子:

package com.bjsxt.jdbc;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.sql.Timestamp;

import java.util.Random;

 

 

/**

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

 * @author 高淇www.sxt.cn

 *

 */

public class Demo07 {

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:3306/testjdbc","root","123456");

for(int i=0;i<1000;i++){

ps = conn.prepareStatement("insert into t_user (username,pwd,regTime,lastLoginTime) values (?,?,?,?)");

ps.setObject(1, "高淇"+i);

ps.setObject(2, "123456");

 

int rand =  100000000+new Random().nextInt(1000000000);

/*随机生成的一个日期。*/

java.sql.Date date = new java.sql.Date(System.currentTimeMillis()-rand);

Timestamp stamp = new Timestamp(System.currentTimeMillis()-rand);  //如果需要插入指定日期,可以使用CalendarDateFormat

ps.setDate(3, date);

ps.setTimestamp(4, stamp);

ps.execute();

}

System.out.println("插入一个用户,高淇");

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

if(ps!=null){

ps.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

try {

if(conn!=null){

conn.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

 

0 0
原创粉丝点击