package model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import dbutil.Dbconn;
import entity.Student;
public class StudentModel {
private static PreparedStatement ps;
private static ResultSet rs;
static Dbconn s=new Dbconn();
public List search(){
List studentlist = null;
String sql = "select * from student";
try {
Connection conn=s.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
studentlist = new ArrayList();
while(rs.next()){
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setSex(rs.getString("sex"));
student.setGrade(rs.getString("grade"));
student.setScore(rs.getFloat("score"));
student.setPath(rs.getString("path"));
studentlist.add(student);
}
s.closeAll(conn,ps,rs);
} catch (Exception e) {
e.printStackTrace();
}
return studentlist;
}
public Student load(Integer id) {
Student student = null;
String sql = "select * from student where student.id = ? ";
try {
Connection conn=s.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, id.intValue());
rs = ps.executeQuery();
if(rs.next()){
student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setGrade(rs.getString("grade"));
student.setScore(rs.getFloat("score"));
student.setPath(rs.getString("path"));
}
s.closeAll(conn,ps,rs);
} catch (Exception e) {
e.printStackTrace();
}
return student;
}
public int update(int id,String name,String sex,int age,String grade,float score,String path){
int a=0;
try {
Connection conn=s.getConnection();
String sql="update student set name=?,sex=?,age=?,grade=?,score=?,path=? where id=?";
ps=conn.prepareStatement(sql);
ps.setInt(7, id);
ps.setString(1, name);
ps.setString(2, sex);
ps.setInt(3,age);
ps.setString(4,grade);
ps.setFloat(5,score);
ps.setString(6, path);
a=ps.executeUpdate();
s.closeAll(conn,ps,rs);
} catch (SQLException e) {
e.printStackTrace();
}
return a;
}
public int insert(int id,String name,String sex,int age,String grade,float score,String path){
int a=0;
try {
Connection conn=s.getConnection();
String sql="insert student values(?,?,?,?,?,?,?)";
ps=conn.prepareStatement(sql);
ps.setInt(1, id);
ps.setString(2, name);
ps.setString(3, sex);
ps.setInt(4,age);
ps.setString(5,grade);
ps.setFloat(6,score);
ps.setString(7, path);
a=ps.executeUpdate();
s.closeAll(conn,ps,rs);
} catch (SQLException e) {
e.printStackTrace();
}
return a;
}
public int delete(int id){
int a=0;
try {
Connection conn=s.getConnection();
String sql="delete from student where student.id=?";
ps=conn.prepareStatement(sql);
ps.setInt(1, id);
a=ps.executeUpdate();
s.closeAll(conn,ps,rs);
} catch (SQLException e) {
e.printStackTrace();
}
return a;
}
/**
* 通过姓名模糊查询
* @param name
* @return
*/
public List<Student> getListBy_Name(String name){
List<Student> studentlist = null;
String sql = "select * from student where name like ?";
try {
Connection connection = s.getConnection();
ps = connection.prepareStatement(sql);
ps.setString(1, "%" + name + "%");
rs = ps.executeQuery();
studentlist = new ArrayList<Student>();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setSex(rs.getString("sex"));
student.setGrade(rs.getString("grade"));
student.setScore(rs.getFloat("score"));
student.setPath(rs.getString("path"));
studentlist.add(student);
}
s.closeAll(connection, ps, rs);
} catch (Exception e) {
e.printStackTrace();
}
return studentlist;
}
}