日期:2014-05-17  浏览次数:20850 次

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

    同样数据查询语句,利用Java程序调用的结果为什么和在Oracle数据库查询的结果不相同?以下是我的Java代码调用的数据库后返回的结果和SQL语句(从java代码拷贝的)及在该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());
}
}



控制台显示的结果:



在oracle数据库中查询的SQL语句:

alter session 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';


查询的结果:
STU_ID       CLASS_NAME CLASS_ID STU_NAME             SEX BIRTHDAY    CONTACTTEL  ADDRESS
------------ ---------- -------- -------------------- --- ----------- ----------- --------