college
package c01.s01.t09.bean;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Time;
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 c01.s01.t09.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 c01.s01.t09.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 c01.s01.t09.bean;
import java.util.Date;
public class User {
private int id;
private String username;
private String password;
private String telephone;
private Date registerTime;
public User() {
}
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 +
'}';
}
}
t09.dao.impl(CollegeDaoImpl, StatusDaoImpl, StudentDaoImpl, UserDaoImpl)
package c01.s01.t09.dao.impl;
import c01.s01.t09.bean.Status;
import c01.s01.t09.dao.StatusDao;
import c01.s01.t09.dbutils.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StatusDaoImpl implements StatusDao{
@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;
}
}`
``
```java
package c01.s01.t09.dao.impl;
import c01.s01.t09.bean.Status;
import c01.s01.t09.dao.StatusDao;
import c01.s01.t09.dbutils.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StatusDaoImpl implements StatusDao{
@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 c01.s01.t09.dao.impl;
import c01.s01.t09.bean.Student;
import c01.s01.t09.dao.StudentDao;
import c01.s01.t09.dbutils.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class StudentDaoImpl implements StudentDao {
@Override
public int insert(Student student) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "insert into t_student (id,name,sex,age,department,class,telephone)"
+ "values(?,?,?,?,?,?,?)";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1,student.getId());
pstmt.setString(2, student.getName());
pstmt.setString(3,student.getSex());
pstmt.setInt(4,student.getAge());
pstmt.setString(5,student.getDepartment());
pstmt.setString(6,student.getT_class());
pstmt.setString(7,student.getTelephone());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int deleteById(String id) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "delete from t_student where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1,id);
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int deleteByClass(String clazz) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "delete from t_student where class = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1,clazz);
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int deleteByDepartment(String department) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "delete from t_student where department = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1,department);
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int update(Student student) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "update t_student set name = ?,sex = ?, age = ?,"
+"department = ?,class = ?,telephone = ? where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1,student.getName());
pstmt.setString(2,student.getSex());
pstmt.setInt(3,student.getAge());
pstmt.setString(4,student.getDepartment());
pstmt.setString(5,student.getT_class());
pstmt.setString(6,student.getTelephone());
pstmt.setInt(7,student.getId());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public Student findById(String id) {
Student student = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1,id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()){
student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setT_class(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return student;
}
@Override
public List<Student> findByName(String name) {
List<Student> students = new ArrayList<Student>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student where name like ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1,name + "%");
ResultSet rs = pstmt.executeQuery();
while (rs.next()){
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setT_class(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return students;
}
@Override
public List<Student> findByClass(String clazz) {
List<Student> students = new ArrayList<Student>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student where class like ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, clazz + "%");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setT_class(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return students;
}
@Override
public List<Student> findByDepartment(String department) {
List<Student> students = new ArrayList<Student>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student where department like ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, department + "%");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setT_class(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return students;
}
@Override
public List<Student> findAll() {
List<Student> students = new ArrayList<Student>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setT_class(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
rs.close();
stmt.close();
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return students;
}
@Override
public Vector findRowsBySex() {
Vector rows = new Vector();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select sex as '性别', count(*) as '人数'"
+ " from t_student group by sex order by sex desc";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
Vector<String> currentRow = new Vector();
currentRow.addElement(rs.getString("性别"));
currentRow.addElement(rs.getInt("人数") + "");
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return rows;
}
@Override
public Vector findRowsByClass() {
Vector rows = new Vector();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select class as '班级', count(*) as '人数'"
+ " from t_student group by class order by class desc";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
Vector<String> currentRow = new Vector();
currentRow.addElement(rs.getString("班级"));
currentRow.addElement(rs.getInt("人数") + "");
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return rows;
}
@Override
public Vector findRowsByDepartment() {
Vector rows = new Vector();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select department as '系部', count(*) as '人数'"
+ " from t_student group by department order by department desc";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
Vector<String> currentRow = new Vector();
currentRow.addElement(rs.getString("系部"));
currentRow.addElement(rs.getInt("人数") + "");
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return rows;
}
}
package c01.s01.t09.dao.impl;
import c01.s01.t09.bean.User;
import c01.s01.t09.dao.UserDao;
import c01.s01.t09.dbutils.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao{
@Override
public int insert(User user) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "insert into t_user (username, password, telephone, register_time)"
+ " values (?, ?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3,user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int delete(int id) {
return 0;
}
@Override
public int deleteById(int id) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "delete from t_user where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id);
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int update(User user) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "update t_user set username = ?, password = ?, telephone = ?,"
+ " register_time = ? where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
pstmt.setInt(5, user.getId());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public User findById(int id) {
User user = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_user where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return user;
}
@Override
public List<User> findAll() {
List<User> users = new ArrayList<User>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_user";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
users.add(user);
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return users;
}
@Override
public User login(String username, String password) {
User user = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_user where username = ? and password = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return user;
}
}
c09.dao(CollegeDao, StatusDao, StudentDao, UserDao)
package c01.s01.t09.dao;
import c01.s01.t09.bean.College;
public interface CollegeDao {
College findById(int id);
int update(College college);
}
package c01.s01.t09.dao;
import c01.s01.t09.bean.Status;
public interface StatusDao {
Status findById(int id);
int update(Status status);
}
package c01.s01.t09.dao;
import c01.s01.t09.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 c01.s01.t09.dao;
import c01.s01.t09.bean.User;
import java.util.List;
public interface UserDao {
int insert(User user);
int delete(int id);
int deleteById(int id);
int update(User user);
User findById(int id);
List<User> findAll();
User login(String username, String password);
}
c09.dbutils
package c01.s01.t09.dbutils;
import java.sql.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 = "123456";
private ConnectionManager() {
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER);
conn = 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);
}
}
c09.gui.(AddFrame, ChangeFrame, DeleteFrame, LogonFrame, MainFrame, RegisterFrame, SeleteFrame, Start)
package c01.s01.t09.gui;
import c03.SQLHelp;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class AddFrame extends JFrame {
private JPanel contentPane;
private JTextField idField;
private JTextField nameField;
private JTextField genderField;
private JTextField dobField;
private JTextField batchField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddFrame frame = new AddFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public AddFrame() {
setResizable(false);
setTitle("添加学生");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 470);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(null);
JLabel TitleLabel = new JLabel("请输入新学生的信息:");
TitleLabel.setFont(new Font("宋体", Font.BOLD, 20));
TitleLabel.setBounds(71, 34, 220, 45);
panel.add(TitleLabel);
JLabel idLabel = new JLabel("学号:");
idLabel.setFont(new Font("宋体", Font.PLAIN, 16));
idLabel.setBounds(71, 105, 50, 30);
panel.add(idLabel);
idField = new JTextField();
idField.setBounds(143, 99, 240, 45);
panel.add(idField);
idField.setColumns(10);
JLabel nameLabel = new JLabel("姓名:");
nameLabel.setFont(new Font("宋体", Font.PLAIN, 16));
nameLabel.setBounds(71, 160, 50, 30);
panel.add(nameLabel);
nameField = new JTextField();
nameField.setColumns(10);
nameField.setBounds(143, 154, 240, 45);
panel.add(nameField);
JLabel genderLabel = new JLabel("性别:");
genderLabel.setFont(new Font("宋体", Font.PLAIN, 16));
genderLabel.setBounds(71, 215, 50, 30);
panel.add(genderLabel);
genderField = new JTextField();
genderField.setColumns(10);
genderField.setBounds(143, 209, 240, 45);
panel.add(genderField);
JLabel dobLabel = new JLabel("出生日期:");
dobLabel.setFont(new Font("宋体", Font.PLAIN, 16));
dobLabel.setBounds(41, 270, 80, 30);
panel.add(dobLabel);
dobField = new JTextField();
dobField.setColumns(10);
dobField.setBounds(143, 264, 240, 45);
panel.add(dobField);
JLabel batchLabel = new JLabel("班级:");
batchLabel.setFont(new Font("宋体", Font.PLAIN, 16));
batchLabel.setBounds(71, 325, 50, 30);
panel.add(batchLabel);
batchField = new JTextField();
batchField.setColumns(10);
batchField.setBounds(143, 319, 240, 45);
panel.add(batchField);
JButton addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
String nameString = nameField.getText();
String genderString = genderField.getText();
String dobfieldString = dobField.getText();
int batch = Integer.parseInt(batchField.getText());
System.out.println(id + "\t" + nameString + "\t" + genderString + "\t" + dobfieldString + "\t" + batch);
SQLHelp sqlHelp = new SQLHelp();
try {
sqlHelp.addStudent(id, nameString, genderString, dobfieldString, batch);
JOptionPane.showMessageDialog(AddFrame.this, "添加成功!");
} catch (SQLException e1) {
if(e1.getSQLState().equals("23000")) {
JOptionPane.showMessageDialog(AddFrame.this, "添加失败!该学生已存在");
}
e1.printStackTrace();
}
}
});
addButton.setFont(new Font("宋体", Font.PLAIN, 18));
addButton.setBounds(182, 389, 97, 33);
panel.add(addButton);
}
}
package c01.s01.t09.gui;
import c03.SQLHelp;
import c03.Student;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class ChangeFrame extends JFrame {
private JPanel contentPane;
private JTextField idField;
private JTextField nameField;
private JTextField genderField;
private JTextField dobField;
private JTextField batchField;
private JTextField searchField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChangeFrame frame = new ChangeFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ChangeFrame() {
setResizable(false);
setTitle("修改信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 470);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("请输入要修改的学生的学号:");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel.setBounds(71, 0, 208, 29);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("学号:");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1.setBounds(71, 105, 50, 30);
panel.add(lblNewLabel_1);
idField = new JTextField();
idField.setEditable(false);
idField.setBounds(143, 99, 240, 45);
panel.add(idField);
idField.setColumns(10);
JLabel lblNewLabel_1_1 = new JLabel("姓名:");
lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1.setBounds(71, 160, 50, 30);
panel.add(lblNewLabel_1_1);
nameField = new JTextField();
nameField.setColumns(10);
nameField.setBounds(143, 154, 240, 45);
panel.add(nameField);
JLabel lblNewLabel_1_1_1 = new JLabel("性别:");
lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);
panel.add(lblNewLabel_1_1_1);
genderField = new JTextField();
genderField.setColumns(10);
genderField.setBounds(143, 209, 240, 45);
panel.add(genderField);
JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:");
lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);
panel.add(lblNewLabel_1_1_1_1);
dobField = new JTextField();
dobField.setColumns(10);
dobField.setBounds(143, 264, 240, 45);
panel.add(dobField);
JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");
lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);
panel.add(lblNewLabel_1_1_1_2);
batchField = new JTextField();
batchField.setColumns(10);
batchField.setBounds(143, 319, 240, 45);
panel.add(batchField);
JButton changeButton = new JButton("修改");
changeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int id = Integer.parseInt(searchField.getText());
String nameString = nameField.getText();
String gendeString = genderField.getText();
String dobString = dobField.getText();
String batch = batchField.getText();
SQLHelp sqlHelp = new SQLHelp();
try {
sqlHelp.changeStudent(id, nameString, gendeString, dobString, batch);
JOptionPane.showMessageDialog(ChangeFrame.this, "修改成功!");
} catch (SQLException e) {
JOptionPane.showMessageDialog(ChangeFrame.this, "修改失败!");
e.printStackTrace();
}
}
});
changeButton.setFont(new Font("宋体", Font.PLAIN, 18));
changeButton.setBounds(182, 389, 97, 33);
panel.add(changeButton);
searchField = new JTextField();
searchField.setBounds(71, 39, 208, 45);
panel.add(searchField);
searchField.setColumns(10);
JButton searchButton = new JButton("查找");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
int id = Integer.parseInt(searchField.getText());
try {
SQLHelp sqlHelp = new SQLHelp();
Student student = sqlHelp.queryStudent(id);
if(student != null) {
idField.setText(String.valueOf(id));
nameField.setText(student.getName());
genderField.setText(student.getGender());
dobField.setText(student.getDob());
batchField.setText(String.valueOf(student.getBatch()));
} else {
JOptionPane.showMessageDialog(ChangeFrame.this, "无此用户");
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
searchButton.setBounds(289, 42, 97, 39);
panel.add(searchButton);
}
}
package c01.s01.t09.gui;
/**
import c03.SQLHelp; import c03.Student;
import javax.swing.; import javax.swing.border.EmptyBorder; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException;
class QueryFrame extends JFrame {
private JPanel contentPane;
private JTextField idField;
private JTextField nameField;
private JTextField genderField;
private JTextField dobField;
private JTextField batchField;
private JTextField searchField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
QueryFrame frame = new QueryFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public QueryFrame() {
setResizable(false);
setTitle("查询信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 470);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("请输入要查找的学生的学号:");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel.setBounds(71, 0, 208, 29);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("学号:");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1.setBounds(71, 105, 50, 30);
panel.add(lblNewLabel_1);
idField = new JTextField();
idField.setEditable(false);
idField.setBounds(143, 99, 240, 45);
panel.add(idField);
idField.setColumns(10);
JLabel lblNewLabel_1_1 = new JLabel("姓名:");
lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1.setBounds(71, 160, 50, 30);
panel.add(lblNewLabel_1_1);
nameField = new JTextField();
nameField.setColumns(10);
nameField.setBounds(143, 154, 240, 45);
panel.add(nameField);
JLabel lblNewLabel_1_1_1 = new JLabel("性别:");
lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);
panel.add(lblNewLabel_1_1_1);
genderField = new JTextField();
genderField.setColumns(10);
genderField.setBounds(143, 209, 240, 45);
panel.add(genderField);
JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:");
lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);
panel.add(lblNewLabel_1_1_1_1);
dobField = new JTextField();
dobField.setColumns(10);
dobField.setBounds(143, 264, 240, 45);
panel.add(dobField);
JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");
lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);
panel.add(lblNewLabel_1_1_1_2);
batchField = new JTextField();
batchField.setColumns(10);
batchField.setBounds(143, 319, 240, 45);
panel.add(batchField);
searchField = new JTextField();
searchField.setBounds(71, 39, 208, 45);
panel.add(searchField);
searchField.setColumns(10);
JButton searchButton = new JButton("查找");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// 1.读出要查询的学生输入的id
// 因为学生的学号是唯一的,所以我们根据学号查询后,只会有一条数据
// 转化一下数据类型
int id = Integer.parseInt(searchField.getText());
// 2.执行JDBC语句
try {
SQLHelp sqlHelp = new SQLHelp();
Student student = sqlHelp.queryStudent(id);
// 3.将查询结果填到文本框中
// 前提是学生存在
if(student != null) {
idField.setText(String.valueOf(id));
nameField.setText(student.getName());
genderField.setText(student.getGender());//性别
dobField.setText(student.getDob());//出生日期
batchField.setText(String.valueOf(student.getBatch()));//班级
} else {
// 此时学生不存在,提示用户,不存在该学生
JOptionPane.showMessageDialog(QueryFrame.this, "无此学生");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
searchButton.setBounds(289, 42, 97, 39);
panel.add(searchButton);
}
}
```java
package c01.s01.t09.gui;
import c01.s01.t09.bean.User;
import c01.s01.t09.service.UserService;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;
/**
* 功能:用户登录窗口
* 作者:
* 日期:
*/
public class LoginFrame extends JFrame {
private String username;
private String password;
private JLabel lblUsername;
private JLabel lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnOK;
private JButton btnCancel;
private JPanel panel, panel1, panel2, panel3;
/**
* 有参构造方法
*
* @param title
*/
public LoginFrame(String title) {
super(title);
initGUI(); // 调用初始化图形用户界面方法
eventsHandling();
}
/**
* 事件处理
*/
private void eventsHandling() {
// 给【确定】按钮注册监听器
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取用户输入的用户名和密码
username = txtUsername.getText().trim();
password = new String(txtPassword.getPassword());
// 创建用户服务对象
UserService userService = new UserService() {
@Override
public User login(String username, String password) {
return null;
}
@Override
public int deleteUserById(int i) {
return 0;
}
@Override
public User findUserById(int i) {
return null;
}
@Override
public void updateUser(User user) {
}
@Override
public List<User> findAllUsers() {
return null;
}
};
// 调用服务对象的登录方法
User user;
user = userService.login(username, password);
// 判断用户登录是否成功
if (user != null) {
// 弹出消息框提示用户
JOptionPane.showMessageDialog(null, "恭喜,[" + username + "]登录成功~");
dispose(); // 关闭登录窗口
} else {
// 弹出消息框提示用户
JOptionPane.showMessageDialog(null, "遗憾,用户名或密码错误~");
// 清空两个文本框
txtUsername.setText("");
txtPassword.setText("");
// 让姓名文本框获取焦点
txtUsername.requestFocus();
}
}
});
// 给【取消】按钮注册监听器
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0); // 退出应用程序
}
});
}
/**
* 初始化图形用户界面方法
*/
private void initGUI() {
// 实例化组件(面板与控件)
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密 码:");
txtUsername = new JTextField(15);
txtPassword = new JPasswordField(15);
btnOK = new JButton("确定[O]");
btnCancel = new JButton("取消[C]");
// 将控件添加到三个小面板
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
// 设置主面板为三行一列的网格布局
panel.setLayout(new GridLayout(3, 1));
// 将三个小面板依次添加到主面板
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
// 设置按钮热键字母
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
// 设置密码框回显字符
txtPassword.setEchoChar('*');
// 设置窗口大小
setSize(250, 200);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口刚好容纳组件
pack();
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new LoginFrame("用户登录");
}
}
package c01.s01.t09.gui;
import c03.SelectFrame;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class MainFrame extends JFrame {
private JPanel contentPane;
public MainFrame() {
setResizable(false);
setTitle("学生管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 500);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("学生管理系统");
lblNewLabel.setForeground(Color.RED);
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 25));
lblNewLabel.setBounds(140, 10, 163, 44);
contentPane.add(lblNewLabel);
JButton addButton = new JButton("添加学生");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new AddFrame().setVisible(true);
}
});
addButton.setFont(new Font("宋体", Font.PLAIN, 18));
addButton.setBounds(167, 64, 114, 37);
contentPane.add(addButton);
JButton changeButton = new JButton("修改信息");
changeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ChangeFrame().setVisible(true);
}
});
changeButton.setFont(new Font("宋体", Font.PLAIN, 18));
changeButton.setBounds(167, 121, 114, 37);
contentPane.add(changeButton);
JButton deleteButton = new JButton("删除学生");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new DeleteFrame().setVisible(true);
}
});
deleteButton.setFont(new Font("宋体", Font.PLAIN, 18));
deleteButton.setBounds(167, 180, 114, 37);
contentPane.add(deleteButton);
JButton queryButton = new JButton("查询信息");
queryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new QueryFrame().setVisible(true);
}
});
queryButton.setFont(new Font("宋体", Font.PLAIN, 18));
queryButton.setBounds(167, 240, 114, 37);
contentPane.add(queryButton);
JButton selectButton = new JButton("查询所有学生记录");
selectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
SelectFrame s = new SelectFrame();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
selectButton.setBounds(134,300,180,37);
selectButton.setFont(new Font("宋体", Font.PLAIN, 18));
contentPane.add(selectButton);
}
private class DeleteFrame {
private boolean visible;
public void setVisible(boolean visible) {
this.visible = visible;
}在这里插入代码片
public boolean getVisible() {
return visible;
}
}
}
package c01.s01.t09.gui;
import c03.SQLHelp;
import c03.User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class Register extends JFrame {
JLabel username;
JLabel password;
JLabel passwordAgain;
JTextField usernameFile;
JPasswordField passwordFile;
JPasswordField PasswordFileAgain;
JButton sign;
JPanel p;
JPanel p1;
final int WIDTH = 410;
final int HEIGHT = 610;
public Register() {
init();
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
validate();
setVisible(true);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
int x = (width - WIDTH) / 2;
int y = (height - HEIGHT) / 2;
setBounds(x, y, WIDTH, HEIGHT);
setTitle("账号注册");
}
void init() {
p = new JPanel();
p.setLayout(null);
p.setVisible(true);
p1 = new JPanel();
p1.setVisible(true);
p1.setLayout(null);
p1.setBounds(0, 0, 400, 600);
username = new JLabel("账号:");
password = new JLabel("密码:");
passwordAgain = new JLabel("确认密码:");
usernameFile = new JTextField(20);
passwordFile = new JPasswordField(20);
PasswordFileAgain = new JPasswordField(20);
passwordFile.setEchoChar('*');
PasswordFileAgain.setEchoChar('*');
sign = new JButton("注册");
sign.setBounds(160, 300, 100, 50);
username.setBounds(130, 150, 40, 20);
password.setBounds(130, 200, 40, 20);
passwordAgain.setBounds(130, 250, 120, 20);
usernameFile.setBounds(200, 150, 100, 20);
passwordFile.setBounds(200, 200, 100, 20);
PasswordFileAgain.setBounds(200, 250, 100, 20);
p1.add(username);
p1.add(password);
p1.add(passwordAgain);
p1.add(usernameFile);
p1.add(passwordFile);
p1.add(PasswordFileAgain);
p1.add(sign);
p.add(p1);
this.add(p);
sign.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String strusername = usernameFile.getText();
String strpassword = new String(passwordFile.getPassword());
String strpasswordAgain = new String(PasswordFileAgain.getPassword());
SQLHelp sqlHelp = new SQLHelp();
try {
User users = sqlHelp.userpassweord(strusername);
if (users == null) {
if (strpassword.equals(strpasswordAgain)) {
System.out.println("注册成功");
String username = strusername;
String password = strpassword;
sqlHelp.addManeger(username,password);
JOptionPane.showMessageDialog(null, "注册成功", "点击确定后返回登录界面", JOptionPane.INFORMATION_MESSAGE);
} else {
System.out.println("注册失败");
JOptionPane.showMessageDialog(null, "注册失败,请检查两次密码是否相同", "登录失败提示窗口", JOptionPane.INFORMATION_MESSAGE);
}
}else{
System.out.println("已有账号无法注册");
JOptionPane.showMessageDialog(null,"注册失败了,已拥有该账号,请检查密码是否输入错误,目前不支持找回密码","账号已有窗口",JOptionPane.INFORMATION_MESSAGE);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
}
public static void main(String[] args) {
new Register();
}
}
package c01.s01.t09.gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;
public class SeleteFrame extends JFrame {
public static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DB_URL = "jdbc:mysql://localhost:3306/java_sims?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
public static final String username = "root";
public static final String password = "111111";
public SeleteFrame() throws SQLException {
Connection connection=null;
connection = DriverManager.getConnection(DB_URL,username,password);
PreparedStatement preparedStatement=connection.prepareStatement("select *from student");
ResultSet res=preparedStatement.executeQuery();
res.last();
int row=res.getRow();
res.beforeFirst();
String arr[]= {"姓名","性别","出生日期","班级"};
String comm[][] = new String[row][4];
res.next();
for(int i=0;i<row;i++) {
comm[i][0]=res.getString("name");
comm[i][1]=res.getString("sex");
comm[i][2]=res.getString("birthday");
comm[i][3]=res.getString("gender");
res.next();
}
JTable jTable=new JTable(comm,arr);
jTable.setPreferredScrollableViewportSize(new Dimension(800,200));
JScrollPane jScrollPane=new JScrollPane(jTable);
add(jScrollPane,BorderLayout.CENTER);
setVisible(true);
validate();
this.setBounds(550,400,800,200);
pack();
connection.close();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int choice = JOptionPane.showConfirmDialog(null, "关闭该窗口?","学生管理", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
dispose();
} else {
try {
new SeleteFrame();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
});
}
}
package c01.s01.t09.gui;
import c03.LoginStar;
public class Start {
public static void main(String[] args) {
LoginStar l = new LoginStar();
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.College;
import c01.s01.t09.dao.CollegeDao;
import c01.s01.t09.dao.impl.CollegeDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestCollegeDaoImpl {
@Before
public void beforeTest(){
System.out.println("准备工作开始了!");
}
@Test
public void testFindById() {
CollegeDao dao = new CollegeDaoImpl();
College college = dao.findById(1);
System.out.println(college);
}
@Test
public void testUpdate(){
CollegeDao dao = new CollegeDaoImpl();
College college = dao.findById(1);
college.setPresident("王洪礼");
dao.update(college);
college = dao.findById(1);
System.out.println(college);
}
@After
public void afterTest(){
System.out.println("单元测试结束了!");
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.College;
import c01.s01.t09.service.CollegeService;
import c01.s01.t09.service.impl.CollegeServiceImpl;
import org.junit.Test;
public class TestCollegeServiceImpl {
@Test
public void testFindCollegeById(){
CollegeService service = (CollegeService) new CollegeServiceImpl();
College college = service.findCollegeById(1);
System.out.println(college);
}
@Test
public void testUpdateCollege(){
CollegeService service = (CollegeService) new CollegeServiceImpl();
College college = service.findCollegeById(1);
college.setPresident("王洪礼");
college.setTelephone("3152639");
int count = service.updateCollege(college);
if (count > 0){
System.out.println("恭喜,学校记录更新成功!" + college);
}else {
System.out.println("遗憾,学校记录更新失败!");
}
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.Status;
import c01.s01.t09.dao.StatusDao;
import c01.s01.t09.dao.impl.StatusDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestStatusDaoImpl {
@Before
public void beforeTest(){
System.out.println("准备工作开始了!");
}
@Test
public void testFindById() {
StatusDao dao = (StatusDao) new StatusDaoImpl();
Status college = dao.findById(1);
System.out.println(college);
}
@Test
public void testUpdate(){
StatusDao dao = (StatusDao) new StatusDaoImpl();
Status status = dao.findById(1);
status.setAuthor("王洪礼");
dao.update(status);
status = dao.findById(1);
System.out.println(status);
}
@After
public void afterTest(){
System.out.println("单元测试结束了!");
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.Status;
import org.junit.Test;
public class TestStatusServiceImpl {
@Test
public void testFindStatusById(){
TestStatusServiceImpl service = new TestStatusServiceImpl();
Status status = service.findStatusById(1);
System.out.println(status);
}
private Status findStatusById(int i) {
return null;
}
@Test
public void testUpdateStatus(){
TestStatusServiceImpl service = new TestStatusServiceImpl();
Status status = service.findStatusById(1);
status.setAuthor("巍巍妹纸");
status.setTelephone("181210000000");
int count = service.updateStatus(status);
if (count > 0){
System.out.println("恭喜,状态记录更新成功!" );
status = service.findStatusById(1);
System.out.println(status);
}else {
System.out.println("遗憾,状态记录更新失败!");
}
}
private int updateStatus(Status status) {
return 0;
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.Student;
import c01.s01.t09.dao.StudentDao;
import c01.s01.t09.dao.impl.StudentDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class TestStudentDaoImpl {
@Before
public void beforeTest() {
System.out.println("准备工作开始了!");
}
@Test
public void testInsert() {
Student student = new Student();
student.setId(18101001);
student.setName("小可爱");
student.setSex("女");
student.setAge(19);
student.setDepartment("信息工程系");
student.setT_class("18大数据1班");
student.setTelephone("15890674568");
StudentDao dao = new StudentDaoImpl();
int count = dao.insert(student);
if (count > 0) {
System.out.println("恭喜,学生记录插入成功!");
} else {
System.out.println("遗憾,学生记录插入失败!");
}
}
@Test
public void testDeleteById() {
StudentDao dao = new StudentDaoImpl();
String id = "18101001";
int count = dao.deleteById(id);
if (count > 0) {
System.out.println("恭喜,学生记录删除成功!");
} else {
System.out.println("遗憾,学生记录删除失败!");
}
}
@Test
public void testDeleteByClass() {
StudentDao dao = new StudentDaoImpl();
String clazz = "10英教1班";
int count = dao.deleteByClass(clazz);
if (count > 0) {
System.out.println("恭喜,[" + clazz + "]学生记录删除成功!");
} else {
System.out.println("遗憾,[" + clazz + "]学生记录删除失败!");
}
}
@Test
public void testDeleteByDepartment() {
StudentDao dao = new StudentDaoImpl();
String department = "信息工程系";
int count = dao.deleteByDepartment(department);
if (count > 0) {
System.out.println("恭喜,[" + department + "]学生记录删除成功!");
} else {
System.out.println("遗憾,[" + department + "]学生记录删除失败!");
}
}
@Test
public void testUpdate() {
StudentDao dao = new StudentDaoImpl();
Student student = dao.findById("10080301");
student.setName("圆圆");
int count = dao.update(student);
if (count > 0){
System.out.println("更新成功!");
}else{
System.out.println("更新失败!");
}
}
@Test
public void testFindById() {
StudentDao dao = new StudentDaoImpl();
Student student = dao.findById("10080301");
System.out.println(student);
}
@Test
public void testFindByName() {
StudentDao dao = new StudentDaoImpl();
String name = "李";
List<Student> students = dao.findByName(name);
if (students.size() > 0) {
for (Student student : students) {
System.out.println(student);
}
} else {
System.out.println("温馨提示:查无此人!");
}
}
@Test
public void testFindRowsByClass() {
StudentDao dao = new StudentDaoImpl();
String clazz = "10英教1班";
List<Student> students = dao.findByClass(clazz);
if (students.size() > 0) {
for (Student student : students) {
System.out.println(student);
}
} else {
System.out.println("温馨提示:查无此班!");
}
}
@Test
public void testFindByDepartment() {
StudentDao dao = new StudentDaoImpl();
String department = "外语系";
List<Student> students = dao.findByDepartment(department);
if (students.size() > 0) {
for (Student student : students) {
System.out.println(student);
}
} else {
System.out.println("温馨提示:查无此系!");
}
}
@Test
public void testFindAll() {
StudentDao dao = new StudentDaoImpl();
String all;
List<Student> students = dao.findAll();
for (Student student : students) {
System.out.println(student);
}
}
@Test
public void testRowsFindBySex() {
StudentDao dao = new StudentDaoImpl();
Vector rows = dao.findRowsBySex();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
@Test
public void testRowsFindByClass(){
StudentDao dao = new StudentDaoImpl();
Vector rows = dao.findRowsByClass();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByDepartment() {
StudentDao dao = new StudentDaoImpl();
Vector rows = dao.findRowsByDepartment();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
@After
public void afterTest(){
System.out.println("单元测试结束了!");
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.Student;
import c01.s01.t09.service.StudentService;
import c01.s01.t09.service.impl.StudentServiceImpl;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class TestStudentServiceImpl {
@Test
public void testDeleteStudentById() {
StudentService service = (StudentService) new StudentServiceImpl();
String id = "18101001";
int count = service.deleteStudentById(id);
if (count > 0) {
System.out.println("恭喜,学生记录删除成功!");
} else {
System.out.println("遗憾,学生记录删除失败!");
}
}
@Test
public void testDeleteStudentClass() {
StudentService service = (StudentService) new StudentServiceImpl();
String clazz = "10英教1班";
int count = service.deleteStudentsByClass(clazz);
if (count > 0) {
System.out.println("恭喜,[" + clazz + "]学生记录删除成功!");
} else {
System.out.println("遗憾,[" + clazz + "]学生记录删除失败!");
}
}
@Test
public void testDeleteStudentDepartment() {
StudentService service = (StudentService) new StudentServiceImpl();
String department = "信息工程系";
int count = service.deleteStudentsByDepartment(department);
if (count > 0) {
System.out.println("恭喜,[" + department + "]学生记录删除成功!");
} else {
System.out.println("遗憾,[" + department + "]学生记录删除失败!");
}
}
@Test
public void testupdateStudent() {
StudentService service = (StudentService) new StudentServiceImpl();
Student student = service.findStudentById("10080301");
int count = service.updateStudent(student);
if (count > 0){
System.out.println("更新成功!");
}else{
System.out.println("更新失败!");
}
}
@Test
public void testStudentById() {
StudentService service = (StudentService) new StudentServiceImpl();
Student student = service.findStudentById("10080301");
System.out.println(student);
}
@Test
public void testFindStudentsByName(){
StudentService service = (StudentService) new StudentServiceImpl();
String name = "李";
List<Student> students = service.findStudentsByName(name);
for (Student student:students){
System.out.println(student);
}
}
@Test
public void testFindStudentsByClass(){
StudentService service = (StudentService) new StudentServiceImpl();
String clazz = "15软件1班";
List<Student> students = service.findStudentsByClass(clazz);
for (Student student:students){
System.out.println(student);
}
}
@Test
public void testFindStudentsByDepartment(){
StudentService service = (StudentService) new StudentServiceImpl();
String department = "信息工程系";
List<Student> students = service.findStudentsByDepartment(department);
for (Student student:students){
System.out.println(student);
}
}
@Test
public void testFindAllStudents(){
StudentService service = (StudentService) new StudentServiceImpl();
String all = "信息工程系";
List<Student> students = service.findAllStudents();
for (Student student:students){
System.out.println(student);
}
}
@Test
public void testFindRowsBySex() {
StudentService service = (StudentService) new StudentServiceImpl();
Vector rows = service.findRowsBySex();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByClass() {
StudentService service;
service = (StudentService) new StudentServiceImpl();
Vector rows = service.findRowsByClass();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByDepartment() {
StudentService service = (StudentService) new StudentServiceImpl();
Vector rows = service.findRowsByDepartment();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.User;
import c01.s01.t09.dao.UserDao;
import c01.s01.t09.dao.impl.UserDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
public class TestUserDaoImpl {
@Before
public void beforeTest() {
System.out.println("准备工作开始了!");
}
@Test
public void testInsert() {
User user = new User();
user.setUsername("欢欢");
user.setPassword("root");
user.setTelephone("12345678908");
user.setRegisterTime(new Timestamp(new Date().getTime()));
UserDao dao = (UserDao) new UserDaoImpl();
int count = dao.insert(user);
if (count > 0) {
System.out.println("恭喜,用户记录插入成功!");
} else {
System.out.println("遗憾,用户记录插入失败!");
}
}
@Test
public void testDeleteById() {
UserDao dao = (UserDao) new UserDaoImpl();
String id = "1";
int count = dao.deleteById(1);
if (count > 0) {
System.out.println("恭喜,用户记录删除成功!");
} else {
System.out.println("遗憾,用户记录删除失败!");
}
}
@Test
public void testUpdate() {
UserDao dao = (UserDao) new UserDaoImpl();
User user = dao.findById(1);
user = dao.findById(1);
System.out.println(user);
}
@Test
public void testFindById(){
UserDao dao = (UserDao) new UserDaoImpl();
User user = dao.findById(1);
System.out.println(user);
}
@Test
public void testFindAll() {
UserDao dao = (UserDao) new UserDaoImpl();
String all;
List<User> users = dao.findAll();
for (User user : users) {
System.out.println(users);
}
}
@Test
public void testLogin(){
UserDao dao = (UserDao) new UserDaoImpl();
String username,password;
username = "admin";
password = "12345";
User user = dao.login(username,password);
if (user != null){
System.out.println("恭喜,用户和密码输入成功!");
}else {
System.out.println("遗憾,用户和密码输入错误!");
}
}
@After
public void afterTest(){
System.out.println("单元测试结束了!");
}
}
package c01.s01.t09.test;
import c01.s01.t09.bean.User;
import c01.s01.t09.service.UserService;
import c01.s01.t09.service.impl.UserServiceImpl;
import org.junit.Test;
import java.util.List;
public class TestUserServiceImpl {
@Test
public void testDeleteUserById() {
UserService service = (UserService) new UserServiceImpl();
String id = "1";
int count = service.deleteUserById(1);
if (count > 0) {
System.out.println("恭喜,用户记录删除成功!");
} else {
System.out.println("遗憾,用户记录删除失败!");
}
}
@Test
public void testUpdateUser() {
UserService service = (UserService) new UserServiceImpl();
User user = service.findUserById(1);
user.setUsername("袁雨晴");
service.updateUser(user);
user = service.findUserById(1);
System.out.println(user);
}
@Test
public void testAllUsers() {
UserService service = (UserService) new UserServiceImpl();
String all;
List<User> users = service.findAllUsers();
for (User user : users) {
System.out.println(users);
}
}
@Test
public void testLogin(){
UserService service = (UserService) new UserServiceImpl();
String username,password;
username = "admin";
password = "12345";
User user = service.login(username,password);
if (user != null){
System.out.println("恭喜,用户和密码输入成功!");
}else {
System.out.println("遗憾,用户和密码输入错误!");
}
}
}
|