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.jdbcOdbcDr iver");
// java.sql.DriverManager.set LogStream( java.lang. System.out );
dbCon = DriverManager.getConnectio n (
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.AccessContro lException : access denied (java.lang.RuntimePermissi on accessClassInPackage.sun.j dbc.odbc )
at java.security.AccessContro lContext.c heckPermis sion(Compi led Code)
at java.security.AccessContro ller.check Permission (Compiled Code)
at java.lang.SecurityManager. checkPermi ssion(Comp iled Code)
at java.lang.SecurityManager. checkPacka geAccess(C ompiled Code)
at sun.applet.AppletSecurity. checkPacka geAccess(A ppletSecur ity.java:1 72)
at sun.applet.AppletClassLoad er.loadCla ss(AppletC lassLoader .java:107)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:237)
at java.lang.Class.forName0(N ative Method)
at java.lang.Class.forName(Co mpiled Code)
at HelloWorld.openDbConnectio n(Compiled Code)
at HelloWorld.paint(HelloWorl d.java:9)
at sun.awt.windows.WComponent Peer.handl eEvent(WCo mponentPee r.java:111 )
at java.awt.Component.dispatc hEventImpl (Component .java:2429 )
at java.awt.Container.dispatc hEventImpl (Container .java:1032 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:2289)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:258)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:68)
I think it has something to do with security, but I'am not sure. Any help would be welcome.
Thanx,
Henk
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.jdbcOdbcDr
// java.sql.DriverManager.set
dbCon = DriverManager.getConnectio
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.AccessContro
at java.security.AccessContro
at java.security.AccessContro
at java.lang.SecurityManager.
at java.lang.SecurityManager.
at sun.applet.AppletSecurity.
at sun.applet.AppletClassLoad
at java.lang.ClassLoader.load
at java.lang.Class.forName0(N
at java.lang.Class.forName(Co
at HelloWorld.openDbConnectio
at HelloWorld.paint(HelloWorl
at sun.awt.windows.WComponent
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
I think it has something to do with security, but I'am not sure. Any help would be welcome.
Thanx,
Henk
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
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
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...
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER