package com.dao;
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 com.JavaBean.Student;
import com.utils.DBUtils;
public class StudentDao {
/**
* 所有学生信息
*
* @return
*/
public List<Student> findallStudent() {
List<Student> list = new ArrayList<>();
Connection conn = DBUtils.open();
String sql = "select * from student";
try {
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
student.setSex(rs.getString("sex"));
student.setPlace(rs.getString("place"));
student.setTel(rs.getString("tel"));
list.add(student);
System.out.println("--------StudenDao:"+student);
}
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
/**
* 添加学生
*
* @param student
* @return
*/
public boolean addStudent(Student student) {
String sql = "insert into student (name,age,sex,place,tel) values(?,?,?,?,?)";
Connection conn = DBUtils.open();
try {
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, student.getName());
pst.setString(2, student.getAge());
pst.setString(3, student.getSex());
pst.setString(4, student.getPlace());
pst.setString(5, student.getTel());
int count = pst.executeUpdate();//执行
pst.close();
return count > 0 ? true : false;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 更新学生信息
*
* @param student
* @return
*/
public boolean updateStudent(Student student) {
String sql = "update student set name=?,age=?,sex=?,place=?,tel=? where id=?";
Connection conn = DBUtils.open();
try {
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, student.getName());
pst.setString(2, student.getAge());
pst.setString(3, student.getSex());
pst.setString(4, student.getPlace());
pst.setString(5, student.getTel());
pst.setInt(6,student.getId());
int count = pst.executeUpdate();
pst.close();
return count > 0 ? true : false;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 删除学生信息
*
* @param studentId
* @return
*/
public int deleteStudent(int id) {
String sql = "delete from student where id = ?";
Connection conn = DBUtils.open();
try {
PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1, id);
int count = pst.executeUpdate();//执行
pst.close();
return count > 0 ? 1 : 0;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
/**
* 根据id查询学生
*
* @param id
* @return
*/
public Student getStudentById(int id) {
Connection conn = DBUtils.open();
String sql = "select * from student where id= "+id ;
Student student = new Student();
try {
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
student.setSex(rs.getString("sex"));
student.setPlace(rs.getString("place"));
student.setTel(rs.getString("tel"));
}
System.out.println("FindID----------");
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
/**
* 同Id学生
*
* @param id
* @return
*/
public List<Student> FindStudentById(int id) {
List<Student> list = new ArrayList<>();
Connection conn = DBUtils.open();
String sql = "select* from student where id=?";
try {
PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1, id);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
student.setSex(rs.getString("sex"));
student.setPlace(rs.getString("place"));
student.setTel(rs.getString("tel"));
list.add(student);
System.out.println("---FindAllSameIDStudent");
}
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
/**
* 根据name查询学生
*
* @param name
* @return
*/
public Student getStudentByName(String name) {
Connection conn = DBUtils.open();
String sql = "select * from student where name= "+"name" ;
Student student = new Student();
try {
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
student.setSex(rs.getString("sex"));
student.setPlace(rs.getString("place"));
student.setTel(rs.getString("tel"));
}
System.out.println("FindName----------");
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
/**
* 同姓名的所有学生
*
* @param name
* @return
*/
public List<Student> FindStudentByName(String name) {
List<Student> list = new ArrayList<>();
Connection conn = DBUtils.open();
String sql = "select * from student where name like '%"+name+"%' ";
try {
PreparedStatement pst = conn.prepareStatement(sql);
//pst.setString(1, name);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
student.setSex(rs.getString("sex"));
student.setPlace(rs.getString("place"));
student.setTel(rs.getString("tel"));
list.add(student);
System.out.println("---FindAllSameNameStudent");
}
rs.close()