Java程序和Oracle数据库调用相同的SQL查询语句,结果却不同

package org.programming.student;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class StudentsManagerImpl implements StudentsManager
{
	public ArrayList<Student> getStudentList(java.util.Date beginDate, java.util.Date endDate)
	{
		String sql = 
			"select stu_id, class_name, c.class_id, stu_name, sex, birthday, contactTel, address"+
			" from students stu join class c on stu.class_id = c.class_id "+
			"where stu.birthday between ? and ?";

		Connection conn = null;
		PreparedStatement psmt = null;
		ResultSet res = null;
		ArrayList<Student> stu_list = new ArrayList<Student>();

		try
		{
			conn = DBUtil.getConnection();
			psmt = conn.prepareStatement(sql);
			psmt.setDate(1, new java.sql.Date(beginDate.getTime()));
			psmt.setDate(2, new java.sql.Date(endDate.getTime()));
			res = psmt.executeQuery();

			while(res.next())
			{
				Student student = new Student();
				
				student.setStudentId(res.getString("stu_id"));
				student.setName(res.getString("stu_name"));
				student.setSex(res.getString("sex"));
				student.setBirthday(res.getDate("birthday"));
				student.setContactTel(res.getString("contacttel"));
				student.setAddress(res.getString("address"));

				Clas clas = new Clas();
				clas.setClassName(res.getString("class_name"));
				clas.setClassId(res.getString("class_id"));
				student.setClas(clas);

				stu_list.add(student);
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		
		return stu_list;
	}
	
	public static void main(String[] args) throws ParseException
	{
		StudentsManagerImpl stu_manager = new StudentsManagerImpl();
		
		ArrayList<Student> list = 
				stu_manager.getStudentList(new SimpleDateFormat("yyyy-mm-dd").parse("1988-01-01"), 
						new SimpleDateFormat("yyyy-mm-dd").parse("1992-11-2"));
		
		System.out.println("符合条件的学生人数为:"+list.size());
		
		for(Iterator<Student> iterator = list.iterator();iterator.hasNext();)
		{
			Student stu = iterator.next();
			System.out.println(stu.getName());
		}	
	}
}

在控制台输出的结果:


students整张表:

select * from students order by birthday;



调用和上述Java程序中相同的SQL语句:

alter session set nls_date_format = 'yyyy-mm-dd';
select stu_id, class_name, c.class_id, stu_name, sex, birthday, contactTel, address
from students stu join class c on stu.class_id = c.class_id
where stu.birthday between '1988-01-01' and '1992-11-2';


上述SQL语句在数据库中所得结果:


        在使用用java程序调用时,满足条件的学生人数只有5个,而在Oracle数据库中调用相同的SQL语句却得到6个满足条件的学生(这个是正确的),对比一下,是少了学生“旺财”,这搞的我很郁闷。后来将我的问题发到论坛后,经过大家的指点,最终发现了我问题所在:方法SimpleDateFormat()的日期模式写错了,在类SimpleDateFormat的日期模式中,Month in year应该是大写“M”,而我写成了小写,这样一来就成了Minute in hour。小小的书写错误,竟造成了整个业务逻辑上的错误,真是“差之毫厘谬以千里”啊!大家要引以为戒。
        另外,为什么在这种错误之下还能查询出看似不赖的结果,由于我水平有限的很,自己百思不得其解。还希望大家能多多指教!




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值