java jdbc按时间查询数据库内容

传参是Util.Date 查询 数据库中内容

sql.Date 实际上是 Util.Date的一个子类,可以把Util.Date强制类型转化为sql.Date作为查询参数

 

package team.heroes.sqlcon.dateQuery;
//要写几个查询函数
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.*;
import team.heroes.sqlcon.ConnectionPool;

public class TaskDateQuery {
	static public List QueryBeansByDate(Date StartTime,Date EndTime) throws SQLException{
		Connection conn = ConnectionPool.getConnection();
		//时间Between查询
		String sql = "SELECT ID_,TASK_DEF_KEY_,NAME_,ASSIGNEE_,START_TIME_,END_TIME_ FROM ACT_HI_TASKINST WHERE (START_TIME_ BETWEEN ? AND ?) AND (END_TIME_ BETWEEN ? AND ?)";
		PreparedStatement ptmt = conn.prepareStatement(sql);
    	ptmt.setDate(1,  new java.sql.Date(StartTime.getTime()));
    	ptmt.setDate(2,   new java.sql.Date(EndTime.getTime()));
    	ptmt.setDate(3,  new java.sql.Date(StartTime.getTime()));
    	ptmt.setDate(4,   new java.sql.Date(EndTime.getTime()));
    	ResultSet ans = ptmt.executeQuery();
    	List QueryAns = new ArrayList<>();
    	while(ans.next()) {
    		String id = ans.getString(1);
    		String task_def_key=ans.getString(2);
    		String name = ans.getString(3);
    		String assignee = ans.getString(4);
    		Date starttime= ans.getDate(5);
    		Date endtime = ans.getDate(6);
    		QueryAns.add(new HiTaskDateBean(id, task_def_key, name, assignee, starttime, endtime));
    	}
    	ans.close();
    	ptmt.close();
    	conn.close();
    	return QueryAns;
	}
	/*public static void main(String args[]) throws ParseException, SQLException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date st = sdf.parse("2018-08-19");
		Date et = sdf.parse("2018-08-21");
		List T = QueryBeansByDate(st, et);
		
		//Between a ,b 是>=a且

 

你可能感兴趣的:(java,mysql)