package school_manage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
public class 修改学籍功能 {
public static void main(String[] args) {
new txgxj();
}
}
class txgxj{
JFrame jf;
JLabel l1,l2,l3,l4,l5,l6,l7;
JTextField f1,f2,f3,f4,f5,f6,f7;
JButton jb1,jb2,jb3;
public txgxj(){
jf = new JFrame("修改学籍");
jf.setSize(320,500);//设置窗体大小
jf.setLocationRelativeTo(null);//设置窗口每次启动都显示在屏幕正中央
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(null);
Font font = new Font("仿宋", Font.BOLD, 20);//设置统一的字体,让代码更加整洁美观
l1 = new JLabel("学 号:");
l1.setBounds(10,10,100,40);
l1.setFont(font);
f1 = new JTextField(null,20);
f1.setBounds(90,15,180,30);
f1.setFont(font);
l2 = new JLabel("姓 名:");
l2.setBounds(8, 50, 100, 40);
l2.setFont(font);
f2=new JTextField(null,20);
f2.setBounds(90,55,180,30);
f2.setFont(font);
l3 = new JLabel("姓 别:");
l3.setBounds(8, 90, 100, 40);
l3.setFont(font);
f3=new JTextField(null,20);
f3.setBounds(90,95,180,30);
f3.setFont(font);
l4 = new JLabel("出生日期:");
l4.setBounds(8, 130, 140, 40);
l4.setFont(font);
f4=new JTextField(null,20);
f4.setBounds(90,135,180,30);
f4.setFont(font);
l5 = new JLabel("籍 贯:");
l5.setBounds(8, 170, 130, 40);
l5.setFont(font);
f5=new JTextField(null,20);
f5.setBounds(90,175,180,30);
f5.setFont(font);
l6 = new JLabel("班级编号:");
l6.setBounds(8, 210, 140, 40);
l6.setFont(font);
f6=new JTextField(null,20);
f6.setBounds(90,215,180,30);
f6.setFont(font);
l7 = new JLabel("学院名称:");
l7.setBounds(8, 250, 140, 40);
l7.setFont(font);
f7=new JTextField(null,20);
f7.setBounds(90,255,180,30);
f7.setFont(font);
jb1 = new JButton("修改学籍信息");
jb1.setBounds(65, 300, 180, 40);
jb1.setFont(font);
jb1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
PreparedStatement ps = null;//PreparedStatement==>表示预编译的SQL语句的对象。
// SQL语句已预编译并存储在PreparedStatement对象中。
Connection con = null;//Connection==>与特定数据库的连接
Statement st=null;
ResultSet rs=null;
try {
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//这里的3306/后跟的是数据库名
//获取数据库连接
String url = "jdbc:mysql://localhost:3306/school_manage?serverTimezone=GMT%2B8";
//通过DriverManager完成注册
con = DriverManager.getConnection(url, "root", "123456");
//执行SQL语句
String sql2="update 学生表 set 姓名=?,性别=?,出生日期=?,籍贯=?,学院名称=? where 班级编号=? and 学号=?";
ps = con.prepareStatement(sql2);
String sql = "select * from 学生表";
st=con.createStatement();
rs=st.executeQuery(sql);
int flag=0;
while (rs.next()){
if(rs.getString("班级编号").equals(f6.getText())&&rs.getString("学号").equals(f1.getText())){
ps.setString(1,f2.getText());
ps.setString(2,f3.getText());
ps.setString(3,f4.getText());
ps.setString(4,f5.getText());
ps.setString(5,f7.getText());
ps.setString(6,f6.getText());
ps.setString(7,f1.getText());
flag++;
}
}
if(flag==0){
JOptionPane.showMessageDialog(null, "没有找到相应的班级和学生!");
}
if(f1.getText().length()!=0&&f2.getText().length()!=0&&f3.getText().length()!=0
&&f4.getText().length()!=0&&f5.getText().length()!=0 && flag!=0
&&f6.getText().length()!=0 && flag!=0&&f7.getText().length()!=0 && flag!=0){
JOptionPane.showMessageDialog(null, "修改成功!");
f1.setText(null);
f2.setText(null);
f3.setText(null);
f4.setText(null);
f5.setText(null);
f6.setText(null);
f7.setText(null);
try {
int i = ps.executeUpdate();//将注册的账户存储到数据库中
} catch (SQLException ex) {
ex.printStackTrace();
}
}else{
JOptionPane.showMessageDialog(null, "信息填写不完整,修改失败!");
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
if (f1.getText().length() != 0 && f2.getText().length() != 0 && f3.getText().length() != 0
&& f4.getText().length() != 0&& f5.getText().length() != 0
&& f6.getText().length() != 0&& f7.getText().length() != 0) {//如果所有信息都填写了,便添加成功
JOptionPane.showMessageDialog(null, "修改成功!");
f1.setText(null);
f2.setText(null);
f3.setText(null);
f4.setText(null);
f5.setText(null);
f6.setText(null);
f7.setText(null);
try {
int i = ps.executeUpdate();//将数据存储到数据库中
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
});
jb2 = new JButton("返回");
jb2.setBounds(35, 350, 80, 40);
jb2.setFont(font);
jb2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//返回到教师学籍管理页面
new txj();
jf.dispose();//关闭窗体,释放所有资源
}
});
jb3 = new JButton("退出");
jb3.setBounds(190, 350, 80, 40);
jb3.setFont(font);
jb3.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "退出成功!");
//系统退出
System.exit(0);
}
});
jf.add(l1);
jf.add(f1);
jf.add(l2);
jf.add(f2);
jf.add(l3);
jf.add(f3);
jf.add(l4);
jf.add(f4);
jf.add(l5);
jf.add(f5);
jf.add(l6);
jf.add(f6);
jf.add(l7);
jf.add(f7);
jf.add(jb1);
jf.add(jb2);
jf.add(jb3);
jf.setVisible(true);
}
}