|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
新手上路,请多关照。
实现窗体填入个人信息,增加到数据库中。用了if else语句进行判断是否学号已存在,但弹出窗体填入信息点击确定按钮,第一次成功填入。不关闭窗体,保留原来的信息,再次点击按钮插入仍然是成功的 (应该是学号已经存在,插入信息失败)
求大神解答。
代码如下:
- package com.zsgc.jframe;
- import java.awt.Color;
- import java.awt.GridLayout;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- 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 com.jdbc.driver.GetConn;
- import com.jdbc.manager.*;
- /**
- *
- * @author 23203 增加学生信息的窗体,待完善。
- *
- */
- public class InsertFrane extends JFrame { // ItemListener
- // 下拉列表监听器
- private JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7;
- private JComboBox<String> comboBox1, comboBox2, comboBox3, comboBox4,
- comboBox5;
- private JButton button1, button2;
- private JTextField textField1, textField2, textField3, textField4;
- private String nation[] = { "汉", "白", "傣", "蒙古族", "水族", "维吾尔族", "美" };
- private String sex[] = { "男", "女" };
- private int START_YEAR = 1980;
- private int END_YEAR = 2010;
- public static void main(String[] args) {
- InsertFrane A = new InsertFrane();
- A.setVisible(true);
- }
- public InsertFrane() {
- button1 = new JButton("确定");
- button2 = new JButton("取消");
- comboBox1 = new JComboBox<String>(nation);
- comboBox2 = new JComboBox<String>(sex);
- comboBox3 = new JComboBox<String>();
- comboBox4 = new JComboBox<String>();
- comboBox5 = new JComboBox<String>();
- panel1 = new JPanel();
- panel2 = new JPanel();
- panel3 = new JPanel();
- panel4 = new JPanel();
- panel5 = new JPanel();
- panel6 = new JPanel();
- panel7 = new JPanel();
- textField1 = new JTextField(10);
- textField2 = new JTextField(10);
- textField3 = new JTextField(10);
- textField4 = new JTextField(10);
- setTitle("增加学生信息");
- setLocation(200, 200);
- setSize(400, 400);
- setResizable(false);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(new GridLayout(8, 1));
- panel1.add(new JLabel("学号:"));
- panel1.add(textField1);
- panel2.add(new JLabel("姓名:"));
- panel2.add(textField2);
- panel3.add(new JLabel("年级:"));
- panel3.add(textField3);
- panel3.add(new JLabel(" 例如:1601,1602"));
- panel4.add(new JLabel("专业:"));
- panel4.add(textField4);
- panel5.add(new JLabel("民族:"));
- panel5.add(comboBox1);
- panel5.add(new JLabel(" 性别:"));
- panel5.add(comboBox2);
- panel6.add(new JLabel("生日:年:"));
- panel6.add(comboBox3);
- panel6.add(new JLabel(" 月:"));
- panel6.add(comboBox4);
- panel6.add(new JLabel(" 日:"));
- panel6.add(comboBox5);
- panel7.add(button1);
- panel7.add(button2);
- add(panel1);
- add(panel2);
- add(panel3);
- add(panel4);
- add(panel5);
- add(panel6);
- add(panel7);
- // 初始化年月日
- for (int i = START_YEAR; i <= END_YEAR; i++) {
- comboBox3.addItem("" + i);
- }// 初始化年份
- for (int i = 1; i <= 12; i++) {
- comboBox4.addItem("" + i);
- }// 初始化月份
- for (int i = 1; i <= 31; i++) {
- comboBox5.addItem("" + i);
- }// 初始化号
- comboBox4.addItemListener(new ItemListener() {
- /**
- * 下拉列表事件,解决闰年二月份问题。和别的事件处理方法一致,注意曾经写错(继承监听器,却没有注册监听器)
- */
- @Override
- public void itemStateChanged(ItemEvent e) {
- int data = 31;
- Object obj = comboBox4.getSelectedItem(); // getSelectedItem()方法,返回当前下拉列表的值(以对象的形式返回)
- // 取得当前月份
- if (obj != null)
- comboBox5.removeAllItems();// removeAllItems()方法,删除掉下拉列表的所有的项目,清空掉日
- int mouth = Integer.valueOf(obj.toString());// 将对象(月份)变为整数
- if (mouth == 4 || mouth == 6 || mouth == 9 || mouth == 11) {
- data = 30;
- } else if (mouth == 2) {
- int year = Integer.valueOf(comboBox3.getSelectedItem()
- .toString());// 取得当前年号
- if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))// 是闰年
- data = 29;
- else
- data = 28;
- }
- for (int i = 1; i <= data; i++) {
- comboBox5.addItem("" + i);
- }// 添加号
- }
- });
- // 鼠标事件
- button1.addMouseListener(new MouseAdapter() {
- String sql = "select *from student_information";
- @Override
- public void mouseClicked(MouseEvent e) {
- GetConn getconn = new GetConn();
- Connection conn = getconn.getConnection();
- try {
- Statement statement1 = conn.createStatement();
- ResultSet resultSet1 = statement1.executeQuery(sql);
- while (resultSet1.next()) {
- int userID = resultSet1.getInt("userId");
- [color=Red]if (textField1.getText().equals("" + userID)) {
- button1.setBackground(Color.RED);
- JOptionPane.showMessageDialog(null, "此学号已存在");
- break;
- } else {
- int UserId = Integer.parseInt(textField1.getText());
- String Name = textField2.getText();
- String Sex = String.valueOf(comboBox2
- .getSelectedItem());
- String birthday = String.valueOf(comboBox3
- .getSelectedItem())
- + String.valueOf(comboBox4
- .getSelectedItem())
- + String.valueOf(comboBox5
- .getSelectedItem());
- int Birthday = Integer.parseInt(birthday);
- String Nation = String.valueOf(comboBox1
- .getSelectedItem());
- int Class1 = Integer.parseInt(textField3.getText());
- String Zhuanye = textField4.getText();
- new InsertInformation(UserId, Name, Sex, Birthday,
- Nation, Class1, Zhuanye);// 调用插入的方法,连接数据库等等操作
- button1.setBackground(Color.GREEN);
- JOptionPane.showMessageDialog(null, "插入信息成功");
- break;
- }
- [/color]
- }
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } finally {
- if (conn != null) {
- try {
- conn.close();// 关闭当前数据库连接
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- }
- }
- });
- button2.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- System.exit(0);
- super.mouseClicked(e);
- }
- });
- }
- }
复制代码 |
|