0

I wish to create login page which uses Email Address and Password as log in details. Is there anyway to do this using Jackcess? I am avoiding ucanaccess method because it keeps giving me SQL exception error.

Here's the code for sign in page:

login.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent action){

            try {
                File file= new File("User_Details.accdb");
                Database data=DatabaseBuilder.open(file);
                if((file.exists())&&(!file.isDirectory())){

                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            }



        });

Here's the database:

public void actionPerformed(ActionEvent action) {
    if(action.getSource()==next){
        int x=0;
        String s1=t_name.getText();
        String s2=t_email.getText();
        char[]s3=pw.getPassword();
        char[]s4=c_pw.getPassword();
        String pass=new String(s3);
        String conf=new String(s4);
        String s5=t_phone.getText();
        Object s6=city.getSelectedItem();
        String s7=t_cc.getText();
        if((!s1.isEmpty())&&(!s2.isEmpty())&&(!pass.isEmpty())&&(!conf.isEmpty())&&(!s5.isEmpty())&&(!s7.isEmpty())&&(pass.equals(conf))){
            String file="C:/Users/Ameer Izwan/Documents/User_Details.accdb";
        try{
            Database db=DatabaseBuilder.create(Database.FileFormat.V2000,new File(file));
            Table table=new TableBuilder("Login")
            .addColumn(new ColumnBuilder("Email Address",DataType.TEXT))
            .addColumn(new ColumnBuilder("Name",DataType.TEXT))
            .addColumn(new ColumnBuilder("Password",DataType.TEXT))
            .addColumn(new ColumnBuilder("Phone No",DataType.TEXT))
            .addColumn(new ColumnBuilder("Cities",DataType.TEXT))
            .addColumn(new ColumnBuilder("Credit/Debit Card No",DataType.TEXT))
            .toTable(db);


            table.addRow(s2,s1,pass,s5,s6,s7);
            x++;
            if(x>0){    

                for(int i=0;i<=100;i++){
                    final int value=i;
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            bar.setValue(value);
                        }
                    });

This is the database after created and filled: https://i.stack.imgur.com/2UUh8.png

This is the sample input and output: https://i.stack.imgur.com/HMjzc.png

Do you guys have any idea using Jackcess for this?

  • What is the schema of the db? Does each row contain username and password? Do you want check the table for username and password and authenticate the user? – prasanth Apr 08 '17 at 00:14
  • I added the database code. For now, I'll just want to try one row first. I want to check the table for Email Address(primary key) and password. – MrBlock2274 Apr 08 '17 at 00:20

1 Answers1

1

From http://jackcess.sourceforge.net/ sample code, I managed to write a code that would work for you.

public boolean authenticate(String email, char[] password) {
   Table table = DatabaseBuilder.open(new File("C:/Users/Ameer Izwan/Documents/User_Details.accdb")).getTable("Login");
   Row row = CursorBuilder.findRow(table, 
            Collections.singletonMap("Email Address", email));
   if(row != null) {
     String p = row.get('Password');
   // if the password matches authenticate or else deny
   } else {
   // Dont authenticate
   }
}

NOTE: Make sure the file path is absolute or else row will return null

prasanth
  • 3,502
  • 4
  • 28
  • 42
  • How would I replace "test@company.com"? What index should I include? It supposed to be the data inside the database right? Many thanks for providing me a progress. – MrBlock2274 Apr 08 '17 at 00:37
  • You just need to pass the username and password you got from the login page to this method. You dont have to provide any index since we access the column by name. Yeah it is the data inside your database – prasanth Apr 08 '17 at 00:41
  • If your database or your table is very large, you can open them at the start of your program and reuse them throughout the life time of your application instead of opening each time in the method – prasanth Apr 08 '17 at 00:47
  • It seems findRow can't include two Strings inside singletonMap. How do you make it work? My database is still small. Maybe later it would help. Thanks. – MrBlock2274 Apr 08 '17 at 00:55
  • Collections.singletonMap can take two strings as arguments. – prasanth Apr 08 '17 at 01:07
  • Thanks. It seems that i need to call Table instead of directly call the table as it would be considered as a String rather than a table. But I guess the row is still null because I didn't get into the password block. – MrBlock2274 Apr 08 '17 at 01:31
  • Does the email present in the database? – prasanth Apr 08 '17 at 01:32
  • It is there. Have a look at this line: Row row=CursorBuilder.findRow(table, Collections.singletonMap("Email Address",t_email)); where t_email is textfield for email. – MrBlock2274 Apr 08 '17 at 01:52
  • Can you post a sample input and few rows in your table including the column names? – prasanth Apr 08 '17 at 01:54
  • Added sample input and output on question. – MrBlock2274 Apr 08 '17 at 15:29
  • Did you try reading the rows and see if you can get the username and password from the table? – prasanth Apr 08 '17 at 23:14
  • Apparently, my problem was the file name. I didn't put the full directory. I thought it was not necessary since the file existence was true. Now I feel bad. Many thanks for the help – MrBlock2274 Apr 09 '17 at 00:34