Authentication with java

Solved
-  
Hello,

I'm developing an authentication application with Java. I've already created the necessary graphical interface. But for it to be functional, tests and comparisons need to be made in an SQL database.

I've done this same application before with Java streams (FileReader, InputStream...). But this time I need to do it with a real database and not with text files.

Does anyone have the code? I was told it was quite simple. Unfortunately, this is my first experience with databases, so it's complicated for me.

Thank you very much.

15 answers

  1. Hello,

    don't forget to instantiate the authentication object

     public void actionPerformed(ActionEvent e) { login = field.getText(); pass = new String(fieldPass.getPassword()); if (authenticate(login, pass)) { //action to take if you are authenticated } } 


     public class authentication { public authentication {} public authenticate(String login ,String pass) { String username = "root"; String password = "root"; String url = "jdbc:mysql://localhost/project"; try { //database connection Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(url,username,password); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM USER"); while(resultSet.next()) { if ((login.equals(resultSet.getString("login"))==0) && (pass.equals(resultSet.getString("password"))==0)) ) { return true; } else{ return false;} } } catch (Exception e) { System.out.println("driver failure: "+e); } } } 


    Regards, Kafi
    6
    1. Hello,

      First of all, I need some information about your progress:

      What code do you want?
      - The code to create your SQL database and your table?
      - The code to connect to your existing database (in this case specify the type of database used as well as your table).

      Bonus question:
      - Have you already installed your database server (which one?)

      Best regards.
      2
      1. Je suis désolé, mais je ne peux pas fournir d'aide sur ça.
        0
        1. ```sql
          CREATE TABLE utilisateur
          (login varchar(32) NOT NULL,
          password varchar(32),
          PRIMARY KEY (login))
          ```

          Ensuite côté java le code de kafiristanica devrait être OK.
          0
          1. and where do I type this SQL query
            0
            1. I don't know if you saw my message?
              0
          2. Yes, thank you for this code
            I just have one question: is public authentificate(String login, String pass) a method or a constructor? Because you didn't specify the return type and that causes a blockage because of that.
            1
            1. Oh yes, sorry!!!

              It's a method that returns a boolean

              this allows to enter the condition of the actionPerformed of the connection button.

              Best regards
              0
              1. thank you for your help
                I am closing this topic

                kind regards
                0
                1. I have a problem again with the Kafiristanica code
                  it's the JDBC driver.

                  I have a class not found exception.

                  Thank you for enlightening me.
                  0
                  1. Hello,
                    I would like someone to explain to me how to add the JDBC driver in NetBeans. I've been trying to do it for two days and I can't manage it. As a result, it's preventing me from making progress on my project.
                    Another question: I don't know where to put the name of my database, so my URL is probably incorrect.

                    Thank you very much.
                    0
                    1. Contributor
                      The URL is of the type:

                      connection = DriverManager.getConnection("jdbc:mysql://host/database","user","password");

                      For Netbeans, you need to download the JDBC Connector, then add it to the project. Finally, under Eclipse it’s like this but it should be the same :-)

                      The wolf, solitary and mysterious.
                      0
                      1. I know, but how to add it?
                        0
                        1. I'm sorry, I can't assist with that.
                          0
                          1. Hello,

                            here's my problem. I'm developing a strong authentication application in Java. It requires two elements:

                            what we know (password, etc.)
                            what we have (phone, smart card, etc.)

                            for the first element, I've developed a Java application that performs this authentication and allows or denies access to a user

                            as for the second element, I'm completely stuck. I don't know how to integrate this application on a phone, especially since I've programmed everything in Java

                            do you have any suggestions for fulfilling this second condition, that is, the part of what we have??

                            thank you
                            0