package com.server.StudentTest;
import java.sql.*;
import java.util.*;
public class StudentUtil {
private Connection con;
DBconn condb=new DBconn();
//构造方法,获得数据库的连接。
public StudentUtil(){
this.con = condb.getConn();
}
//添加一个学生,使用Student值对象作为参数传给这个方法。
public void addStudent(Student student) throws Exception {
/*
* PreparedStatement接口用来执行动态SQL语句
*/
PreparedStatement pstmt = con
.prepareStatement("insert into student values(?,?,?,?,?)");
pstmt.setString(1, student.getSid());
pstmt.setString(2, student.getName());
pstmt.setInt(3, student.getSex());
pstmt.setString(4, student.getPhone());
pstmt.setString(5, student.getBirth());
pstmt.execute();//preparedStatement类执行SQL语句的方法:执行保存操作!
pstmt.close();
con.close();
}
//按学号删除指定学生信息!
public void deleteStudent(String sid) throws Exception {
Statement stmt = con.createStatement();
stmt.executeUpdate("delete from student where sid='" + sid + "'");
stmt.close();
con.close();
}
//更改学生的信息,使用Student值对象作为参数传给这个方法。
public void modifyStudent(Student student) throws Exception {
PreparedStatement pstmt = con
.prepareStatement("update student set name=?, sex=?,phone=?,birth=? where sid=?");
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getSex());
pstmt.setString(3, student.getPhone());
pstmt.setString(4, student.getBirth());
pstmt.setString(5, student.getSid());
pstmt.execute();
pstmt.close();
con.close();
}
//查询所有学生的信息
public Collection getAllStudent() throws Exception {
/*
* java.sql.Statement接口用来执行静态的SQL语句,并返回执行结果。
*/
Statement stmt = con.createStatement();//获取Statement对象
/*
* java.sql.ResultSet接口类似一个数据表,通过该接口的实例
* 可以获得检索结果集以及对应的数据表相关信息。
*/
ResultSet rst = stmt.executeQuery("select * from student");
Collection ret = new ArrayList();
while (rst.next()) {
Student tempSt = new Student();
tempSt.setSid(rst.getString("sid"));
tempSt.setName(rst.getString("name"));
tempSt.setSex(rst.getInt("sex"));
tempSt.setPhone(rst.getString("phone"));
tempSt.setBirth(rst.getString("birth"));
ret.add(tempSt);
}
stmt.close();
con.close();
return ret;
}
//按学号查询学生信息!
public Student getStudentInfo(String sid) throws Exception {
Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery("select * from student where sid='"
+ sid + "'");
Student student = null;
while (rst.next()) {
student = new Student();
student.setSid(rst.getString("sid"));
student.setName(rst.getString("name"));
student.setSex(rst.getInt("sex"));
student.setPhone(rst.getString("phone"));
student.setBirth(rst.getString("birth"));
}
stmt.close();
con.close();
return student;
}
/*/按照学生的学号查找学生
public Student getStudentBySid(String sid) throws Exception {
Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery("select * from student where sid='"
+ sid + "'");
Student tempSt = new Student();
rst.next();
tempSt.setSid(rst.getString("sid"));
tempSt.setName(rst.getString("name"));
tempSt.setSex(rst.getInt("sex"));
tempSt.setPhone(rst.getString("phone"));
tempSt.setBirth(rst.getString("birth"));
stmt.close();
con.close();
return tempSt;
}
*/
//把String类型的数据转换成日期型。
public synchronized static java.util.Date getStringToDate(String date,
String type) throws Exception {
if (type == null || type.equals("") || type.equals("null")) {
type = "yyyyMMdd";
}
java.text.SimpleDateFormat jts = new java.text.SimpleDateFormat(type);
java.util.Date fact = jts.parse(date);
return fact;
}
}