我正在为我的次要项目制作java应用程序。我正在输入正确的数据,但它不允许登录。我很困惑为什么代码不工作。 我的数据库中有2个字段(用户名和密码)。我正在输入正确的数据,但它允许我登录 `
public class login extends JFrame implements ActionListener{
JButton jb1;
JTextField tf3,tf1,tf2;
ResultSet rs;
String s2,s1,s3;
String a;
Connection con;
login(){
Container c =getContentPane();
c.setLayout(null);
c.setBackground(Color.orange);
jb1=new JButton("submit");
jb1.addActionListener(this);
jb1.setBounds(180,370,100,50);
JLabel jl1=new JLabel("username");
jl1.setBounds(10,60,100,50);
JLabel jl2=new JLabel("password");
jl2.setBounds(10,110,100,50);
JLabel jl3=new JLabel("user or emp");
jl3.setBounds(10,160,100,50);
tf1= new JTextField();
tf1.setBounds(110,70,200,40);
tf2= new JTextField();
tf2.setBounds(110,120,200,40);
tf3=new JTextField();tf3.setBounds(110,180,200,40);
tf1.setText("username");
tf2.setText("password");
c.add(tf1);c.add(tf2);c.add(tf3);
c.add(jl1);c.add(jl2);c.add(jl3);
c.add(jb1);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb1){
s1 = tf1.getText();
s2 = tf2.getText();
s3 = tf3.getText();
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "hr");
PreparedStatement ps = con.prepareStatement("select * from login ");
rs = ps.executeQuery();
while(rs.next()) {
String username = rs.getString("username");
String pass = rs.getString("pass");
if ((s1.equals(username)) && (s2.equals(pass))) {
if (s3.equals("Admin")) {
dispose();
home f=new home();
f.setSize(500,500);
f.setTitle("Bank Management System");
f.setVisible(true);
} else if (s3.equals("user")) {
dispose();
} else {
dispose();
}
} else {
JOptionPane.showMessageDialog(null, "User name and password do" + " not match!","ALERT!",JOptionPane.ERROR_MESSAGE);
break;
}
}
}
catch(Exception ex)
{
System.out.println( ex);
}
}
}
public static void main(String[] args) {
login af =new login();
af.setSize(500,500);
af.setTitle("login");
af.setVisible(true);
}
}
`