gpt4 book ai didi

java - SQLite登录授权失败

转载 作者:行者123 更新时间:2023-11-29 22:25:52 24 4
gpt4 key购买 nike

我编写了一个所谓的“错误较少”代码,但在使用该应用程序时遇到了某些问题。这是我的代码:

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Login implements ActionListener {

Connection conn1 = null;
Statement stmt1 = null;

Connection conn2 = null;
Statement stmt2 = null;

Connection conn3 = null;
Statement stmt3 = null;

JFrame frame = new JFrame("Login Window");
JPanel startPanel = new JPanel();
JPanel adminPanel = new JPanel();
JPanel engineerPanel = new JPanel();

JButton adminLogin = new JButton("Admin Login");
JButton engineerLogin = new JButton("Engineer Login");

JTextField adminUsername = new JTextField();
JPasswordField adminPassword = new JPasswordField();
JButton adminLog = new JButton("Login");
JButton adminBack = new JButton("Go Back");

JComboBox engineerUsername = new JComboBox();
JPasswordField engineerPassword = new JPasswordField();
JButton engineerLog = new JButton("Login");
JButton engineerBack = new JButton("Go Back");

public Login(){
//Establishing connection with database
conn1 = sqliteConnection.dbConnector();

frame.setLayout(new GridLayout(1, 1));

/*
Setting up the startPanel
*/
startPanel.setLayout(new GridLayout(3, 3, 15, 15));

// Row 1
startPanel.add(new JLabel(" "));

JLabel loginType = new JLabel(" SELECT LOGIN TYPE");
loginType.setFont(new Font("Tahoma", Font.BOLD, 12));
startPanel.add(loginType);

startPanel.add(new JLabel(" "));

// Row 2
startPanel.add(new JLabel(" "));
startPanel.add(adminLogin);
startPanel.add(new JLabel(" "));

// Row 3
startPanel.add(new JLabel(" "));
startPanel.add(engineerLogin);
startPanel.add(new JLabel(" "));

adminLogin.addActionListener(this);
engineerLogin.addActionListener(this);

/*
Setting up adminPanel
*/
adminPanel.setLayout(new GridLayout(4, 2, 15, 15));

// Row 1
adminPanel.add(new JLabel("Admin Login"));
adminPanel.add(new JLabel(" "));

// Row 2
adminPanel.add(new JLabel("Username"));
adminPanel.add(adminUsername);

// Row 3
adminPanel.add(new JLabel("Password"));
adminPanel.add(adminPassword);

// Row 4
adminPanel.add(adminLog);
adminPanel.add(adminBack);

adminLog.addActionListener(this);
adminBack.addActionListener(this);

//Initial Visibility False
adminPanel.setVisible(false);

/*
Setting up engineerPanel
*/
engineerPanel.setLayout(new GridLayout(4, 2, 15, 15));

// Row 1
engineerPanel.add(new JLabel("Engineer Login"));
engineerPanel.add(new JLabel(" "));

// Row 2

try{
Class.forName("org.sqlite.JDBC");
conn1.setAutoCommit(false);
stmt1 = conn1.createStatement();
ResultSet rs1 = stmt1.executeQuery( "SELECT * FROM EngineerData;" );

List<String> engineerNamesList = new ArrayList<String>();

while ( rs1.next() ){
String name = rs1.getString("Name");
engineerNamesList.add(name);
}

// Converting array list to array
String[] engineerNames = new String[engineerNamesList.size()];
engineerNamesList.toArray(engineerNames);

// Adding array into combo-box
for (String en : engineerNames){
engineerUsername.addItem(en);
}

rs1.close();
stmt1.close();
conn1.close();
}
catch ( Exception e1 ) {
JOptionPane.showMessageDialog(null, e1);
}

engineerPanel.add(new JLabel("Engineer Name"));
engineerPanel.add(engineerUsername);

// Row 3
engineerPanel.add(new JLabel("Password"));
engineerPanel.add(engineerPassword);

// Row 4
engineerPanel.add(engineerLog);
engineerPanel.add(engineerBack);

engineerLog.addActionListener(this);
engineerBack.addActionListener(this);

//Initial Visibility False
engineerPanel.setVisible(false);



frame.setSize(500, 200);
frame.setResizable(false);
frame.add(startPanel);
frame.setVisible(true);

}

//Method to convert integar array list to integar array
public int[] convertIntegers(List<Integer> integers)
{
int[] ret = new int[integers.size()];
for (int i=0; i < ret.length; i++)
{
ret[i] = integers.get(i).intValue();
}
return ret;
}

//Admin login method
public void adminLogin(){

String adminUser = adminUsername.getText();
String adminPass = adminPassword.getText();

String adminUserDB = null;
String adminPassDB = null;

conn2 = sqliteConnection.dbConnector();

try{
Class.forName("org.sqlite.JDBC");
conn2.setAutoCommit(false);
stmt2 = conn2.createStatement();
ResultSet rs2 = stmt2.executeQuery( "SELECT * FROM AdminData;" );

adminUserDB = rs2.getString("Username");
adminPassDB = rs2.getString("Password");

conn2.close();
stmt2.close();
rs2.close();
}
catch (Exception e2){

}

if (adminUser.equals(adminUserDB) && adminPass.equals(adminPassDB)){
AdminClass ac = new AdminClass();

frame.dispose();
}

else if (adminUser.equals(adminUserDB) && adminPass != adminPassDB){
JOptionPane.showMessageDialog(null, "Incorrect Password.\nPlease enter again.");
}

else if (adminUser != adminUserDB && adminPass.equals(adminPassDB)){
JOptionPane.showMessageDialog(null, "Incorrect Username.\nPlease enter again.");
}

else if (adminUser != adminUserDB && adminPass != adminPassDB){
JOptionPane.showMessageDialog(null, "Incorrect Username and Password.\nPlease enter again.");
}


}

//Engineer login method
public void engineerLogin(){

String engineerUser = engineerUsername.getSelectedItem().toString();
String engineerPass = engineerPassword.getText();

List<String> engineerNamesList = new ArrayList<String>();
List<String> engineerPasswordsList = new ArrayList<String>();
ArrayList<Integer> uniqueIDList = new ArrayList<Integer>();

conn3 = sqliteConnection.dbConnector();

try{
Class.forName("org.sqlite.JDBC");
conn3.setAutoCommit(false);
stmt3 = conn3.createStatement();
ResultSet rs3 = stmt3.executeQuery( "SELECT * FROM EngineerData;" );

while ( rs3.next() ){

int uniqueId = rs3.getInt("UniqueId");
String engineerUserDB = rs3.getString("Name");
String engineerPassDB = rs3.getString("Password");

//Adding data from database to variables that exist in our code
engineerNamesList.add(engineerUserDB);
engineerPasswordsList.add(engineerPassDB);
uniqueIDList.add(uniqueId);

}

conn3.close();
stmt3.close();
rs3.close();
}
catch (Exception e3){

}

// Creating usable arrays from array lists
String[] engineerNames = new String[engineerNamesList.size()];
engineerNamesList.toArray(engineerNames);

String[] engineerPasswords = new String[engineerPasswordsList.size()];
engineerPasswordsList.toArray(engineerPasswords);

int[] uniqueIDs = convertIntegers(uniqueIDList);

for (int i = 0; i < engineerNames.length; i++){


boolean condition = (engineerUser.equals(engineerNames[i]) && engineerPass.equals(engineerPasswords[i]));

if (condition){

frame.dispose();

EngineerPanel ep = new EngineerPanel();

//This ID is the identifier of the engineer
//This will be used to generate data only for his particular project
ep.setUniqueID(uniqueIDs[i]);

break;
}

else if (i>= 1 && condition != true){
JOptionPane.showMessageDialog(null, "Incorrect Password");
continue;
}

}

}

public void actionPerformed(ActionEvent e){

if (e.getSource() == adminLogin){

startPanel.setVisible(false);
frame.remove(startPanel);
frame.add(adminPanel);
adminPanel.setVisible(true);


}

if (e.getSource() == engineerLogin){

startPanel.setVisible(false);
frame.remove(startPanel);
frame.add(engineerPanel);
engineerPanel.setVisible(true);
}

if (e.getSource() == adminBack){

adminUsername.setText(null);
adminPassword.setText(null);
adminPanel.setVisible(false);
frame.remove(adminPanel);
frame.add(startPanel);
startPanel.setVisible(true);
}

if (e.getSource() == engineerBack){

engineerPassword.setText(null);
engineerPanel.setVisible(false);
frame.remove(engineerPanel);
frame.add(startPanel);
startPanel.setVisible(true);
}

if (e.getSource() == adminLog){
adminLogin();
}

if (e.getSource() == engineerLog){
engineerLogin();
}
}
}

