Solved

Problems using jdbc-odbc bridge

Posted on 1999-01-28
5
413 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

920 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

15 Experts available now in Live!

Get 1:1 Help Now