package com.dao;
import com.db.DBHelper;
import com.bean.StudentBean;
import java.util.*;
import java.sql.*;
public class StudentDao {
//验证登录
public String CheckLogin(String username, String password){
String id = null;
String sql="select * from Student where Student_Username='"+username+"' and Student_Password='"+password+"' and Student_State='入住'";
Statement stat = null;
ResultSet rs = null;
Connection conn = new DBHelper().getConn();
try{
stat = conn.createStatement();
rs = stat.executeQuery(sql);
while (rs.next()) {
id = rs.getString("Student_ID");
}
}
catch(SQLException ex){}
return id;
}
//验证密码
public boolean CheckPassword(String id, String password){
boolean ps = false;
String sql="select * from Student where Student_ID='"+id+"' and Student_Password='"+password+"'";
Statement stat = null;
ResultSet rs = null;
Connection conn = new DBHelper().getConn();
try{
stat = conn.createStatement();
rs = stat.executeQuery(sql);
while (rs.next()) {
ps=true;
}
}
catch(SQLException ex){}
return ps;
}
//获取所有列表
public List<StudentBean> GetAllList(String strwhere,String strorder){
String sql="select * from Student";
if(!(isInvalid(strwhere)))
{
sql+=" where "+strwhere;
}
if(!(isInvalid(strorder)))
{
sql+=" order by "+strorder;
}
// System.out.println(sql);
Statement stat = null;
ResultSet rs = null;
Connection conn = new DBHelper().getConn();
List<StudentBean> list=new ArrayList<StudentBean>();
try{
stat = conn.createStatement();
rs = stat.executeQuery(sql);
while(rs.next()){
StudentBean cnbean=new StudentBean();
cnbean.setStudent_ID(rs.getInt("Student_ID"));
cnbean.setStudent_DomitoryID(rs.getInt("Student_DomitoryID"));
cnbean.setStudent_Username(rs.getString("Student_Username"));
cnbean.setStudent_Password(rs.getString("Student_Password"));
cnbean.setStudent_Name(rs.getString("Student_Name"));
cnbean.setStudent_Sex(rs.getString("Student_Sex"));
cnbean.setStudent_Class(rs.getString("Student_Class"));
cnbean.setStudent_State(rs.getString("Student_State"));
list.add(cnbean);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
if (stat != null)
stat.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
//获取列表
public List<StudentBean> GetList(String strwhere,String strorder){
String sql="select * from Student,Domitory,Building where Student_DomitoryID=Domitory_ID and Domitory_BuildingID=Building_ID";
if(!(isInvalid(strwhere)))
{
sql+=" and "+strwhere;
}
if(!(isInvalid(strorder)))
{
sql+=" order by "+strorder;
}
// System.out.println(sql);
Statement stat = null;
ResultSet rs = null;
Connection conn = new DBHelper().getConn();
List<StudentBean> list=new ArrayList<StudentBean>();
try{
stat = conn.createStatement();
rs = stat.executeQuery(sql);
while(rs.next()){
StudentBean cnbean=new StudentBean();
cnbean.setStudent_ID(rs.getInt("Student_ID"));
cnbean.setStudent_DomitoryID(rs.getInt("Student_DomitoryID"));
cnbean.setStudent_Username(rs.getString("Student_Username"));
cnbean.setStudent_Password(rs.getString("Student_Password"));
cnbean.setStudent_Name(rs.getString("Student_Name"));
cnbean.setStudent_Sex(rs.getString("Student_Sex"));
cnbean.setStudent_Class(rs.getString("Student_Class"));
cnbean.setStudent_State(rs.getString("Student_State"));
cnbean.setDomitory_Name(rs.getString("Domitory_Name"));
cnbean.setBuilding_Name(rs.getString("Building_Name"));
cnbean.setDomitory_Type(rs.getString("Domitory_Type"));
cnbean.setDomitory_Number(rs.getString("Domitory_Number"));
cnbean.setDomitory_Tel(rs.getString("Domitory_Tel"));
list.add(cnbean);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
if (stat != null)
stat.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
//获取指定ID的实体Bean
public StudentBean GetAllFirstBean(String strwhere){
String sql="select * from Student where "+strwhere;
Statement stat = null;
ResultSet rs = null;
Connection conn = new DBHelper().getConn();
StudentBean cnbean=new StudentBean();
try{
stat = conn.createStatement();
rs = stat.executeQuery(sql);
if(rs.next()){
cnbean.setStudent_ID(rs.getInt("Student_ID"));
cnbean.setStudent_DomitoryID(rs.getInt("Student_DomitoryID"));
cnbean.setStudent_Username(rs.getString("Student_Username"));
cnbean.setStudent_Password(rs.getString("Student_Password"));
cnbean.setStudent_Name(rs.getString("Student_Name"));
cnbean.setStudent_Sex(rs.getString("Student_Sex"));
cnbean.setStudent_Class(rs.getString("Student_Class"));
cnbean.setStudent_State(rs.getString("Student_State"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
if (stat != null)
stat.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return cnbean;
}
//获取指定ID的实体Bean
public StudentBean GetFirstBean(String strwhere){
String sql="select * from Student,Domitory,Building where Student_DomitoryID=Domitory_ID and Domitory_BuildingID=Building_ID and "+strwhere;
Statement stat = null;
ResultSet rs = null;
Connection conn = new DBHelper().getConn();
StudentBean cnbean=new StudentBean();
try{
stat = conn.createStatement();
rs = stat.executeQuery(sql);
if(rs.next()){
cnbean.setStudent_ID(rs.getInt("Student_ID"));
cnbean.setStudent_DomitoryID(rs.getInt("Student_DomitoryID"));
cnbean.setStudent_Username(rs.getString("Student_Username"));
cnbean.setStudent_Password(rs.getString("Student_Password"));
cnbean.setStudent_Name(rs.getString("Student_Name"));
cnbean.setStudent_Sex(rs.getString("Student_Sex"));
cnbean.setStudent_Class(rs.getString("Student_Class"));
cnbean.setStudent_State(rs.getString("Student_State"));
cnbean.setDomitory_Name(rs.getString("Domitory_Name"));
cnbean.setBuilding_Name(rs.getString("Building_Name"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
if (stat != null)
stat.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return cnbean;
}
//获取指定ID的实体Bean
public StudentBean GetAllBean(int id){
String sql="select * from Student where Student_ID="+id;
Statement stat = null;
ResultSet rs = null;
Connection conn = new DBHelper().getConn();
StudentBean cnbean=new StudentBean();
try{
stat = conn.createStatement();
rs = stat.executeQuery(sql);
while(rs.next()){
cnbean.setStudent_ID(rs.getInt("Student_ID"));
cnbean.setStudent_DomitoryID(rs.getInt("Student_DomitoryID"));
cnbean.setStudent_Username(rs.getString("Student_Username"));
cnbean.setStudent_Password(rs.getString("Student_Password"));
cnbean.setStudent_Name(rs.getString("Student_Name"));
cnbean.setStudent_Sex(rs.getString("Student_Sex"));
cnbean.setStudent_Class(rs.getString("Student_Class"));
cnbean.setStudent_State(rs.getString("Student_State"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
if (stat != null)
stat.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();