Sqlite登录授权失败

时间:2015-05-16 13:47:01

标签: java mysql sql sqlite jdbc

我写了一个所谓的'错误少'代码但我在使用应用程序时遇到了一些问题。 这是我的代码:

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,虽然它说密码不正确。 请帮忙!

1 个答案:

答案 0 :(得分:0)

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

Uncaught TypeError: Cannot set property 'innerHTML' of null

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

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 } 是一个OR运算符:您想查看是否有任何行匹配。当然,如果确实如此,你可以继续避免查看其他比赛。

但是为什么不使用提供的名称和密码的查询呢?

ok |= condition

看看你是否得到任何结果。