JDBC中的时间处理

MySQL中常用的时间类有:
java.sql.Date,
Time,
Timestamp
用的比较多的是ava.sql.Date和TimeStamp:

先看表结构

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(10) DEFAULT NULL,
  `pwd` varchar(10) DEFAULT NULL,
  `regTime` date DEFAULT NULL,
  `lastLoginTime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20213 DEFAULT CHARSET=utf8;


JDBC中的时间处理

插入数据:

public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps1 = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			// 建立连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?useSSL=false", "root", "123456");
			// JDBC中默认是true,自动提交事务
			conn.setAutoCommit(false);
			
			ps1 = conn.prepareStatement("insert into t_user(username, pwd, regTime, lastLoginTime) values(?, ?, ?, ?)");
			for (int i = 0; i < 100; i++) {
				ps1.setString(1, "张三" + i);
				ps1.setString(2, "123456");
				int r = 3600*24*30;
				long rand = new Random().nextInt(r);
				ps1.setDate(3, new Date(System.currentTimeMillis() - rand*1000));
				ps1.setTimestamp(4, new Timestamp(System.currentTimeMillis() - rand*1000));
				ps1.execute();
			}
			conn.commit();
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(ps1 != null) {
					ps1.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

取出数据:

package com.xzlf.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * 测试时间处理(java.sql.Date,Time,Timestamp),取出指定时间段的数据
 * @author xzlf
 *
 */
public class Demo08 {
	
	public static long strToDate(String dateStr) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			return format.parse(dateStr).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}
	}

	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			// 建立连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?useSSL=false", "root", "123456");
			ps = conn.prepareStatement("select * from t_user where lastLoginTime>? and lastLoginTime<? order by lastLoginTime");
			Timestamp start = new Timestamp(strToDate("2020-4-18 12:11:01"));
			Timestamp end = new Timestamp(strToDate("2020-4-20 0:00:00"));
			ps.setObject(1, start);
			ps.setObject(2, end);
			rs = ps.executeQuery();
			while(rs.next()) {
				System.out.println(rs.getObject("id") + "--" + rs.getObject("username") + "--" + rs.getObject("lastLoginTime"));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

运行:
JDBC中的时间处理

JDBC中的时间处理

上一篇:JAVA访问数据库工具


下一篇:JDBC 基本操作