javaweb学生管理系统-----管理用户的实现
一、管理用户类的实现
public class StuDao {
//增加用户
public String addstudent(Student student) {
String info = "success";
String sql = "insert into students values(?,?,?,?,?,?,?)";
//执行更新语句
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
if(!check(student)) {
//设置参数
ps.setString(1, student.getId());
ps.setString(2, student.getName());
ps.setInt(3, student.getAge());
ps.setString(4, student.getSex());
ps.setInt(5, student.getCore());
ps.setString(6, student.getUserName());
ps.setString(7, student.getPassword());
//执行更新语句
int r = ps.executeUpdate();
if (r <= 0) info = "fail";
System.out.println("addstudent()...");
}else {
info="fail";
}
}catch(SQLException e){
e.printStackTrace();
} finally{
// 关闭数据库连接
DBUtil.close(null,ps,conn);
}
return info;
}
//用户是否在数据库中,对用户输入的用户数据进行检查。
public boolean check(Student student) {
boolean ret = false;
String sql = "select count(*) from students where id=? and username=? and password=?";
//执行查询语句
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//获得连接
conn = DBUtil.getConnection();
//执行查询
ps = conn.prepareStatement(sql);
ps.setString(1, student.getId());
ps.setString(2, student.getUserName());
ps.setString(3, student.getPassword());
rs = ps.executeQuery();
//处理查询结果
if(rs.next()){
int r = rs.getInt(1);
if(r==1) ret = true;
}
System.out.println("check()...");
} catch (SQLException e) {
e.printStackTrace();
} finally{
//关闭连接
DBUtil.close(rs, ps, conn);
}
return ret;
}
//从数据库中取数据
public Student getStudent(Student student) {
String sql = "select * from students";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//获得连接
conn = DBUtil.getConnection();
//执行查询
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
//处理查询结果
while (rs.next()) {
String id =rs.getString("id");
if(student.getId().equals(id)) {
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setSex(rs.getString("sex"));
student.setCore(rs.getInt("core"));
student.setUserName(rs.getString("username"));
student.setPassword(rs.getString("password"));
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
//关闭连接
DBUtil.close(rs, ps, conn);
}
return student;
}
// 修改学生信息
public String updateStudent(Student student) {
String info = "success";
String sql = "update students set username=?,password=? where id=?";
// 执行更新语句
Connection conn = null;
PreparedStatement ps = null;
try{
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
//设置参数(注意参数顺序!)
ps.setString(1, student.getUserName());
ps.setString(2, student.getPassword());
ps.setString(3, student.getId());
//执行更新语句
int r = ps.executeUpdate();
if (r <= 0) info = "fail";
}
catch(SQLException e){
e.printStackTrace();
} finally{
// 关闭数据库连接
DBUtil.close(null,ps,conn);
}
return info;
}
/* 测试拿取数据库数据
public static void main(String[] args) {
Student student=new Student();
student.setId("100");
StuDao stuDao=new StuDao();
stuDao.getStudent(student);
System.out.println(student.toString());
}*/
}