创建学校实体类College
package c02.s02.p02.bean;
import java.sql.ResultSet;
import java.sql.Time;
import java.util.Date;
public class College {
private int id;
private String username;
private String password;
private String telephone;
private Time register_time;
private String president;
private String name;
private ResultSet startTime;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Time getRegister_time() {
return register_time; }
public void setRegister_time(Time register_time) {
this.register_time = register_time; }
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime=" + register_time +
'}';
}
public void setPresident(String president) {
this.president = president;
}
public String getPresident() {
return president;
}
public void setName(String name) {
this.name = name;
}
public void setStartTime(Date start_time) {
}
public void setEmail(String email) {
}
public void setAddress(String address) {
}
public void setProfile(String profile) {
}
public String getName() {
return null;
}
public ResultSet getStartTime() {
return startTime;
}
public String getEmail() {
return email;
}
public String getProfile() {
return null;
}
}
创建状态实体类Status
package c02.s02.p02.bean;
public class Status {
private int id;
private String college;
private String version;
private String author;
private String telephone;
private String address;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCollege() {
return college;
}
public void setCollege(String username) {
this.college = college;
}
public String getVersion() {
return version;
}
public void setVersion(String password) {
this.version = version;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAdress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email; }
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + college + '\'' +
", password='" + version + '\'' +
", telephone='" + author + '\'' +
", telephone='" + telephone + '\'' +
", telephone='" + address + '\'' +
", registerTime=" + email +
'}';
}
public String getAddress() {
return null;
}
}
创建学生实体类Student
package c02.s02.p02.bean;
public class Student {
private int id;
private String name;
private String sex;
private int age;
private String department;
private String t_class ;
private String telephone;
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex ; }
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getT_class() {
return t_class ; }
public void setT_class(String t_class) {
this.sex = t_class;
}
public String getTelephone() {
return telephone ; }
public void setTelephone(String telephone) {
this.telephone = telephone; }
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + name + '\'' +
", username='" + sex + '\'' +
", username='" + age + '\'' +
", username='" + department + '\'' +
", username='" + t_class + '\'' +
", username='" + telephone + '\'' +
'}';
}
}
创建用户实体类User
package c02.s02.p02.bean;
import java.util.Date;
public class User {
private int id;
private String username;
private String password;
private String telephone;
private Date registerTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime=" + registerTime +
'}';
}
}
创建数据库连接管理类
package c02.s02.p02.dbutils;
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionManager {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/student?useSSL=false";
private static final String USER = "root";
private static final String PASSWORD = "qian0717.";
private ConnectionManager() {
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER);
conn = (Connection) DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("提示:数据库连接成功~");
} catch (ClassNotFoundException e) {
System.err.println("异常:数据库驱动程序未找到!");
} catch (SQLException e) {
System.err.println("异常:数据库连接失败!");
}
return conn;
}
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
if (!conn.isClosed()) {
conn.close();
System.out.println("提示:数据库连接关闭~");
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}
public static void main(String[] args) {
Connection conn = getConnection();
closeConnection(conn);
}
}
创建数据访问接口
package c02.s02.p02.dao;
import c02.s02.p02.bean.College;
public interface CollegeDao {
College findById(int id);
int update(College college);
}
package c02.s02.p02.dao;
import c02.s02.p02.bean.Status;
public interface StatusDao {
Status findById(int id);
int update(Status status);
}
package c02.s02.p02.dao;
import c02.s02.p02.bean.Student;
import java.util.List;
import java.util.Vector;
public interface StudentDao {
int insert(Student student);
int deleteById(String id);
int deleteByClass(String clazz);
int deleteByDepartment(String department);
int update(Student student);
Student findById(String id);
List<Student> findByName(String name);
List<Student> findByClass(String clazz);
List<Student> findByDepartment(String department);
List<Student> findAll();
Vector findRowsBySex();
Vector findRowsByClass();
Vector findRowsByDepartment();
}
package c02.s02.p02.dao;
import c02.s02.p02.bean.User;
import java.util.List;
public interface UserDao {
int insert(User user);
int delete(int id);
int update(User user);
User findById(int id);
List<User> findAll();
User login(String username, String password);
}
创建数据访问接口实现类
package c02.s02.p02.dao.impl;
import c02.s02.p02.bean.College;
import c02.s02.p02.dao.CollegeDao;
import c02.s02.p02.dbutils.ConnectionManager;
import com.mysql.jdbc.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import static c02.s02.p02.dbutils.ConnectionManager.getConnection;
public class CollegeDaoImpl implements CollegeDao {
@Override
public College findById(int id) {
College college = null;
Connection conn = getConnection();
String strSQL = "select * from t_college where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
college = new College();
college.setId(rs.getInt("id"));
college.setName(rs.getString("name"));
college.setPresident(rs.getString("president"));
college.setStartTime(rs.getTimestamp("start_time"));
college.setTelephone(rs.getString("telephone"));
college.setEmail(rs.getString("email"));
college.setAddress(rs.getString(" address"));
college.setProfile(rs.getString("profile"));
}
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return college;
}
@Override
public int update(College college) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "update t_college set name = ?, president = ?, start_time =?,"
+ " telephone E ?, email E ?, address = ?, profile E ? where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, college.getName());
pstmt.setString(2, college.getPresident());
pstmt.setTimestamp(3, new Timestamp(college.getStartTime().getTime()));
pstmt.setString(4, college.getTelephone());
pstmt.setString(5, college.getEmail());
pstmt.setString(6, college.getAddress());
pstmt.setString(7, college.getProfile());
pstmt.setInt(8, college.getId());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
}
package c02.s02.p02.dao.impl;
import c02.s02.p02.bean.Status;
import c02.s02.p02.dbutils.ConnectionManager;
import com.mysql.jdbc.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StatusDaoImpl {
@Override
public Status findById(int id) {
Status status = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_status where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
status = new Status();
status.setId(rs.getInt("id"));
status.setCollege(rs.getString("college"));
status.setVersion(rs.getString("version"));
status.setAuthor(rs.getString("author"));
status.setTelephone(rs.getString("telephone"));
status.setAddress(rs.getString("address"));
status.setEmail(rs.getString("email"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return status;
}
@Override
public int update(Status status) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "update t_status set college = ?, version = ?, author = ?,"
+ " telephone = ?, address = ?, email = ? where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, status.getCollege());
pstmt.setString(2, status.getVersion());
pstmt.setString(3, status.getAuthor());
pstmt.setString(4, status.getTelephone());
pstmt.setString(5, status.getAddress());
pstmt.setString(6, status.getEmail());
pstmt.setInt(7, status.getId());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
}
package c02.s02.p02.dao.impl;
import c02.s02.p02.bean.Student;
import c02.s02.p02.dbutils.ConnectionManager;
import com.mysql.jdbc.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException