我已经放弃了旧问题,因为它不清楚并且有一堆错误,我发现“我认为”的解决方案更容易一些,尽管没有完全工作。修订:::::::: LognL 类
public final class LogInL extends JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
String Username;
String ID;
public LogInL() {
initComponents();
ButtonGroup();
}
private void backBActionPerformed(ActionEvent e) {
LoginMain login = new LoginMain();
login.setVisible(true);
this.dispose();
}
private void LoginBActionPerformed(ActionEvent e) {
if(prosecutorCB.isSelected())
{
try {
conn = SQLConnect.ConnectDb();
String sql = "SELECT prosecutors.username, criminalrecords.ID FROM prosecutors, criminalrecords "
+ "WHERE username = ? and ID = ?";
pst = conn.prepareStatement(sql);
pst.setString(1, usernameF.getText());
pst.setString(2, criminalIdF.getText());
Username = usernameF.getText();
System.out.println("Username is " + Username);
ID = criminalIdF.getText();
System.out.println("Criminal ID is " + ID);
rs = pst.executeQuery();
if(rs.next())
{
ProsecutorMain Frame = new ProsecutorMain(); // call Policemain class //display it
Frame.pack();
Frame.setVisible(true); // make it visible
System.out.println("Welcome to prosecutors");
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null,"<html>wrong username or criminal ID<br>"
+ "Criminal may not longer be in database");
}
}
catch (SQLException ex)
{
Logger.getLogger(LogInL.class.getName()).log(Level.SEVERE, null, ex);
}
}//end if proseutor
}//end logination
private void ButtonGroup()
{
ButtonGroup bg = new ButtonGroup();
bg.add(prosecutorCB);
bg.add(criminaldefenceCB);
}
修订:::::::: ProsecutorMain 类
public class ProsecutorMain extends JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
LogInL id = new LogInL();
String UserName;
public ProsecutorMain() throws SQLException
{
initComponents();
UserName = id.Username;
username.setText(UserName);
firstname.setText("blablabla");
incidentlocation.setText("kupa");
System.out.println(UserName);
}
private void logOutActionPerformed(ActionEvent e) {
int response = JOptionPane.showConfirmDialog(null, "<html> Are you sure you want to log out?",
"Confirm",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION)
{
}
if (response == JOptionPane.YES_OPTION)
{
this.dispose();
LogInL login = new LogInL();
login.setVisible(true);
JOptionPane.showMessageDialog(null,"You have been sucessfully logged out");
}
}
问题中的变量是用户名