【问题标题】:Creating string by combining value from combobox and serial count通过组合组合框中的值和序列计数来创建字符串
【发布时间】:2016-10-16 03:35:11
【问题描述】:

现在我的 count 输出对于组合框中的每个元素都是 0,即它就像 B10、B20、B30(B 是默认值,下一项是数据库中的值,0 表示此连接字符串中的计数)...我的人数没有增加

当我从 Jcombobox 中选择一个值时,我应该怎么做才能增加计数 并按下按钮,即我得到 B10,B11,B12,B20,B21,B22,B30,B31,B32

           public void actionPerformed(ActionEvent ae) {
           String str = ae.getActionCommand();
           if (str.equals("GENERATE PART NO. :")) {
           try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/d03", "root", "");
            st = con.createStatement();

            String s = "select value from user1 where Userdata='" + jc.getSelectedItem() + "'";

            rs = st.executeQuery(s);
            t1.getText();
            if (rs.next()) {
                int j = 0;
                String add1 = rs.getString("value");
                t1.setEditable(false);

                String str9 = new String();
                str9 = "B" + add1; //B is the default value, add1 is the value from database  

                String str10 = new String();
                str10 = str9 + j;
                String query = "select MC from final";
                ResultSet rs1 = st.executeQuery(query);
                while (rs1.next()) {
                    if (str10.equals(rs1)) {
                        j = j + 1;
                        j=new Integer(j+1);
                        t1.setText(str10);
                    } else {
                        t1.setText(str10);
                    }
                }
            }
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost/d03", "root", "");

                String s1 = ("insert into final(MC)values(?)");
                PreparedStatement pstm = con.prepareStatement(s1);
                pstm.setString(1, t1.getText());
                int rowi = pstm.executeUpdate();

                if (rowi > 0) {
                    JOptionPane.showMessageDialog(null, "DATA INSERTED");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "ERROR CLOSE");
            }

        }

【问题讨论】:

    标签: java


    【解决方案1】:

    答案很简单: 将str10 = str9 + j; 移动到while 正文中。

    您的(代码片段)应如下所示:

      /*  if */while(rs.next()) {
           int j = 0;
            String add1 = rs.getString("value");
            t1.setEditable(false);
    
       // String str9; //= new String(); redundant
            String str9 = "B" + add1; //B is the default value add1 is the value from database  
            String str10 = str9;
            String query = "select MC from final";
            ResultSet rs1 = st.executeQuery(query);
            while (rs1.next()) {   
                if (str10.equals(rs1.getString("MC")) {
                    j++; //j = j + 1;
                   // j=new Integer(j+1); you simply increment j twice
                    str10 = str9 + j;// EDITED LINE!
                }
                t1.setText(str10);  
            }  
        }
    

    【讨论】:

    • 最后一个值仍然显示 0(第一个),即 B10(对于组合框的第一个元素)B20(对于第二个元素)等等。它应该增加但它不会增加
    • @user6443988 请立即尝试
    • @user6443988 我希望您采用带有注释行的更正代码// EDITED LINE!?
    • 如果您愿意,可以尝试任何其他方法
    • @user6443988 1. 我猜while(rs.next()) 应该是if(rs.next()),2。 t1.getText(); 是什么意思-丢弃的值?主要问题:查看上面的固定文本....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多