鱼C论坛

 找回密码
 立即注册
查看: 3125|回复: 3

else语句失效

[复制链接]
发表于 2017-7-3 14:09:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
新手上路,请多关照。

实现窗体填入个人信息,增加到数据库中。用了if else语句进行判断是否学号已存在,但弹出窗体填入信息点击确定按钮,第一次成功填入。不关闭窗体,保留原来的信息,再次点击按钮插入仍然是成功的 (应该是学号已经存在,插入信息失败)

求大神解答。

代码如下:
  1. package com.zsgc.jframe;

  2. import java.awt.Color;
  3. import java.awt.GridLayout;
  4. import java.awt.event.ItemEvent;
  5. import java.awt.event.ItemListener;
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8. import java.sql.Connection;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;

  12. import javax.swing.JButton;
  13. import javax.swing.JComboBox;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.JTextField;

  19. import com.jdbc.driver.GetConn;
  20. import com.jdbc.manager.*;

  21. /**
  22. *
  23. * @author 23203 增加学生信息的窗体,待完善。
  24. *
  25. */
  26. public class InsertFrane extends JFrame { // ItemListener
  27.                                                                                         // 下拉列表监听器
  28.         private JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7;
  29.         private JComboBox<String> comboBox1, comboBox2, comboBox3, comboBox4,
  30.                         comboBox5;
  31.         private JButton button1, button2;
  32.         private JTextField textField1, textField2, textField3, textField4;
  33.         private String nation[] = { "汉", "白", "傣", "蒙古族", "水族", "维吾尔族", "美" };
  34.         private String sex[] = { "男", "女" };
  35.         private int START_YEAR = 1980;
  36.         private int END_YEAR = 2010;

  37.         public static void main(String[] args) {
  38.                 InsertFrane A = new InsertFrane();
  39.                 A.setVisible(true);

  40.         }

  41.         public InsertFrane() {
  42.                 button1 = new JButton("确定");
  43.                 button2 = new JButton("取消");
  44.                 comboBox1 = new JComboBox<String>(nation);
  45.                 comboBox2 = new JComboBox<String>(sex);
  46.                 comboBox3 = new JComboBox<String>();
  47.                 comboBox4 = new JComboBox<String>();
  48.                 comboBox5 = new JComboBox<String>();
  49.                 panel1 = new JPanel();
  50.                 panel2 = new JPanel();
  51.                 panel3 = new JPanel();
  52.                 panel4 = new JPanel();
  53.                 panel5 = new JPanel();
  54.                 panel6 = new JPanel();
  55.                 panel7 = new JPanel();

  56.                 textField1 = new JTextField(10);
  57.                 textField2 = new JTextField(10);
  58.                 textField3 = new JTextField(10);
  59.                 textField4 = new JTextField(10);

  60.                 setTitle("增加学生信息");
  61.                 setLocation(200, 200);
  62.                 setSize(400, 400);
  63.                 setResizable(false);
  64.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65.                 setLayout(new GridLayout(8, 1));
  66.                 panel1.add(new JLabel("学号:"));
  67.                 panel1.add(textField1);
  68.                 panel2.add(new JLabel("姓名:"));
  69.                 panel2.add(textField2);
  70.                 panel3.add(new JLabel("年级:"));
  71.                 panel3.add(textField3);
  72.                 panel3.add(new JLabel("  例如:1601,1602"));
  73.                 panel4.add(new JLabel("专业:"));
  74.                 panel4.add(textField4);
  75.                 panel5.add(new JLabel("民族:"));
  76.                 panel5.add(comboBox1);
  77.                 panel5.add(new JLabel("           性别:"));
  78.                 panel5.add(comboBox2);
  79.                 panel6.add(new JLabel("生日:年:"));
  80.                 panel6.add(comboBox3);
  81.                 panel6.add(new JLabel("   月:"));
  82.                 panel6.add(comboBox4);
  83.                 panel6.add(new JLabel("   日:"));
  84.                 panel6.add(comboBox5);

  85.                 panel7.add(button1);
  86.                 panel7.add(button2);
  87.                 add(panel1);
  88.                 add(panel2);
  89.                 add(panel3);
  90.                 add(panel4);
  91.                 add(panel5);
  92.                 add(panel6);
  93.                 add(panel7);

  94.                 // 初始化年月日
  95.                 for (int i = START_YEAR; i <= END_YEAR; i++) {
  96.                         comboBox3.addItem("" + i);
  97.                 }// 初始化年份
  98.                 for (int i = 1; i <= 12; i++) {
  99.                         comboBox4.addItem("" + i);
  100.                 }// 初始化月份
  101.                 for (int i = 1; i <= 31; i++) {
  102.                         comboBox5.addItem("" + i);
  103.                 }// 初始化号

  104.                 comboBox4.addItemListener(new ItemListener() {
  105.                         /**
  106.                          * 下拉列表事件,解决闰年二月份问题。和别的事件处理方法一致,注意曾经写错(继承监听器,却没有注册监听器)
  107.                          */
  108.                         @Override
  109.                         public void itemStateChanged(ItemEvent e) {
  110.                                 int data = 31;
  111.                                 Object obj = comboBox4.getSelectedItem(); // getSelectedItem()方法,返回当前下拉列表的值(以对象的形式返回)
  112.                                 // 取得当前月份

  113.                                 if (obj != null)
  114.                                         comboBox5.removeAllItems();// removeAllItems()方法,删除掉下拉列表的所有的项目,清空掉日

  115.                                 int mouth = Integer.valueOf(obj.toString());// 将对象(月份)变为整数
  116.                                 if (mouth == 4 || mouth == 6 || mouth == 9 || mouth == 11) {
  117.                                         data = 30;
  118.                                 } else if (mouth == 2) {
  119.                                         int year = Integer.valueOf(comboBox3.getSelectedItem()
  120.                                                         .toString());// 取得当前年号
  121.                                         if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))// 是闰年
  122.                                                 data = 29;
  123.                                         else
  124.                                                 data = 28;

  125.                                 }
  126.                                 for (int i = 1; i <= data; i++) {
  127.                                         comboBox5.addItem("" + i);
  128.                                 }// 添加号

  129.                         }
  130.                 });

  131.                 // 鼠标事件
  132.                 button1.addMouseListener(new MouseAdapter() {
  133.                         String sql = "select *from student_information";

  134.                         @Override
  135.                         public void mouseClicked(MouseEvent e) {
  136.                                 GetConn getconn = new GetConn();
  137.                                 Connection conn = getconn.getConnection();

  138.                                 try {
  139.                                         Statement statement1 = conn.createStatement();
  140.                                         ResultSet resultSet1 = statement1.executeQuery(sql);
  141.                                         while (resultSet1.next()) {
  142.                                                 int userID = resultSet1.getInt("userId");
  143.                                                 [color=Red]if (textField1.getText().equals("" + userID)) {
  144.                                                         button1.setBackground(Color.RED);
  145.                                                         JOptionPane.showMessageDialog(null, "此学号已存在");
  146.                                                         break;
  147.                                                 } else {
  148.                                                         int UserId = Integer.parseInt(textField1.getText());
  149.                                                         String Name = textField2.getText();
  150.                                                         String Sex = String.valueOf(comboBox2
  151.                                                                         .getSelectedItem());
  152.                                                         String birthday = String.valueOf(comboBox3
  153.                                                                         .getSelectedItem())
  154.                                                                         + String.valueOf(comboBox4
  155.                                                                                         .getSelectedItem())
  156.                                                                         + String.valueOf(comboBox5
  157.                                                                                         .getSelectedItem());
  158.                                                         int Birthday = Integer.parseInt(birthday);
  159.                                                         String Nation = String.valueOf(comboBox1
  160.                                                                         .getSelectedItem());
  161.                                                         int Class1 = Integer.parseInt(textField3.getText());
  162.                                                         String Zhuanye = textField4.getText();
  163.                                                         new InsertInformation(UserId, Name, Sex, Birthday,
  164.                                                                         Nation, Class1, Zhuanye);// 调用插入的方法,连接数据库等等操作
  165.                                                         button1.setBackground(Color.GREEN);
  166.                                                         JOptionPane.showMessageDialog(null, "插入信息成功");
  167.                                                         break;
  168.                                                 }
  169. [/color]
  170.                                         }

  171.                                 } catch (SQLException e1) {
  172.                                         // TODO Auto-generated catch block
  173.                                         e1.printStackTrace();
  174.                                 } finally {
  175.                                         if (conn != null) {
  176.                                                 try {
  177.                                                         conn.close();// 关闭当前数据库连接
  178.                                                 } catch (SQLException e1) {
  179.                                                         // TODO Auto-generated catch block
  180.                                                         e1.printStackTrace();
  181.                                                 }
  182.                                         }
  183.                                 }

  184.                         }

  185.                 });

  186.                 button2.addMouseListener(new MouseAdapter() {
  187.                         @Override
  188.                         public void mouseClicked(MouseEvent e) {
  189.                                 System.exit(0);
  190.                                 super.mouseClicked(e);
  191.                         }
  192.                 });

  193.         }

  194. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-7-4 10:46:27 | 显示全部楼层
这里要用到刷新数据库的知识吗?还是程序本身的存在问题,,不能沉,不能沉
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-4 11:12:04 | 显示全部楼层
本帖最后由 zlj19931010 于 2017-7-4 11:13 编辑

你单步调式下呗,没有你的数据库,我不能帮你调式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-19 13:37:04 | 显示全部楼层
把报的错误信息发上来看下,应该是你分析的那样,表中有主键,主键不能重复,第二次添加的时候重复,如果是这样就会报SQLException,如果不是这个错误,那应该是逻辑错误   如果重复 肯定else肯定不能执行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 03:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表