Compare commits

..

9 Commits

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="C:/Users/86150/Documents/Tencent Files/2872589858/FileRecv/mysql-connector-java-8.0.16.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>lyfproject</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

@ -1,8 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
@ -11,4 +11,4 @@ org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17
org.eclipse.jdt.core.compiler.source=1.8

@ -0,0 +1,18 @@
//java接口定义 Dao层用于数据访问
package com.lyf.dao;//包名
//接口名称为studentDao
import java.sql.SQLException;//导入了SQLException类用于处理可能发生的数据库异常
import java.util.List;//导入List接口用于后续方法返回学生列表
import com.lyf.entity.Studentlyf;//导入了student实体类这个类定义了学生的数据结构
public interface StudentlyfDao {//声明了一个公共接口StudentDao接口名遵循驼峰命名法
public boolean LoginCheck(String Sno, String Spwd) throws SQLException;//声明了一个公共方法LoginCheck它接受两个字符串参数学号和密码),返回一个布尔值表示登录是否成功并声明可能会抛出SQLException
public boolean Register(String Sno ,String Spwd) throws SQLException;//声明了一个公共方法 Register 用于注册新学生同样接受学号和密码作为参数返回布尔值表示注册是否成功并声明可能会抛出 SQLException 
public List<Studentlyf> res() throws Exception;//声明了一个公共方法 res 用于检索所有学生信息返回一个 Student 对象的列表并声明可能会抛出异常。
}

