package com.cn;
import java.sql.*;
import java.util.*;
public class StuDAOImpl implements StuDAO {
public void delete(String id) throws Exception {
String sql="delete from stu where stu_id=?";
PreparedStatement pstm=null;
DataBaseConnection dbc=null;
try{
dbc=new DataBaseConnection();
pstm=dbc.getConnection().prepareStatement(sql);
pstm.setString(1,id);
pstm.executeUpdate();
pstm.close();
}catch(Exception e){
throw new Exception("操作出现异常!");
}finally{
dbc.close();
}
}
public void insert(Stu stu) throws Exception {
String sql = "insert into stu(stu_id,stu_name,stu_password,stu_age,stu_email) values(?,?,?,?,?)";
PreparedStatement pstm=null;
DataBaseConnection dbc=null;
try{
dbc=new DataBaseConnection();
pstm = dbc.getConnection().prepareStatement(sql);
pstm.setString(1,stu.getStu_id()) ;
pstm.setString(2,stu.getStu_name()) ;
pstm.setString(3,stu.getStu_password()) ;
pstm.setInt(4,stu.getStu_age()) ;
pstm.setString(5,stu.getStu_email()) ;
pstm.executeUpdate() ;
pstm.close() ;
}
catch (Exception e)
{
throw new Exception("操作出现异常") ;
}
finally
{
dbc.close() ;
}
}
public boolean isLogin(Stu stu) throws Exception {
boolean flag = false;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null;
DataBaseConnection dbc = null;
dbc = new DataBaseConnection();
sql= "select stu_name from stu where stu_name=? and stu_password=?";
try{
pstmt = dbc.getConnection().prepareStatement(sql);
pstmt.setString(1,stu.getStu_name());
pstmt.setString(2,stu.getStu_password());
rs = pstmt.executeQuery();
if(rs.next())
{
flag = true;
stu.setStu_name(rs.getString(1));
}
rs.close();
pstmt.close();
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
dbc.close() ;
}
return flag;
}
public Stu querById(String id) throws Exception {
Stu stu = null ;
String sql = "select * from stu where stu_id=?";
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
try
{
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,id) ;
ResultSet rs = pstmt.executeQuery() ;
if(rs.next())
{
stu = new Stu() ;
stu.setStu_id(rs.getString(1));
stu.setStu_name(rs.getString(2));
stu.setStu_password(rs.getString(3));
stu.setStu_age(rs.getInt(4));
stu.setStu_email(rs.getString(5));
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作出现异常") ;
}
finally
{
dbc.close() ;
}
return stu ;
}
public List queryAll() throws Exception {
List all = new ArrayList() ;
String sql = "select * from stu" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
try
{
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
ResultSet rs = pstmt.executeQuery() ;
while(rs.next())
{
Stu stu= new Stu() ;
stu.setStu_id(rs.getString(1));
stu.setStu_name(rs.getString(2));
stu.setStu_password(rs.getString(3));
stu.setStu_age(rs.getInt(4));
stu.setStu_email(rs.getString(5));
all.add(stu) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作出现异常") ;
}
finally
{
dbc.close() ;
}
return all ;
}
public List queryByLike(String cond) throws Exception {
List all = new ArrayList() ;
String sql = "select * from stu where stu_name like ? or stu_email like ?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
try
{
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,"%"+cond+"%") ;
pstmt.setString(2,"%"+cond+"%") ;
ResultSet rs = pstmt.executeQuery() ;
while(rs.next())
{
Stu stu = new Stu() ;
stu.setStu_id(rs.getString(1));
stu.setStu_name(rs.getString(2));
stu.setStu_password(rs.getString(3));
stu.setStu_age(rs.getInt(4));
stu.setStu_email(rs.getString(5));
all.add(stu) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作出现异常") ;
}
finally
{
dbc.close() ;
}
return all ;
}
public void update(Stu stu) throws Exception {
String sql = "update stu set stu_name=?,stu_password=?,stu_age=?,stu_email=? where stu_id=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
try
{
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,stu.getStu_name()) ;
pstmt.setString(2,stu.getStu_password()) ;
pstmt.setInt(3,stu.getStu_age()) ;
pstmt.setString(4,stu.getStu_email()) ;
pstmt.setString(5,stu.getStu_id()) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作出现异常") ;
}
finally
{
dbc.close() ;
}
}
}