java中的Sqlite子表摆动

时间:2015-01-21 20:50:49

标签: java swing sqlite jtable

所以我一直在尝试制作这个程序,为每个用户存储一些数据(一些简单的字符串)。它有登录,注册和主屏幕。我正在使用sqlite DB,我为登录做了一个表,一切正常。但我无法弄清楚如何做,是如何在用户注册时为每个用户创建另一个“子表”?我想要做的是,列出每个用户自己的数据的主框架(将成为该子表)。

PS。在成功登录时,登录框会在主框架中不稳定地填充JTable,用户可以在其中插入,删除或更改数据。任何人都可以帮助我吗?

如果它有帮助,那就是登录框架的代码(我只是要发布ActionListener部分):

LoginBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                String query = "select * from UserData where username =? and password =? ";
                PreparedStatement pst = connection.prepareStatement(query);
                pst.setString (1, userField.getText() );
                pst.setString (2, passField.getText() );

                ResultSet rs = pst.executeQuery();
                int count = 0;
                while (rs.next() ) {
                    count = count +1;   
                }
                if (userField.getText().trim().length() != 0 && passField.getPassword().length != 0) {
                    if (count == 1) {
                        JOptionPane.showMessageDialog(null, "Login was succesful !");
                        frmAccountManager.dispose();
                        SAM app = new SAM ();
                        app.setVisible (true);

                        try {

                            String query2 = "select * from UserData";
                            //this here is not going to be UserData table, but the subtable of the user
                            pst = connection.prepareStatement(query2);
                            rs = pst.executeQuery();

                            SAM.table.setModel(DbUtils.resultSetToTableModel(rs));

                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }



                    } else if (count >1) {
                        JOptionPane.showMessageDialog(null, "Duplicated Username and password !");
                    } else {
                        JOptionPane.showMessageDialog(null, "That username and password don't exist !");
                    }

                    rs.close ();
                    pst.close();
                } else JOptionPane.showMessageDialog(null, "You have to enter username and password !");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex);
            }
        }
    });

注册框架:

Regbtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {    
            try {
                if (passField.getText().equals(repPassField.getText() )) {
                    if (userField.getText().trim().length() != 0 && passField.getPassword().length != 0) {
                        String query = "insert or ignore into UserData (name, username, password) values (?, ?, ?)";

                        PreparedStatement pst = connection.prepareStatement(query);

                        pst.setString(1, nameField.getText() );
                        pst.setString(2, userField.getText() );
                        pst.setString(3, passField.getText() );             

                        dispose();      
                        pst.execute();

                        JOptionPane.showMessageDialog(null, "Registration was succesful !");

                        SAMlogin sam = new SAMlogin ();
                        sam.main(null);

                        pst.close();
                    } else 
                        JOptionPane.showMessageDialog(null, "You have to enter username and password");

                } else 
                    JOptionPane.showMessageDialog(null, "Passwords don't match");

            }catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex);
            }

        }
    });

0 个答案:

没有答案