0

I have this code, I use it for connect to my Access database. If I try to execute it from eclipse, it works. But if I try to create a jar file then I run it I have this error:

DEBUG [AWT-EventQueue-0] (MyLog4J.java:45) - java.lang.NullPointerException
        at sun.jdbc.odbc.JdbcOdbcDriver.initialize(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at esempio.Visualizzazioni_Access.<init>(Visualizzazioni_Access.java:40)

        at Scontrini.abstractScontrini.apriDB(abstractScontrini.java:72)
        at Scontrini.PanelScontrini.creaSwing(PanelScontrini.java:60)
        at esp.TestaFrame$MenuLookDemo22$4.actionPerformed(TestaFrame.java:289)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.AbstractButton.doClick(Unknown Source)
        at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
        at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown
Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$400(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)

This is the code that I use for connect to database:

public Visualizzazioni_Access() {
        try {
            log=new MyLog4J();
            // Load MS accces driver class
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
            String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:/CassaGestionale.accdb";
            db = DriverManager.getConnection(url, "", "");
            connesso=true;
            System.out.println("Connection Succesfull");
        } catch (Exception e) {
            connesso=false;
            log.logStackTrace(e);
        }
    }

The error is on this line code:

db = DriverManager.getConnection(url, "", "");

Where is my error?

Jim Garrison
  • 81,234
  • 19
  • 144
  • 183
bircastri
  • 2,707
  • 7
  • 38
  • 96
  • You use Odbc, so you must add `Data Sources`. – Rong Nguyen Jan 24 '15 at 06:49
  • But, where I must add Data Sourcers? I have see , there is a procedure in Administrative tool to create a data sources, but I want to use this database from USB Key, so if I insert the usb key in a PC1 I can use it, if I insert the usb key in another PC I can also use it. It's possibile? or not? – bircastri Jan 24 '15 at 12:05
  • @RongNguyen That is not necessary if you use the connection string as in the code in the question. – Mark Rotteveel Jan 24 '15 at 12:27
  • I have try this: I have compile this code with java 1.6 in eclipse. But I have java 1.8 in my PC, so I have copy all jar file in jre 1.6 directory and launch it from that folder, the program works. I think the problem is the version of java. – bircastri Jan 25 '15 at 15:16
  • 1
    The JDBC-ODBC bridge driver has been removed in Java 8, so I'm not surprised you're having problems. I would recommend using UCanAccess instead, see [this question](http://stackoverflow.com/questions/21955256/) for more info. – Luke Woodward Jan 25 '15 at 19:01

0 Answers0