3

I am trying to figure out why my code is getting sent to the exception catch block and how to make this part of my log-in work correctly. The problem seems to be in Class.forName(driver); While debugging I noticed that I get an error, "variable source not available, source compiled with-out g-option". Is this the reason my code will not move onto the next step? if it is what do I need to fix it, and what does it mean?

I do have imported in my program.....

import java.sql.*;
import javax.swing.JOptionPane;
import java.sql.DriverManager;

 private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {                                       

        try {
            String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        Class.forName(driver);
        String db = "jdbc:odbc:db1.mdb";
        con = DriverManager.getConnection(db);
        st = con.createStatement();
        System.out.println("it actually ready this set of code!");
        String un = UserName.getText().trim();
        String pw = Password.getText().trim();
        String sql = "select user,pass from Table2 where user='"+un+"'and pass='"+pw+"'";
        rs=st.executeQuery(sql);
        int count = 0;
        while(rs.next()){
            count = count+1;
        }
        if (count==1){
            JOptionPane.showMessageDialog(null,"User, Found Access Granted!");
        }
        else if (count>1){
            JOptionPane.showMessageDialog(null,"Duplicate User, Access Denied!");
        } 
            else {
            JOptionPane.showMessageDialog(null, "user doesn't exsist. ");
             }

        } catch (Exception ex){
            System.out.println("exception 2 ");
        }

        // TODO add your handling code here:
    }   
CAPTiNDANCE
  • 87
  • 1
  • 7

2 Answers2

1

Try removing .mdb from String db = "jdbc:odbc:db1.mdb"; and simply write String db = "jdbc:odbc:db1";

This might work for you!

Note this is gonna work on or below Java runtime 7 only Since Java 8, the JDBC-ODBC Driver support has been removed. You can still do something like this to connect to MSAccess DB if you want.

Alternatively, you can use one of the many databases for which free JDBC drivers are available, like MySQL, PostgreSQL, SQLServer etc.

Lalit Rao
  • 541
  • 4
  • 22
  • 1
    I did make the change you suggested, and thank you for pointing this out. I am still getting the error. While debugging, the error is happening before I get to this string. The problem is at "Class.forName(driver);" I just don't know how to get it to go past this line of code. – CAPTiNDANCE May 07 '15 at 16:25
  • 1
    Which JDK version you are using? 7 or 8? – Lalit Rao May 07 '15 at 16:27
  • 1
    I am using java 8 ( sorry had to remember where to find the file ) – CAPTiNDANCE May 07 '15 at 16:31
  • 1
    Then you cant access MS Access, visit the link i provided in the answer above, download those 4 jars, add it in your library and things will work for you. Alternatively, download lower version of Java – Lalit Rao May 07 '15 at 16:33
  • You know I think your right I might need to try using one of those other options mentioned above. I had no idea my method was no longer supported. I suppose I need to find another way to accomplish this. Also using one of your suggested options listed above. Thank you! You have been very helpful. – CAPTiNDANCE May 07 '15 at 16:36
  • 1
    @CAPTiNDANCE Anytime mate :) – Lalit Rao May 07 '15 at 16:36
1

Get database connection object as below:

String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=db1.mdb;";
Connection conn = DriverManager.getConnection(database, "", "");

Also add code to print exception stack trace:

catch (Exception ex) {
    System.out.println("exception 2 ");         
    ex.printStackTrace();
}
Rajesh
  • 2,120
  • 1
  • 10
  • 14
  • Thank you for showing me how to do the ex.printStackTrace(); I wasn't sure how to do that ( though I wanted to ) I will attempt what you have posted here. and see how that works out. – CAPTiNDANCE May 07 '15 at 16:44
  • ok I have made adjustments to how this is Written. I am now getting the following errors: – CAPTiNDANCE May 07 '15 at 16:48
  • it won't let me post all of the errors so I will post the first 2 since I think the are relevant. atjava.sql.SQLException: No suitable driver found for jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=db1.mdb; java.sql.DriverManager.getConnection(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:247) – CAPTiNDANCE May 07 '15 at 16:52
  • @captindance : Go through this link for Java 8 JDBC-ODBC : – Rajesh May 07 '15 at 16:54