package com.lin.sql;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JButton;
public class Addinformation extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField Stuidvalue;
private JTextField Namevalue;
private JTextField Birthdayvalue;
private JTextField Hometownvalue;
private JComboBox collegevalue;
private JComboBox majorvalue;
private Connection ct = new ConnectionDB().getConnection();
private Statement stmt = null;
private AbstractButton radioButton;
private AbstractButton radioButton_1;
private AbstractButton men;
private AbstractButton women;
private String sx = "男";
private String xueyuan = null;// 学院ID
private String nuw = null;// 专业id
private String zhiye = null;// 职业id
private JButton sureadd = new JButton("\u786E\u5B9A\u6DFB\u52A0");
private JComboBox year = new JComboBox();
private JComboBox month = new JComboBox();
private String tag = "待就业";
public Addinformation() {
setToolTipText("\u8BA1\u7B97\u673A\r\n\u4E66");
setLayout(null);
JLabel Stuid = new JLabel("学号");
Stuid.setBounds(35, 27, 72, 18);
add(Stuid);
Stuidvalue = new JTextField();
Stuidvalue.setBounds(121, 24, 128, 24);
add(Stuidvalue);
Stuidvalue.setColumns(10);
Namevalue = new JTextField();
Namevalue.setColumns(10);
Namevalue.setBounds(121, 64, 128, 24);
add(Namevalue);
JLabel name = new JLabel("姓名");
name.setBounds(35, 67, 72, 18);
add(name);
JLabel Birthday = new JLabel("出生年月");
Birthday.setBounds(35, 154, 72, 18);
add(Birthday);
Hometownvalue = new JTextField();
Hometownvalue.setColumns(10);
Hometownvalue.setBounds(121, 190, 128, 24);
add(Hometownvalue);
JLabel Hometown = new JLabel("籍贯");
Hometown.setBounds(35, 193, 72, 18);
add(Hometown);
JLabel major = new JLabel("专业");
major.setBounds(296, 67, 72, 18);
add(major);
JComboBox majorvalue = new JComboBox();
try {
PreparedStatement ps2 = ct.prepareStatement("select Zymc from tb_Zyinfo order by Zybh asc");
ResultSet rs2 = ps2.executeQuery();
while (rs2.next())// 利用循环,读取每条记录
{
majorvalue.addItem(rs2.getObject(1));// 将每条记录添加到jComboBox3中去
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
majorvalue.setBounds(395, 64, 141, 24);
add(majorvalue);
majorvalue.setSelectedIndex(0);
JLabel college = new JLabel("学院");
college.setBounds(296, 27, 72, 18);
add(college);
JComboBox collegevalue = new JComboBox();
try {
PreparedStatement ps3 = ct.prepareStatement("select Yxmc from tb_Xueyinfo");
ResultSet rs3 = ps3.executeQuery();
while (rs3.next())// 利用循环,读取每条记录
{
collegevalue.addItem(rs3.getObject(1));// 将每条记录添加到jComboBox3中去
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
collegevalue.setToolTipText("\u8BA1\u7B97\u673A");
collegevalue.setSelectedIndex(0);
collegevalue.setBounds(395, 24, 141, 24);
add(collegevalue);
ButtonGroup sex = new ButtonGroup();
JRadioButton men = new JRadioButton("男");
men.setSelected(true);
men.setBounds(128, 111, 48, 27);
// sex.
add(men);
sex.add(this.men);
JRadioButton women = new JRadioButton("女");
women.setBounds(201, 111, 48, 27);
add(women);
sex.add(this.women);
men.addActionListener(new ActionListener() { // 捕获单选按钮被选中的事件
public void actionPerformed(ActionEvent e) {
// radioButton.setText("您选中的单选按钮是:" + radioButton.getText());
women.setSelected(false);
sx = men.getText();
System.out.println("" + sx);
}
});
women.addActionListener(new ActionListener() { // 捕获单选按钮被选中的事件
public void actionPerformed(ActionEvent e) {
// radioButton.setText("您选中的单选按钮是:" + radioButton.getText());
men.setSelected(false);
sx = women.getText();
System.out.println("" + sx);
}
});
JLabel sexlabel = new JLabel("性别");
sexlabel.setBounds(35, 115, 72, 18);
add(sexlabel);
JLabel label = new JLabel("就业情况");
label.setBounds(296, 115, 72, 18);
add(label);
ButtonGroup RadioGroup = new ButtonGroup();
JRadioButton radioButton = new JRadioButton("待就业");
radioButton.setSelected(true);
radioButton.setBounds(385, 111, 78, 27);
add(radioButton);
RadioGroup.add(this.radioButton);
JRadioButton radioButton_1 = new JRadioButton("已就业");
radioButton_1.setBounds(471, 111, 78, 27);
add(radioButton_1);
RadioGroup.add(this.radioButton_1);
radioButton.addActionListener(new ActionListener() { // 捕获单选按钮被选中的事件
public void actionPerformed(ActionEvent e) {
// radioButton.setText("您选中的单选按钮是:" + radioButton.getText());
tag = "待就业";
System.out.println("" + tag);
radioButton_1.setSelected(false);
}
});
radioButton_1.addActionListener(new ActionListener() { // 捕获单选按钮被选中的事件
public void actionPerformed(ActionEvent e) {
// radioButton.setText("您选中的单选按钮是:" + radioButton.getText());
tag = "已就业";
System.out.println("" + tag);
radioButton.setSelected(false);
}
});
JLabel label_1 = new JLabel("用人单位");
label_1.setBounds(295, 157, 72, 18);
add(label_1);
JComboBox comboBox1 = new JComboBox();
try {
PreparedStatement ps = ct.prepareStatement("select Yrdw from tb_Yrdwinfo");
ResultSet rs = ps.executeQuery();
while (rs.next())// 利用循环,读取每条记录
{
comboBox1.addItem(rs.getObject(1));// 将每条记录添加到jComboBox3中去
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
comboBox1.setBounds(394, 154, 141, 24);
add(comboBox1);
sureadd.setBounds(121, 253, 113, 27);
add(sureadd);
JComboBox comboBox2 = new JComboBox();
comboBox2.setBounds(395, 190, 141, 24);
add(comboBox2);
sureadd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("add suceed");
int i = 0;
String sql = "insert into tb_Stuinfo(StuID,StuNA,Sex,Date,Place,Yxbh,Zybh,Sign) values (?,?,?,?,?,?,?,?)";
PreparedStatement pstmt;
PreparedStatement pstmt2;
if (zhiyeid(comboBox1, comboBox2) == null) {
try {
pstmt = (PreparedStatement) ct.prepareStatement(sql);
pstmt.setString(1, Stuidvalue.getText());
pstmt.setString(2, Namevalue.getText());
pstmt.setString(3, sx);
pstmt.setString(4, year.getSelectedItem().toString() + month.getSelectedItem().toString());
pstmt.setString(5, Hometownvalue.getText());
pstmt.setString(6, yuanxiid(collegevalue));
pstmt.setString(7, zhuanyeid(majorvalue));
pstmt.setString(8, "待就业");
i = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException ee) {
ee.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失败,该数据有误或已存在", "提示", JOptionPane.ERROR_MESSAGE);
}
} else {
String sql2 = "insert into tb_Yginfo(StuID,Zhiyh) values (?,?)";
try {
int j = 0;
pstmt = (PreparedStatement) ct.prepareStatement(sql);
pstmt.setString(1, Stuidvalue.getText());
pstmt.setString(2, N