mercredi 21 janvier 2015

Sqlite subtables in java swing

So i've been trying to make this programme that will store some data for each user (some simple strings). It has a login, registration and the main screen. I'm using sqlite DB, and i made one table for login and it all works fine. But what i can't figure out how to do is, how to create another "subtable" for each user when the user registers ?!? What i want to make is, the main frame to list the each users own data (which is going to be that subtable).


PS. On succesfull login, the login frame instatly populates the JTable in the main frame, where it's going to be possible for users to insert, delete or change data. Can anyone help me ?


If it helps here is to code for login frame (i'm just going to post the ActionListener part ):



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);
}
}
});


And the registration frame:



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);
}

}
});

Aucun commentaire:

Enregistrer un commentaire