Solved

Problems using jdbc-odbc bridge

Posted on 1999-01-28
5
410 Views
Last Modified: 2013-11-23
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
Comment
Question by:hge
  • 3
  • 2
5 Comments
 

Author Comment

by:hge
ID: 1234849
I'am using JDK 1.2 on both Win95 and Win98.
0
 
LVL 8

Expert Comment

by:diakov
ID: 1234850
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
 

Author Comment

by:hge
ID: 1234851
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
 
LVL 8

Accepted Solution

by:
diakov earned 50 total points
ID: 1234852
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
 
LVL 8

Expert Comment

by:diakov
ID: 1234853
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now