package dialog;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import mysystem.shujuku;
public class updateDialog extends JDialog implements ActionListener {
Connection cnt=null;
PreparedStatement pre=null;
ResultSet rst=null;
String sql=null;
int i=1;
String[] student=new String[10];
int id=0;
//初始化控件
JLabel no = new JLabel("输入要修改的学号:");
JTextField jtno=new JTextField();
JButton beginup=new JButton("开始修改");
JLabel name = new JLabel("(新)姓名:");
JTextField jtname=new JTextField();
JLabel sex = new JLabel("(新)性别:");
JRadioButton male = new JRadioButton("男"); // 创建单选按钮
JRadioButton female = new JRadioButton("女");
JLabel department = new JLabel("(新)专业:");
JTextField jtdepartment=new JTextField(15);
JLabel grade = new JLabel("(新)年级:");
JTextField jtgrade=new JTextField(15);
JLabel birthdate = new JLabel("(新)出生:");
JTextField jtbirthdate=new JTextField(15);
JButton update=new JButton("录入修改");
JButton reset=new JButton("重置");
ButtonGroup btngrp = new ButtonGroup();
public updateDialog(JFrame owner){
super(owner);
btngrp.add(male);
btngrp.add(female);
//设置控件位置(横坐标,纵坐标,宽度,高度)
//row1
no.setBounds(new Rectangle(80, 5, 160, 20));
jtno.setBounds(new Rectangle(210, 5, 90, 20));
beginup.setBounds(new Rectangle(300, 5, 90, 20));
//row2
name.setBounds(new Rectangle(80, 35, 100, 20));
jtname.setBounds(new Rectangle(160, 35, 350, 20));
//row3
sex.setBounds(new Rectangle(80, 65, 100, 20));
male.setBounds(new Rectangle(160, 65, 60, 20));
female.setBounds(new Rectangle(220, 65, 60, 20));
//row4
department.setBounds(new Rectangle(80, 95, 100, 20));
jtdepartment.setBounds(new Rectangle(160, 95, 350, 20));
//row5
grade.setBounds(new Rectangle(80, 125, 100, 20));
jtgrade.setBounds(new Rectangle(160, 125, 350, 20));
//row6
birthdate.setBounds(new Rectangle(80, 155, 100, 20));
jtbirthdate.setBounds(new Rectangle(160, 155, 350, 20));
//row7
update.setBounds(new Rectangle(230, 185, 100, 30));
reset.setBounds(new Rectangle(330, 185, 100, 30));
//向主窗体添加
this.add(no);
this.add(jtno);
this.add(name);
this.add(jtname);
this.add(sex);
this.add(male);
this.add(female);
this.add(department);
this.add(jtdepartment);
this.add(grade);
this.add(jtgrade);
this.add(birthdate);
this.add(jtbirthdate);
this.add(update);
this.add(reset);
this.add(beginup);
beginup.addActionListener(this);
reset.addActionListener(this);
//设置主窗体的一些属性
update.setEnabled(false);//设置录入修改按钮不可点击
this.setTitle("学生修改");
this.setLayout(null);
this.setSize(550, 300);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setModal(true);
this.setFont((new Font("宋体", Font.PLAIN, 20)));
this.setVisible(true);
}
public void query(){
try{
id=Integer.parseInt(jtno.getText());
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "您输入的ID不合法,请输入纯数字");
return;
}
try {
sql="select * from student where id=?";
cnt=shujuku.getConnection();
pre=cnt.prepareStatement(sql);
pre.setInt(1, id);
rst=pre.executeQuery();
if(rst.next()){
update.setEnabled(true);
update.addActionListener(this);
for(int i=0;i<6;i++){
student[i]=rst.getString(i+1);
}
}
else{JOptionPane.showMessageDialog(null, "没有查找到该ID");return;}
for(int i=0;i<student.length;i++){
// System.out.println(student[i]);
}
jtname.setText(student[1]);
if("男".equals(student[2])) male.setSelected(true);
else female.setSelected(true);
jtdepartment.setText(student[3]);
jtgrade.setText(student[4]);
jtbirthdate.setText(student[5]);
} catch (Exception e2) {
e2.printStackTrace();
}
}
public String isMan(){
if(male.isSelected()||female.isSelected()){
if(male.isSelected()) return "男";
else return "女";
}
else return null;
}
public boolean isFinished(){
if(jtno.getText().length()==0) return false;
if(jtname.getText().length()==0) return false;
if(!male.isSelected()&&!female.isSelected()) return false;
if(jtdepartment.getText().length()==0) return false;
if(jtgrade.getText().length()==0) return false;
if(jtbirthdate.getText().length()==0) return false;
else return true;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==beginup){
query();
}
else if(e.getSource()==update){
int i2=0;
if(isFinished()){
sql="UPDATE student SET name=?,sex=?,department=?,grade=?,birthdate=? WHERE id=?";
cnt=shujuku.getConnection();
try {
pre=cnt.prepareStatement(sql);
pre.setString(1, jtname.getText());
pre.setString(2, isMan());
pre.setString(3, jtdepartment.getText());
pre.setString(4, jtgrade.getText());
pre.setString(5, jtbirthdate.getText());
pre.setInt(6, id);
i2=pre.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(i2>0)
JOptionPane.showMessageDialog(null, "修改成功");
else
JOptionPane.showMessageDialog(null, "修改异常");
}
else JOptionPane.showMessageDialog(null, "请将信息填写完全");
}
else if(e.getSource()==reset){
jtname.setText(null);
btngrp.clearSelection();
jtdepartment.setText(null);
jtgrade.setText(null);
jtbirthdate.setText(null);
}
}
}