@ -0,0 +1,100 @@
//java实体类定义它代表了一个学生管理系统中的学生实体
// Student 类是一个简单的JavaBean用于封装学生数据。
//该类遵循Java的封装原则通过私有成员变量和公共的getter/setter方法来访问和修改这些变量。
package com.lyf.entity;//包
public class Studentlyf {//声明了一个公共类student
private int sno;//学号,私有整型变量
private String clas;//班级,私有字符串变量
private String name;
private String gender;
private int age;
private String major;
private String dept;
public Studentlyf() {//无参构造器调用父类构造器 super() 
super();
}
public Studentlyf(int sno, String clas, String name, String gender, int age, String major, String dept) {
//全参构造器,用于创建具有指定属性的学生对象,并初始化成员变量。
super();
this.sno = sno;
this.clas = clas;
this.name = name;
this.gender = gender;
this.age = age;
this.major = major;
this.dept = dept;
}
public int getSno() {//返回学号
return sno;
}
public void setSno(int sno) {//设置学号
this.sno = sno;
}
public String getClas() {
return clas;
}
public void setClas(String clas) {
this.clas = clas;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
@Override//表示重写父类方法
public String toString() {//重写 Object 类的 toString() 方法,返回包含学生所有属性的字符串。
return "Student [sno=" + sno + ", clas=" + clas + ", name=" + name + ", gender=" + gender + ", age=" + age
+ ", major=" + major + ", dept=" + dept + "]";
}
}
//每个成员变量都是私有的,这是封装的体现,防止外部直接访问和修改。
//提供了全参构造器,可以在创建对象时直接初始化所有属性。
//提供了标准的getter和setter方法允许外部代码在运行时访问和修改对象的属性。
//toString() 方法提供了一种方便的方式来打印或显示学生对象的详细信息。
//这个 Student 类是学生管理系统中模型层的一部分通过封装学生的数据和行为为其他层如视图和控制器提供了一个清晰和简单的接口。

@ -0,0 +1,116 @@
//java类实现它实现了之前提到的 StudentDao 接口属于学生管理系统的数据访问对象DAO
package com.lyf.impl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.lyf.dao.StudentlyfDao;
import com.lyf.dao.StudentlyfDao;
import com.lyf.entity.Studentlyf;
import com.lyf.utils.DB;// import 语句导入了所需的Java类和接口。
/*
 Connection conn 
 DB dataBase 
 Studentimpl  StudentDao 
使JDBCJava Database ConnectivityAPI
JDBC
 Connection 
 Statement SQL
 ResultSet 
 executeQuery(String sql) SQL ResultSet 
 executeUpdate(String sql) INSERT, UPDATE, DELETESQL
 DB  getConnection() */
public class Studentlyfimpl implements StudentlyfDao { //声明了公共类 Studentimpl 
Connection conn = null;//初始化数据库连接对象。
DB dataBase = new DB();//创建数据库工具类的实例。
//学生登录
public boolean LoginCheck(String Sno, String Spwd) throws SQLException {
//这是一个公共方法名为 LoginCheck 它接受两个参数 Sno 学号 Spwd 密码都是字符串类型。
//方法声明抛出 SQLException 表示如果发生数据库相关错误将向外层抛出这个异常。
try {
conn = dataBase.getConnection();//通过调用 dataBase 对象的 getConnection() 方法获取数据库连接并将其赋值给 conn 变量。
Statement stat = null;//初始化 Statement 对象 stat  null 。这是一个准备用来执行SQL语句的对象。
ResultSet rs = null;//初始化 ResultSet 对象 rs  null  ResultSet 用于保存查询结果。
stat = conn.createStatement();//使用 conn 对象创建一个 Statement 对象并赋值给 stat 
String sql = "SELECT * FROM student WHERE Sno ='"+Sno+"' and Spwd = '"+Spwd+"'";
//构建一个SQL查询字符串用于从 student 表中选择所有符合条件的记录。这里存在SQL注入的风险因为直接将用户输入拼接到SQL语句中。
rs = stat.executeQuery(sql);//使用 stat 对象执行上面构建的SQL查询字符串并将结果赋值给 rs 
if (rs.next()){
return true;
}else{
return false;
}
//检查 ResultSet 对象 rs 是否有下一条记录。
//如果 rs.next() 返回 true 表示找到了匹配的记录方法返回 true 表示登录成功。
//如果没有找到记录 rs.next() 返回 false 方法返回 false 表示登录失败。
} catch (Exception e) {
e.printStackTrace();
}
// catch 块捕获 try 块中可能发生的任何异常。
//这里只是简单地打印了异常的堆栈跟踪
conn.close();//关闭数据库连接 conn 
return false;
}
//如果 try 块中发生异常并且没有被 catch 块处理或者没有找到匹配的登录记录方法最终返回 false 
//注册
public boolean Register(String Sno ,String Spwd) throws SQLException {
try {
conn = dataBase.getConnection();
Statement stat = null;
ResultSet rs = null;
stat = conn.createStatement();
String sql = "SELECT * FROM student WHERE Sno ='" + Sno + "' ";
rs = stat.executeQuery(sql);
if (rs.next()){
return false;
}else{
sql = "INSERT INTO student VALUES ('" + Sno + "', '" + Spwd + "')";
stat.executeUpdate(sql);
}
} catch (Exception e) {
e.printStackTrace();
}
conn.close();
return true;
}
//返回学生信息对象
public List<Studentlyf> res() throws Exception {
List<Studentlyf> students = new ArrayList<>();
Studentlyf student;
conn = dataBase.getConnection();
Statement stat = null;
ResultSet rs = null;
stat = conn.createStatement();
String sql = "SELECT * FROM student";
rs = stat.executeQuery(sql);
while (true) {
if ( rs.next() ) {
student = new Studentlyf();
student.setSno( rs.getInt("Sno") );
student.setName( rs.getString("Sname") );
student.setGender( rs.getString("Sgender") );
student.setAge( rs.getInt("Sage") );
student.setClas( rs.getString("Sclass") );
student.setMajor(rs.getString("Smajor"));
student.setDept( rs.getString("Sdept") );
students.add(student);
}else
break;
}
conn.close();
return students;
}
}

@ -0,0 +1,103 @@
package com.lyf.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
// 数据库地址
private String Driver_name = "jdbc:mysql://localhost:3306/student_a?serverTimezone=Asia/Shanghai&useSSL=false";
// 数据库用户名
private String USER = "root"; // 这里需要修改为自己的用户名和密码
// 数据库密码
private String PASS = "123456";
// 声明了一个静态的数据库连接对象 con 用于在类的方法中获取和使用数据库连接。
public static Connection con;
// 构造方法
public DB() {
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver"); // 这个驱动是mysql8版本的
// 获取连接
con = DriverManager.getConnection(Driver_name, USER, PASS);
} catch (Exception e) {
e.printStackTrace();
}
}//构造方法 DB() 在创建 DB 对象时自动执行用于加载数据库驱动并尝试建立数据库连接。
// 获取连接
public static Connection getConnection() {
if (con == null) {
new DB();
}
return con;
}//静态方法 getConnection() 用于获取数据库连接。如果 con  null 则重新创建一个 DB 实例来初始化连接。
// 释放连接
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
//这是一个静态方法用于关闭 ResultSet  Statement  Connection 对象。
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//在 close 方法中分别检查 resultSet  statement  connection 是否为 null 然后尝试关闭它们。如果关闭失败将打印堆栈跟踪。
// 释放连接
public static void close(Statement statement, Connection connection) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 释放连接
public static void close(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//提供了 close 方法的两个重载版本分别用于关闭 Statement  Connection 以及只关闭 Connection 

@ -0,0 +1,218 @@
package com.lyf.view;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import com.lyf.impl.Userimpl;
public class StudentlyfAdd extends JFrame {
private JPanel contentPane;
private JTextField snoField;
private JTextField snameField;
private JTextField sageField;
private JTextField sclassField;
private JTextField smajorField;
private JTextField sdeptField;
private JComboBox comboBox;
/**
* Launch the application.
*/
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// StudentAdd frame = new StudentAdd();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
/**
* Create the frame.
*/
//是Java Swing图形用户界面编程的一部分用于创建一个添加学生信息的界面
public StudentlyfAdd(String admin) {//这是 StudentAdd 类的构造函数它接受一个字符串参数 admin 可能是用于传递管理员信息或者其他用途。
setBounds(100, 100, 723, 506);//设置窗口的位置和大小。窗口距离屏幕左边和顶部都是100像素宽度为723像素高度为506像素。
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//移除窗口内容面板 contentPane 的布局管理器设置为 null 这通常意味着接下来的组件将使用绝对定位。
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setBounds(0, 0, 707, 467);
contentPane.add(panel);
panel.setLayout(null);
//创建一个新的 JPanel 实例设置背景颜色为白色定位和大小设置为填充整个内容面板然后添加到内容面板中并移除其布局管理器
JLabel lblNewLabel = new JLabel("添加学生");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
lblNewLabel.setBounds(237, 28, 172, 36);
panel.add(lblNewLabel);
//创建一个标题标签 JLabel 设置文本为“添加学生”水平居中对齐字体为微软雅黑加粗字号为16定位在面板内的具体位置并添加到面板中
JLabel lblNewLabel_1 = new JLabel("学号:");
lblNewLabel_1.setFont(new Font("微软雅黑", Font.BOLD, 15));
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1.setBounds(201, 81, 53, 30);
panel.add(lblNewLabel_1);
//创建一个标签,用于指示用户输入学号,设置字体样式、大小和对齐方式,然后定位并添加到面板中。
snoField = new JTextField();
snoField.setBounds(267, 80, 194, 36);
panel.add(snoField);
snoField.setColumns(10);
//创建一个文本输入框 JTextField 供用户输入学号设置其大小和位置添加到面板中并设置文本字段的列数为10这影响显示的字符宽度。
JLabel lblNewLabel_1_1 = new JLabel("姓名:");
lblNewLabel_1_1.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1_1.setFont(new Font("微软雅黑", Font.BOLD, 15));
lblNewLabel_1_1.setBounds(201, 129, 53, 30);
panel.add(lblNewLabel_1_1);
//创建一个标签,用于指示用户输入姓名,设置字体样式、大小和对齐方式,然后定位并添加到面板中
JLabel lblNewLabel_1_2 = new JLabel("年龄:");
lblNewLabel_1_2.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1_2.setFont(new Font("微软雅黑", Font.BOLD, 15));
lblNewLabel_1_2.setBounds(201, 178, 53, 30);
panel.add(lblNewLabel_1_2);
//创建一个标签,用于指示用户输入年龄,设置字体样式、大小和对齐方式,然后定位并添加到面板中。
JLabel lblNewLabel_1_3 = new JLabel("班级:");
lblNewLabel_1_3.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1_3.setFont(new Font("微软雅黑", Font.BOLD, 15));
lblNewLabel_1_3.setBounds(201, 231, 53, 30);
panel.add(lblNewLabel_1_3);
JLabel lblNewLabel_1_4 = new JLabel("专业:");
lblNewLabel_1_4.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1_4.setFont(new Font("微软雅黑", Font.BOLD, 15));
lblNewLabel_1_4.setBounds(201, 277, 53, 30);
panel.add(lblNewLabel_1_4);
JLabel lblNewLabel_1_5 = new JLabel("系别:");
lblNewLabel_1_5.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1_5.setFont(new Font("微软雅黑", Font.BOLD, 15));
lblNewLabel_1_5.setBounds(201, 330, 53, 30);
panel.add(lblNewLabel_1_5);
JLabel lblNewLabel_1_6 = new JLabel("性别:");
lblNewLabel_1_6.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1_6.setFont(new Font("微软雅黑", Font.BOLD, 15));
lblNewLabel_1_6.setBounds(201, 381, 53, 30);
panel.add(lblNewLabel_1_6);
//标题标签 JLabel 在用户界面中充当标题或说明文字的角色帮助用户理解界面的功能或指导用户进行操作。在Swing应用程序中 JLabel 是一个常用的组件可以很容易地集成到各种用户界面布局中。
snameField = new JTextField();//创建一个新的文本输入框命名为snameField用于输学生姓名
snameField.setColumns(10);//设置
snameField.setBounds(267, 126, 194, 36);
panel.add(snameField);
sageField = new JTextField();
sageField.setColumns(10);
sageField.setBounds(267, 177, 194, 36);
panel.add(sageField);
sclassField = new JTextField();
sclassField.setColumns(10);
sclassField.setBounds(267, 230, 194, 36);
panel.add(sclassField);
smajorField = new JTextField();
smajorField.setColumns(10);
smajorField.setBounds(267, 276, 194, 36);
panel.add(smajorField);
sdeptField = new JTextField();
sdeptField.setColumns(10);
sdeptField.setBounds(267, 329, 194, 36);
panel.add(sdeptField);
JButton btnNewButton = new JButton("添加");//创建新的按钮JButton其上显示文本添加
//名为btnNewButton的JButton对象
btnNewButton.addActionListener(new ActionListener() {//为btnNewButton添加ActionListener监听器
public void actionPerformed(ActionEvent e) {//事件监听器的 actionPerformed 方法定义了按钮被点击后执行的代码。
String sno = snoField.getText();
String sname = snameField.getText();
String sage = sageField.getText();
String sclass = sclassField.getText();
String smajor = smajorField.getText();
String sdept = sdeptField.getText();
String sex = (String) comboBox.getSelectedItem();//从各个文本输入框中获取用户输入的文本并从下拉框 comboBox 中获取选中项。
if (sno.equals("") || sname.equals("") || sage.equals("") || sclass.equals("") || smajor.equals("")
|| sdept.equals("") || sex.equals("")) {
JOptionPane.showMessageDialog(null, "信息不完整,请填写完整!");//检查所有输入字段是否为空。如果有任何一个字段为空,则显示一个对话框提示用户信息不完整。
} else {
Userimpl userDao = new Userimpl();
try {
boolean istrue = userDao.insert(sno, sname, sex, sage, sclass, smajor, sdept);
if (istrue) {
JOptionPane.showMessageDialog(null, "学生信息录入成功!");
} else{
JOptionPane.showMessageDialog(null, "学生录入失败,此学号已存在!");
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//如果所有字段都已填写创建 Userimpl 类的实例 userDao 尝试调用其 insert 方法将学生信息插入数据库。
//如果 insert 方法返回 true 则显示成功对话框如果返回 false 则显示失败对话框提示学号已存在。
//如果在尝试插入数据时发生 SQLException 则捕获异常并打印堆栈跟踪。
}
}
});
btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 14));//为按钮 btnNewButton 设置字体样式。这里使用了微软雅黑字体加粗( Font.BOLD )字号为14。
btnNewButton.setBounds(201, 427, 93, 30);//为 btnNewButton 按钮设置在面板上的位置和大小。 setBounds 方法的参数分别代表x坐标、y坐标、宽度和高度。
panel.add(btnNewButton);//将 btnNewButton 按钮添加到之前创建的 panel 面板中。
JButton btnNewButton_1 = new JButton("重置");//创建一个新的按钮 JButton 其上显示文本“重置”。
btnNewButton_1.addActionListener(new ActionListener() {//为“重置”按钮添加一个事件监听器,当按钮被点击时,会执行一段代码。
public void actionPerformed(ActionEvent e) {//事件监听器的 actionPerformed 方法定义了按钮被点击后执行的代码。
snoField.setText("");
snameField.setText("");
sageField.setText("");
sclassField.setText("");
smajorField.setText("");
sdeptField.setText("");//在 actionPerformed 方法内部调用每个文本输入框的 setText 方法并传入空字符串将它们的内容清空。
}//结束事件监听器的 actionPerformed 方法。
});
btnNewButton_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
//为按钮 btnNewButton_1 设置字体样式。这里使用了微软雅黑字体加粗( Font.BOLD )字号为14。
btnNewButton_1.setBounds(390, 427, 93, 30);//设置按钮 btnNewButton_1 在面板上的位置和大小。
panel.add(btnNewButton_1);//将按钮 btnNewButton_1 添加到 panel 面板中。
String[] a = new String[] { "男", "女" };//创建一个字符串数组 a 包含两个元素"男"和"女"。这个数组将用作下拉框的选项。
comboBox = new JComboBox(a);
//使用字符串数组 a 作为数据源创建一个新的下拉框组件 JComboBox 并将其引用赋值给 comboBox 变量。 JComboBox 是一个允许用户从列表中选择一个选项的组件。
comboBox.setBounds(267, 381, 194, 27);//设置下拉框 comboBox 在面板上的位置和大小。参数的含义与 setBounds 方法中相同这里的值根据实际布局需要进行了调整
panel.add(comboBox);
//将下拉框 comboBox 添加到 panel 面板中。这样下拉框就成为面板上的一个可见和可交互的组件。
}
}

@ -0,0 +1,103 @@
package com.lyf.view;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import com.lyf.impl.Userimpl;
public class StudentlyfDelete extends Bg {
private JPanel contentPane;
private JTextField keyField;
/**
* Launch the application.
*/
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// StudentDelete frame = new StudentDelete();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
/**
* Create the frame.
*/
public StudentlyfDelete(String a) {
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setBounds(100, 100, 450, 300);
// contentPane = new JPanel();
// contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
// contentPane.setLayout(new BorderLayout(0, 0));
// setContentPane(contentPane);
super(a);
JLabel lblNewLabel = new JLabel("删除学生信息");
//创建一个新的 JLabel 对象用于显示文本"删除学生信息"。 JLabel 是Swing库中用于显示文本或图标的组件。
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
//设置 lblNewLabel 的水平对齐方式为居中。 SwingConstants.CENTER 是一个常量表示文本在水平方向上居中对齐。
lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
//设置 lblNewLabel 的字体样式为微软雅黑加粗字号为18。这使得标签上的文本更加突出和易于阅读。
lblNewLabel.setBounds(442, 113, 227, 37);
//为 lblNewLabel 设置在内容面板中的位置和大小。
getContentPane().add(lblNewLabel);
//将 lblNewLabel 添加到JFrame或JDialog的内容面板 contentPane 中。这样标签就会在用户界面上显示出来。
JLabel lblNewLabel_1 = new JLabel("输入学号:");
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1.setFont(new Font("微软雅黑", Font.BOLD, 16));
lblNewLabel_1.setBounds(402, 197, 81, 27);
getContentPane().add(lblNewLabel_1);
keyField = new JTextField();//创建一个新的文本输入框 JTextField 对象命名为 keyField 用于输入需要删除的学生的学号。
keyField.setBounds(485, 193, 184, 37);
getContentPane().add(keyField);//将 keyField 添加到JFrame或JDialog的 contentPane 使其成为用户界面的一部分。
keyField.setColumns(10);//设置 keyField 的列数为10这通常影响显示的字符宽度使得输入框能够容纳更多的字符
JButton btnNewButton = new JButton("删除");
//创建一个新的按钮 JButton 其上显示文本“删除”
btnNewButton.addActionListener(new ActionListener() {
//为“删除”按钮添加一个事件监听器,当按钮被点击时,会执行一段代码。
public void actionPerformed(ActionEvent e) {
//实现事件监听器的 actionPerformed 方法定义了按钮被点击后执行的代码。
String key = keyField.getText();//从 keyField 文本输入框中获取用户输入的文本这应该是一个学号。
if (key.equals("")) {
JOptionPane.showMessageDialog(null, "学号不能为空!");
//检查用户输入的学号是否为空。如果为空,则弹出一个对话框提示用户学号不能为空。
} else {
Userimpl user = new Userimpl();//创建 Userimpl 类的一个实例这个类应该包含删除学生信息的逻辑。
try {
if (user.delete(key)) {
//调用 Userimpl 实例的 delete 方法传入学号作为参数尝试删除学生信息。根据 delete 方法的返回值弹出相应的成功或失败的消息提示框。
JOptionPane.showMessageDialog(null, "删除学生成功!");
} else {
JOptionPane.showMessageDialog(null, "删除学生失败,请检查该学号是否正确!");
}
} catch (Exception e1) {
//使用 try-catch 块来捕获并处理在删除操作中可能发生的任何异常。
}
}
}
});
btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
//设置“删除”按钮的字体样式使用微软雅黑字体加粗字号为14
btnNewButton.setBounds(696, 193, 102, 37);//设置“删除”按钮在内容面板中的位置和大小。
getContentPane().add(btnNewButton);
}
}

@ -0,0 +1,122 @@
package com.lyf.view;
import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class StudentlyfOpetion extends Bg {
private JPanel contentPane;
/**
* Launch the application.
*/
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// StudentOpetion frame = new StudentOpetion();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
/**
* Create the frame.
*/
public StudentlyfOpetion(String flag) {
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setBounds(100, 100, 450, 300);
// contentPane = new JPanel();
// contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
// contentPane.setLayout(new BorderLayout(0, 0));
// setContentPane(contentPane);
super(flag);
JPanel panel = new JPanel();
//创建一个新的 JPanel 对象这是Swing中用于组织和分组组件的容器。
panel.setBackground(Color.WHITE);
//设置面板的背景颜色为白色。
panel.setBounds(166, 53, 751, 494);
getContentPane().add(panel);//将面板添加到JFrame或JDialog的 contentPane 使其成为用户界面的一部分。
panel.setLayout(null);//移除面板的布局管理器设置为 null 。这意味着接下来添加到面板的组件将使用绝对定位
JLabel lblNewLabel = new JLabel("学生模块基本操作");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);//设置标签文本的水平对齐方式为居中。
lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
lblNewLabel.setBounds(282, 72, 191, 44);
panel.add(lblNewLabel);
JButton btnNewButton = new JButton("查询学生");//创建一个新的按钮 JButton 其上显示文本“查询学生”。
btnNewButton.addActionListener(new ActionListener() {//为“查询学生”按钮添加一个事件监听器,当按钮被点击时,会执行一段代码。
public void actionPerformed(ActionEvent arg0) {
setVisible(true);
new StudentlyfSelect("admin").setVisible(true);
}
});
btnNewButton.setBackground(SystemColor.activeCaption);
btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
btnNewButton.setBounds(158, 140, 134, 44);
panel.add(btnNewButton);
JButton btnNewButton_1 = new JButton("增加学生");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (flag.equals("学生")) {
JOptionPane.showMessageDialog(null, "没有权限!");
} else {
setVisible(true);
new StudentlyfAdd("admin").setVisible(true);
}
}
});
btnNewButton_1.setBackground(SystemColor.activeCaption);
btnNewButton_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
btnNewButton_1.setBounds(437, 140, 134, 44);
panel.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("修改学生");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (flag.equals("学生")) {
JOptionPane.showMessageDialog(null, "没有权限!");
} else {
setVisible(false);
new StudentlyfUpdate("admin").setVisible(true);
}
}
});
btnNewButton_2.setBackground(SystemColor.activeCaption);
btnNewButton_2.setFont(new Font("微软雅黑", Font.BOLD, 14));
btnNewButton_2.setBounds(158, 251, 134, 44);
panel.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("删除学生");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (flag.equals("学生")) {
JOptionPane.showMessageDialog(null, "没有权限!");
} else {
setVisible(false);
new StudentlyfDelete("admin").setVisible(true);
}
}
});
btnNewButton_3.setBackground(SystemColor.activeCaption);
btnNewButton_3.setFont(new Font("微软雅黑", Font.BOLD, 14));
btnNewButton_3.setBounds(437, 251, 134, 44);
panel.add(btnNewButton_3);
}
}

