在按下JButton并将其存储到数据库表中时生成一个唯一的字符串

时间:2016-06-14 09:38:53

标签: java mysql

我需要生成介于0-9之间的序号,并希望在JTextfield中显示它与JCombobox中的值相结合(即jcombobox + count的元素) 按下JButton后的字符串

当我从组合框中选择第一个值并按下JButton时,计数应该从0开始,当选择组合框中的第二个元素时,计数应该再次从0开始,对于第二个元素,这应该适用于所有组合框的其他元素。

例如 - 当选择元素1并按下按钮时,组合输出应为10,11,12,13 ...但是,如果在此元素2之间选择 textfield中显示的输出应该从0开始,即20,21,22 ...... 对于元素3,计数应该从0开始,即30,31,32 ......

组合输出应显示在文本字段

    import java.sql.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;

    class Test extends JFrame implements ActionListener {
        JButton b1, b2;
        JComboBox jc;
        String name;
        JTextField t1;
        Connection con, conn;
        Statement st;
        PreparedStatement pst;
        ResultSet rs, rs1;

        Test() {
            setLayout(null);
            JLabel l1 = new JLabel("USER SELECTION :");
            l1.setBounds(100, 100, 150, 60);
            add(l1);
            jc = new JComboBox();
            jc.setBounds(230, 114, 120, 30);
            jc.addItem("SELECT");
            jc.addActionListener(this);
            add(jc);
            JButton b1 = new JButton("GENERATE PART NO. :");
            b1.setBounds(70, 340, 170, 30);
            b1.addActionListener(this);
            add(b1);
            t1 = new JTextField(10);
            t1.setBounds(270, 340, 200, 30);
            add(t1);
            JButton b2 = new JButton("BACK");
            b2.setBounds(190, 420, 120, 30);
            b2.addActionListener(this);
            add(b2);

            setSize(500, 500);
            setResizable(false);
            this.setVisible(true);
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost/d03", "root", "");
                st = con.createStatement();
                String s = "select Userdata from user1";
                rs = st.executeQuery(s);
                while (rs.next()) {
                    String name = rs.getString("Userdata");
                    jc.addItem(rs.getString("Userdata"));
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "ERROR");
            }

            finally {
                try {
                    st.close();
                    rs.close();
                    con.close();
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "ERROR CLOSE");
                }
            }
        }

        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()) {

                        String add1 = rs.getString("value");
                        t1.setEditable(false);

                        t1.setText("B" + add1);// B is the default value here which
                                                // is also concatenated
                    }
                }

                catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, "ERROR CLOSE");
                }
            }
            if (str.equals("BACK") == true) {
                new categ();
                setVisible(false);
            }
        }

        public static void main(String[] args) throws IOException {
            Test td = new Test();
        }
    }

0 个答案:

没有答案