![](https://i-blog.csdnimg.cn/blog_migrate/cd9d938d679a961f66f0c1561b829a37.png)
![](https://i-blog.csdnimg.cn/blog_migrate/bccf6892fc4be07b1b41825cd100e3f7.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6d831c281391464ec18940180c2233b4.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1c422e9be82a5e67dff420a3292cd8d7.png)
以上为效果图
以下为代码部分
public interface IUserImpl {
// 定义方法
public List<Student> seleAll(String name);
// 查询 通过某些字段进行查询
// Student selectName(String name);
// 添加信息
public int add(Student student);
// 删除方法
public int delete(int id);
public Student selectId(int id);
public int update(Student student);
}
public class UserDao implements IUserImpl {
@Override
public List<Student> seleAll(String name) {
List<Student> list = new ArrayList<Student>();
Connection conn = DBUtil.getConnection();
String sql = "";
try {
if (name != null) {
sql = "select * from student where name like '%" + name + "%'";
} else {
sql = "select * from student";
}
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Student s = new Student();
s.setId(rs.getInt("id"));
s.setName(rs.getString("name"));
s.setAge(rs.getInt("age"));
s.setPhone(rs.getString("phone"));
s.setSchool(rs.getString("school"));
s.setClassName(rs.getString("classname"));
list.add(s);
}
} catch (Exception e) {
// TODO: handle exception
}
return list; // 返回值 切记
}
@Override
public int add(Student student) {
Connection conn = DBUtil.getConnection();
int n = 0;
try {
// 对应数据库五个值
String sql = "insert into student values(?,?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, student.getId());
ps.setString(2, student.getName());
ps.setInt(3, student.getAge());
ps.setString(4, student.getPhone());
ps.setString(5, student.getSchool());
ps.setString(6, student.getClassName());
n = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.close(conn);
return n;
}
@Override
public int delete(int id) {
Connection conn = DBUtil.getConnection();
String sql = "delete from student where id=?";
int n = 0;
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);<