所以你可以看到我正在使用 sqlite 数据库。工程师授权仅适用于第一位工程师,但现在适用于所有工程师。即使数据正确,也说明用户数据不正确。即使我继续单击“确定”,JOptionPane 仍不断弹出。单击几次后,它只会使用下一个 JFrame,尽管它说密码不正确。请帮忙!

最佳答案

如果我理解你的问题,你的逻辑就有缺陷。你这样做:

for (int i = 0; i < engineerNames.length; i++){
boolean condition = ...
if (condition){
// OK
} else {
// show error
}
}

因此,一旦发现输入的信息与数据库行不匹配,就会显示错误。因此,第一个对象的一切都很好,因为第一行将匹配。但对于第二个对象,它将显示错误,因为您已经输入了第二行的数据,而第一行将不匹配。然后你“继续”,这样你就永远不会到达第二个数据库行。你的逻辑应该是:

boolean ok=false;
for (int i = 0; i < engineerNames.length; i++){
boolean condition = ...
ok |= condition;

}
if (ok){
// OK
} else {
// show error
}

ok |= 条件 是一个 OR 运算符:您想要查看是否有任何行匹配。当然,如果 ok 为 true,您可以继续避免查看其他比赛。

但实际上为什么不使用提供的名称和密码进行查询呢?

SELECT * FROM EngineerData where Name=? and Password=?

看看是否得到任何结果。

关于java - SQLite登录授权失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30276456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com