日期:2014-05-18 浏览次数:20875 次
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());
}
}
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 &n