package studentManage;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
public class ChangeFrame extends JFrame {
private JPanel contentPane;
private JTextField idField;
private JTextField nameField;
private JTextField genderField;
private JTextField dobField1;
private JTextField dobField2;
private JTextField dobField3;
private JTextField batchField;
private JTextField searchField;
/**
* Launch the application.
*/
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();
}
}
});
}
/**
* Create the frame.
*/
public ChangeFrame() {
setResizable(false);
setTitle("修改信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(200, 200, 600, 600);
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(180, 3, 220, 45);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("学号:");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1.setBounds(121, 105, 50, 30);
panel.add(lblNewLabel_1);
idField = new JTextField();
idField.setEditable(false);
idField.setBounds(180, 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(121, 160, 50, 30);
panel.add(lblNewLabel_1_1);
nameField = new JTextField();
nameField.setColumns(10);
nameField.setBounds(180, 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(121, 215, 50, 30);
panel.add(lblNewLabel_1_1_1);
genderField = new JTextField();
genderField.setColumns(10);
genderField.setBounds(180, 209, 240, 45);
panel.add(genderField);
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(91, 270, 100, 30);
panel.add(lblNewLabel_1_1_1_2);
batchField = new JTextField();
batchField.setColumns(10);
batchField.setBounds(180, 264, 240, 45);
panel.add(batchField);
JLabel lblNewLabel_2 = new JLabel("第一针接种日期:");
lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_2.setBounds(41, 325, 150, 30);
panel.add(lblNewLabel_2);
dobField1 = new JTextField();
dobField1.setColumns(10);
dobField1.setBounds(180, 319, 240, 45);
panel.add(dobField1);
JLabel lblNewLabel_3 = new JLabel("第二针接种日期:");
lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_3.setBounds(41, 380, 150, 30);
panel.add(lblNewLabel_3);
dobField2 = new JTextField();
dobField2.setColumns(10);
dobField2.setBounds(180, 374, 240, 45);
panel.add(dobField2);
JLabel lblNewLabel_4 = new JLabel("第三针接种日期:");
lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_4.setBounds(41, 435, 150, 30);
panel.add(lblNewLabel_4);
dobField3 = new JTextField();
dobField3.setColumns(10);
dobField3.setBounds(180, 429, 240, 45);
panel.add(dobField3);
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 batch =batchField.getText();
String dobString1 = dobField1.getText();
String dobString2 = dobField2.getText();
String dobString3 = dobField3.getText();
SQLHelp sqlHelp = new SQLHelp();
try {
sqlHelp.changeStudent(id, nameString, gendeString,batch ,dobString1, dobString2, dobString3 );
JOptionPane.showMessageDialog(ChangeFrame.this, "修改成功!");
} catch (SQLException e) {
JOptionPane.showMessageDialog(ChangeFrame.this, "修改失败!");
e.printStackTrace();
}
}
});
changeButton.setFont(new Font("宋体", Font.PLAIN, 18));
changeButton.setBounds(230, 484, 97, 33);
panel.add(changeButton);
searchField = new JTextField();
searchField.setBounds(181, 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) {
// id 是直接读入的,所以就不需要从数据库中读出了
idField.setText(String.valueOf(id));
nameField.setText(student.getName());
genderField.setText(student.getGender());
batchField.setText(String.valueOf(student.getBatch()));
dobField1.setText(student.getDob1());
dobField2.setText(student.getDob2());
dobField3.setText(student.getDob3());
} else {
JOptionPane.showMessageDialog(ChangeFrame.this, "无此用户");
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
searchButton.setBounds(400, 42, 97, 39);
panel.add(searchButton);
}
}