Link to home
Start Free TrialLog in
Avatar of hge
hge

asked on

Problems using jdbc-odbc bridge

I'am quite new with Java and have some problems using the jdbc-odbc bridge when I try to access a local database via an applet. Following is the applet code.

import java.applet.Applet;
import java.awt.Graphics;
import java.sql.*;

public class HelloWorld extends Applet {
    public void paint(Graphics g) {
                boolean bConnect;
                        
                        bConnect = openDbConnection();
            
        g.drawString("Hello world!", 50, 25);
                        
    }
            
            public boolean openDbConnection(){
                boolean bConnect = true;
        Connection dbCon;
                        String url = "jdbc:odbc:my_mdb";
                        
        try {
                          // Load the jdbc-odbc bridge driver
                  Class.forName ("sun.jdbc.odbc.jdbcOdbcDriver");
                              
                  //        java.sql.DriverManager.setLogStream(java.lang.System.out);
                        
                              dbCon = DriverManager.getConnection (
                                 url
                                                            , ""
                                                            , ""
                                                            );
                        }
                        catch (SQLException ex) {
                          // A SQLException was generated.  Catch it and
                  // display the error information.  Note that there
                  // could be multiple error objects chained together
              System.out.println ("\n*** SQLException caught ***\n");      
                              while (ex != null) {
                    System.out.println ("SQLState: " + ex.getSQLState ());
                    System.out.println ("Message:  " + ex.getMessage ());
                    System.out.println ("Vendor:   " + ex.getErrorCode ());
                    ex = ex.getNextException ();
            System.out.println ("");
                              }
                        }
            catch (java.lang.Exception ex) {
                  // Got some other type of exception.  Dump it.            
                              ex.printStackTrace ();
                        }
                        
                        return bConnect;
            }
}

If I run this applet with the AppletViewer, it will output the following messages.

Starting appletviewer for C:\JavaDev\HelloWorld.html
java.security.AccessControlException: access denied (java.lang.RuntimePermission accessClassInPackage.sun.jdbc.odbc )
      at java.security.AccessControlContext.checkPermission(Compiled Code)
      at java.security.AccessController.checkPermission(Compiled Code)
      at java.lang.SecurityManager.checkPermission(Compiled Code)
      at java.lang.SecurityManager.checkPackageAccess(Compiled Code)
      at sun.applet.AppletSecurity.checkPackageAccess(AppletSecurity.java:172)
      at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:107)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:237)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Compiled Code)
      at HelloWorld.openDbConnection(Compiled Code)
      at HelloWorld.paint(HelloWorld.java:9)
      at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:111)
      at java.awt.Component.dispatchEventImpl(Component.java:2429)
      at java.awt.Container.dispatchEventImpl(Container.java:1032)
      at java.awt.Component.dispatchEvent(Component.java:2289)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:258)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:68)

I think it has something to do with security, but I'am not sure. Any help would be welcome.

Thanx,
Henk
Avatar of hge
hge

ASKER

I'am using JDK 1.2 on both Win95 and Win98.
ODBC datasource is a local resource. The applet comes from a remote machine (the web server), runs in the browser, and because of security reasons it is not allowed to use local resources like files, etc, and ODBC data sources as well.
If you want to do that from an applet (which is weird to me) you have to sign your applet. But using ODBC datasource from the users machine is not a good idea, since not all users have the database file, and actually it is only you that have it.
I assume you want to use database from the applet to connect to a database server. I can give you an example how to do this.

- You can use the dbAnywhere server from Symantec (they have an evaluation download at www.symantec.com) to publish local ODBC data source on the Internet and make it accessible through a URL.
In the package there you will find what connect strings you need to connect to the server. If you don't want to sign your applet you have to put the dbAnywhere server on the same machine as the Web server from which the applet code originates. This is another restriction to the applets, they cannot connect ot other than their origin IPs. Of course you can always sign your applet, but this costs money.

Hope this helps,
  Nik
Avatar of hge

ASKER

The answer didn't make any sense. It's not important whether I connect to a local or remote database when I use ODBC. The problem arises when the 'class.forName()' is performed.
This is before the connection is made. By the way, I tried a datasource that pointed to a remote database and the same error occured. So...
ASKER CERTIFIED SOLUTION
Avatar of diakov
diakov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Remark:
You cannot load/instantiate the JDBC ODBC bridge from an unsigned applet because it has a native part in it, and because this would allow you to connect to local resources.