Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

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
0
hge
Asked:
hge
  • 3
  • 2
1 Solution
 
hgeAuthor Commented:
I'am using JDK 1.2 on both Win95 and Win98.
0
 
diakovCommented:
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
0
 
hgeAuthor Commented:
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...
0
 
diakovCommented:
Read carefully:

Again, from an applet you cannot connect to a local resource. ODBC is not a network protocol but Windows API so when you connect to it you use Win API. The JDBC ODBC bridge provides it through its native part. The security restrictions of the Java sandbox apply to loading and instantiating this class from within an applet.

You need a Internet bridging protocol to be able to export ODBC datasource on the net. Symantec offer dbAnywhere along with pure Java (on the applet side) level 3 JDBC driver for it. The server is a small program run on the same machoine as the ODBC data source.

Remember, you cannot connect to a remote machine with ODBC. And locally you can, but only from a Java application or signed applet.

Nik
0
 
diakovCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now