@ -0,0 +1,168 @@
package com.lyf.view;
import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import com.lyf.entity.Studentlyf;
import com.lyf.impl.Userimpl;
public class StudentlyfSelect extends JFrame {
private JPanel contentPane;
private JTextField keyField;
private JTable table;
private JScrollPane scrollPane;
/**
* Launch the application.
*/
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// StudentSelect frame = new StudentSelect();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
/**
* Create the frame.
*/
public StudentlyfSelect(String flag) {
setTitle("学生信息管理系统");
setBounds(100, 100, 845, 558);
contentPane = new JPanel();//创建一个新的 JPanel 对象命名为 contentPane 。这个面板将作为窗口内容的主要容器。
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//为 contentPane 设置边框。 EmptyBorder 创建一个透明的边框其左、右、上、下边缘都是5像素宽。
setContentPane(contentPane);
//将 contentPane 设置为当前窗口或对话框的主要内容面板。所有添加到 contentPane 上的组件都将显示在窗口中。
contentPane.setLayout(null);
//移除 contentPane 的布局管理器设置为 null 。这通常意味着接下来的组件将使用绝对定位。
JPanel panel = new JPanel();
panel.setBackground(SystemColor.activeCaption);
panel.setBounds(0, 0, 848, 51);
contentPane.add(panel);
panel.setLayout(null);//移除 panel 的布局管理器设置为 null 。这意味着 panel 中的组件将使用绝对定位。
JLabel lblNewLabel = new JLabel("学生基本信息");
lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setBounds(325, 10, 222, 31);
panel.add(lblNewLabel);
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.WHITE);
panel_1.setBounds(0, 49, 829, 51);
contentPane.add(panel_1);
panel_1.setLayout(null);
JLabel lblNewLabel_1 = new JLabel("学号:");
lblNewLabel_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
lblNewLabel_1.setBounds(225, 10, 54, 31);
panel_1.add(lblNewLabel_1);
// 表头
final Vector vector = new Vector();
vector.add("学号");
vector.add("姓名");
vector.add("性别");
vector.add("年龄");
vector.add("班级");
vector.add("专业");
vector.add("系别");
// 表里面的数据
Vector tData = new Vector();
keyField = new JTextField();
keyField.setBounds(273, 10, 190, 31);
panel_1.add(keyField);
keyField.setColumns(10);
JButton btnNewButton = new JButton("查询");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// 根据学号查询学生
String key = keyField.getText();
if (key.equals("")) {
JOptionPane.showMessageDialog(null, "学号不能为空!");
} else {
try {
Userimpl user = new Userimpl();
List<Studentlyf> list = user.Select(key, null);
tData.clear();
for (int i = 0; i < list.size(); i++) {
Vector v = new Vector();
v.add(list.get(i).getSno());
v.add(list.get(i).getName());
v.add(list.get(i).getGender());
v.add(list.get(i).getAge());
v.add(list.get(i).getClas());
v.add(list.get(i).getMajor());
v.add(list.get(i).getDept());
tData.add(v);
}
table = new JTable(tData, vector);
scrollPane.setViewportView(table);
// table = new JTable(tData, vector);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
btnNewButton.setBounds(483, 10, 67, 31);
panel_1.add(btnNewButton);
scrollPane = new JScrollPane();
scrollPane.setBounds(0, 98, 829, 421);
contentPane.add(scrollPane);
try {
Userimpl user = new Userimpl();
List<Studentlyf> list = user.res();
tData.clear();
for (int i = 0; i < list.size(); i++) {
Vector v = new Vector();
v.add(list.get(i).getSno());
v.add(list.get(i).getName());
v.add(list.get(i).getGender());
v.add(list.get(i).getAge());
v.add(list.get(i).getClas());
v.add(list.get(i).getMajor());
v.add(list.get(i).getDept());
tData.add(v);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
table = new JTable(tData, vector);
scrollPane.setViewportView(table);
}
}

@ -0,0 +1,233 @@
package com.lyf.view;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import com.lyf.entity.Studentlyf;
import com.lyf.impl.Userimpl;
public class StudentlyfUpdate extends Bg {
private JPanel contentPane;
private JTextField keyField;
private JTextField snoField;
private JTextField snameField;
private JTextField sageField;
private JTextField sclassField;
private JTextField smajorField;
private JTextField sdeptField;
private JComboBox comboBox;
/**
* Launch the application.
*/
// public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
// StudentUpdate frame = new StudentUpdate();
// frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
// }
/**
* Create the frame.
*/
public StudentlyfUpdate(String a) {
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setBounds(100, 100, 450, 300);
// contentPane = new JPanel();
// contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
// contentPane.setLayout(new BorderLayout(0, 0));
// setContentPane(contentPane);
super(a);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setBounds(166, 54, 751, 493);
getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("修改学生信息");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
lblNewLabel.setBounds(270, 32, 214, 32);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("输入学号:");
lblNewLabel_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
lblNewLabel_1.setBounds(140, 81, 81, 19);
panel.add(lblNewLabel_1);
keyField = new JTextField();
keyField.setBounds(214, 81, 182, 21);
panel.add(keyField);
keyField.setColumns(10);
JButton btnNewButton = new JButton("查询");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String key = keyField.getText();
if (key.equals("")) {
JOptionPane.showMessageDialog(null, "学号不能为空!");
} else {
Userimpl user = new Userimpl();
Studentlyf stu = new Studentlyf();
try {
stu = user.select(key);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (stu != null) {
snoField.setText(String.valueOf(stu.getSno()));
snameField.setText(stu.getName());
sageField.setText(String.valueOf(stu.getAge()));
sclassField.setText(stu.getClas());
smajorField.setText(stu.getMajor());
sdeptField.setText(stu.getDept());
comboBox.setSelectedItem(stu.getGender());
} else {
JOptionPane.showMessageDialog(null, "查无此生!");
}
}
}
});
btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
btnNewButton.setBounds(429, 77, 75, 26);
panel.add(btnNewButton);
JLabel lblNewLabel_2 = new JLabel("查询学生是否存在!");
lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_2.setForeground(Color.RED);
lblNewLabel_2.setFont(new Font("微软雅黑", Font.BOLD, 13));
lblNewLabel_2.setBounds(224, 110, 172, 19);
panel.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("学生基本信息:");
lblNewLabel_3.setFont(new Font("微软雅黑", Font.BOLD, 16));
lblNewLabel_3.setBounds(140, 151, 182, 19);
panel.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("学号:");
lblNewLabel_4.setFont(new Font("微软雅黑", Font.BOLD, 14));
lblNewLabel_4.setBounds(140, 193, 53, 21);
panel.add(lblNewLabel_4);
snoField = new JTextField();
snoField.setBounds(190, 188, 227, 32);
panel.add(snoField);
snoField.setColumns(10);
JLabel lblNewLabel_4_1 = new JLabel("姓名:");
lblNewLabel_4_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
lblNewLabel_4_1.setBounds(140, 239, 53, 21);
panel.add(lblNewLabel_4_1);
snameField = new JTextField();
snameField.setColumns(10);
snameField.setBounds(190, 235, 227, 32);
panel.add(snameField);
JLabel lblNewLabel_4_1_1 = new JLabel("年龄:");
lblNewLabel_4_1_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
lblNewLabel_4_1_1.setBounds(140, 284, 53, 21);
panel.add(lblNewLabel_4_1_1);
sageField = new JTextField();
sageField.setColumns(10);
sageField.setBounds(190, 277, 227, 32);
panel.add(sageField);
sclassField = new JTextField();
sclassField.setColumns(10);
sclassField.setBounds(190, 319, 227, 32);
panel.add(sclassField);
JLabel lblNewLabel_4_1_1_1 = new JLabel("班级:");
lblNewLabel_4_1_1_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
lblNewLabel_4_1_1_1.setBounds(140, 327, 53, 21);
panel.add(lblNewLabel_4_1_1_1);
JLabel lblNewLabel_4_1_1_1_2 = new JLabel("专业:");
lblNewLabel_4_1_1_1_2.setFont(new Font("微软雅黑", Font.BOLD, 14));
lblNewLabel_4_1_1_1_2.setBounds(140, 373, 53, 21);
panel.add(lblNewLabel_4_1_1_1_2);
smajorField = new JTextField();
smajorField.setColumns(10);
smajorField.setBounds(190, 369, 227, 32);
panel.add(smajorField);
JLabel lblNewLabel_4_1_1_1_2_1 = new JLabel("系别:");
lblNewLabel_4_1_1_1_2_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
lblNewLabel_4_1_1_1_2_1.setBounds(140, 416, 53, 21);
panel.add(lblNewLabel_4_1_1_1_2_1);
sdeptField = new JTextField();
sdeptField.setColumns(10);
sdeptField.setBounds(190, 411, 227, 32);
panel.add(sdeptField);
JLabel lblNewLabel_4_1_1_1_2_1_1 = new JLabel("性别:");
lblNewLabel_4_1_1_1_2_1_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
lblNewLabel_4_1_1_1_2_1_1.setBounds(140, 462, 53, 21);
panel.add(lblNewLabel_4_1_1_1_2_1_1);
String[] b = new String[] { "男", "女" };
comboBox = new JComboBox(b);
comboBox.setBounds(190, 453, 227, 30);
panel.add(comboBox);
JButton btnNewButton_1 = new JButton("保存");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sno = snoField.getText();
String sname = snameField.getText();
String sage = sageField.getText();
String sclass = sclassField.getText();
String smajor = smajorField.getText();
String sdept = sdeptField.getText();
String sex = (String) comboBox.getSelectedItem();
Userimpl userDao = new Userimpl();
try {
boolean flag = userDao.update(sno, sname, sex, sage, sclass, smajor, sdept);
if (flag) {
JOptionPane.showMessageDialog(null, "修改成功!");
} else {
JOptionPane.showMessageDialog(null, "修改失败!");
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnNewButton_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
btnNewButton_1.setBounds(474, 453, 93, 30);
panel.add(btnNewButton_1);
}
}
Loading…
Cancel
Save