学生实体类
public class Student {
// JavaBean
private Integer id;
private String name;
private String gender;
private Integer score;
private String addr;
private String tel;
public Student() {}
public Student(Integer id, String name, String gender, Integer score, String addr, String tel)
{
this.id = id;
this.name = name;
this.gender = gender;
this.score = score;
this.addr = addr;
this.tel = tel;
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
public Integer getScore()
{
return score;
}
public void setScore(Integer score)
{
this.score = score;
}
public String getAddr()
{
return addr;
}
public void setAddr(String addr)
{
this.addr = addr;
}
public String getTel()
{
return tel;
}
public void setTel(String tel)
{
this.tel = tel;
}
@Override
public String toString()
{
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", score=" + score +
", addr='" + addr + '\'' +
", tel='" + tel + '\'' +
'}';
}
}
StudentDemo接口的实现
public class StudentDaoImpl implements StudentDao
{
/**
* 查询数据库数据方法
* @return
*/
@Override
public List<Student> findAll()
{
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Student> arrayList = new ArrayList<>();
try{
//获取链接
conn = JDBCUtils.getConnection();
//书写sql
String sql = "select * from studentmanage";
stmt=conn.prepareStatement(sql);
rs= stmt.executeQuery();
//查询结果集
while(rs.next()){
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setScore(rs.getInt("score"));
student.setAddr(rs.getString("addr"));
student.setTel(rs.getString("tel"));
arrayList.add(student);
}
}catch (SQLException e){
e.printStackTrace();
}finally {
JDBCUtils.close(stmt,conn,rs);
}
return arrayList;
}
/**
* 添加学生数据
* @param student 学生类对象
* @return
*/
@Override
public boolean save(Student student)
{
Connection conn = null;
PreparedStatement stmt = null;
try{
//获取链接
conn=JDBCUtils.getConnection();
//书写sql
String sql = "insert into studentmanage values(null,?,?,?,?,?);";
stmt=conn.prepareStatement(sql);
//给占位符赋值
stmt.setString(1,student.getName());
stmt.setString(2,student.getGender());
stmt.setInt(3,student.getScore());
stmt.setString(4,student.getAddr());
stmt.setString(5,student.getTel());
int row= stmt.executeUpdate();
if(row>0){
//添加成功
return true;
}
}catch (SQLException e){
e.printStackTrace();
}finally {
JDBCUtils.close(stmt,conn, null);
}
return false;
}
/**
* 根据学生id删除数据
* @param id 学生id
* @return
*/
@Override
public boolean remove(Integer id)
{
Connection conn = null;
PreparedStatement stmt = null;
try {
//获取链接
conn =JDBCUtils.getConnection();
//书写sql
String sql="delete from studentmanage where id=?";
stmt=conn.prepareStatement(sql);
//给占位符赋值
stmt.setInt(1,id);
int row=stmt.executeUpdate();
if(row>0){
return true;
}
}catch (SQLException e){
e.printStackTrace();
}finally {
JDBCUtils.close(stmt,conn,null);
}
return false;
}
/**
* 根据学生id,修改学生成绩
* @param student 学生对象
* @return
*/
@Override
public boolean update(Student student)
{
Connection conn = null;
PreparedStatement stmt = null;
try{
//获取链接
conn =JDBCUtils.getConnection();
//书写sql
String sql="update studentmanage set score=? where id=?";
stmt=conn.prepareStatement(sql);
//给占位符赋值
stmt.setInt(1,student.getScore());
stmt.setInt(2,student.getId());
int row=stmt.executeUpdate();
if(row>0){
return true;
}
}catch (SQLException e){
e.printStackTrace();
}finally {
JDBCUtils.close(stmt,conn,null);
}
return false;
}
}