Problem with using JDBC in Applet (AccessDenied ! )

Problem with using JDBC in Applet (AccessDenied ! )

Post by Emre » Sat, 10 Mar 2001 02:26:53



Hi,

I'm a Java (JDK 1.3) primer and trying to create an applet that
can connect to my database and make some queries. I was trying
to convert an example from Ivor Horton's Java2 book. It was a
normal application and I customised it according to my database.
But I couldn't convert it to an applet.

So I tried a simpler applet and put the SQL connection into it,
it compiled but when it worked I received the following error
messages from the console:

--------------------------------------------------------------
C:\jdk1.3\bin>appletviewer applet.html

Exception occurred during event dispatching:

java.security.AccessControlException: access denied
(java.lang.RuntimePermission
accessClassInPackage.sun.jdbc.odbc)
at java.security.AccessControlContext.checkPermission
(AccessControlConte
xt.java:272)
at java.security.AccessController.checkPermission
(AccessController.java:
399)
at java.lang.SecurityManager.checkPermission
(SecurityManager.java:545)
at java.lang.SecurityManager.checkPackageAccess
(SecurityManager.java:150
1)
at sun.applet.AppletSecurity.checkPackageAccess
(AppletSecurity.java:169)
---------------------------------------------------------------

I'm a little bit confused because the similar code works OK as a
Java application but I have this trouble when it is in an
applet.

What must I do so that my applet can connect to my database?

Please bear in mind that I'm a primer and didn't use that
policytool, etc. :-)

Thanks in advance.

The sample code:
----------------------------------------------------------
/*
* Swing Version
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*; // For JDBC classes

public class TextDemo extends JApplet implements ActionListener
{
JTextField textField;
JTextArea textArea;
String newline = "\n";

//Hack to avoid annoying error message (1.1).
public TextDemo() {
getRootPane().putClientProperty
("defeatSystemEventQueueCheck",
Boolean.TRUE);

Quote:}

public void init() {
textField = new JTextField(20);
textField.addActionListener(this);

textArea = new JTextArea(5, 20);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea,

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

//Add Components to the Applet.
GridBagLayout gridBag = new GridBagLayout();
Container contentPane = getContentPane();
contentPane.setLayout(gridBag);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;

c.fill = GridBagConstraints.HORIZONTAL;
gridBag.setConstraints(textField, c);
contentPane.add(textField);

c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
gridBag.setConstraints(scrollPane, c);
contentPane.add(scrollPane);

Quote:}

public void actionPerformed(ActionEvent evt) {
String text = textField.getText();

// try
// {
// Load the driver class
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// Define the data source for the driver
String sourceURL = "jdbc:odbc:LocalSQLServer";

// Create a connection through the DriverManager
Connection databaseConnection =
DriverManager.getConnection(sourceURL);

Statement statement = databaseConnection.createStatement ();

/*ResultSet authorNames = statement.executeQuery("SELECT Name
FROM Film");*/

// Output the resultset data
/*while(authorNames.next())
System.out.println(authorNames.getString("Name"));*/
// }

/*catch(ClassNotFoundException cnfe)
{
System.err.println(cnfe);

Quote:}

catch(SQLException sqle)
{
System.err.println(sqle);

Quote:}*/

textArea.append(text + newline);
textField.selectAll();
Quote:}
}

 
 
 

Problem with using JDBC in Applet (AccessDenied ! )

Post by -=^ Cardano ^= » Sat, 10 Mar 2001 04:14:23


Try policytool.exe form jdk/bin directory to set up appletviewer policy.


Quote:> Hi,

> I'm a Java (JDK 1.3) primer and trying to create an applet that
> can connect to my database and make some queries. I was trying
> to convert an example from Ivor Horton's Java2 book. It was a
> normal application and I customised it according to my database.
> But I couldn't convert it to an applet.

> So I tried a simpler applet and put the SQL connection into it,
> it compiled but when it worked I received the following error
> messages from the console:

> --------------------------------------------------------------
> C:\jdk1.3\bin>appletviewer applet.html

> Exception occurred during event dispatching:

> java.security.AccessControlException: access denied
> (java.lang.RuntimePermission
> accessClassInPackage.sun.jdbc.odbc)
> at java.security.AccessControlContext.checkPermission
> (AccessControlConte
> xt.java:272)
> at java.security.AccessController.checkPermission
> (AccessController.java:
> 399)
> at java.lang.SecurityManager.checkPermission
> (SecurityManager.java:545)
> at java.lang.SecurityManager.checkPackageAccess
> (SecurityManager.java:150
> 1)
> at sun.applet.AppletSecurity.checkPackageAccess
> (AppletSecurity.java:169)
> ---------------------------------------------------------------

> I'm a little bit confused because the similar code works OK as a
> Java application but I have this trouble when it is in an
> applet.

> What must I do so that my applet can connect to my database?

> Please bear in mind that I'm a primer and didn't use that
> policytool, etc. :-)

> Thanks in advance.

> The sample code:
> ----------------------------------------------------------
> /*
> * Swing Version
> */

> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import java.sql.*; // For JDBC classes

> public class TextDemo extends JApplet implements ActionListener
> {
> JTextField textField;
> JTextArea textArea;
> String newline = "\n";

> //Hack to avoid annoying error message (1.1).
> public TextDemo() {
> getRootPane().putClientProperty
> ("defeatSystemEventQueueCheck",
> Boolean.TRUE);
> }

> public void init() {
> textField = new JTextField(20);
> textField.addActionListener(this);

> textArea = new JTextArea(5, 20);
> textArea.setEditable(false);
> JScrollPane scrollPane = new JScrollPane(textArea,

> JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

> JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

> //Add Components to the Applet.
> GridBagLayout gridBag = new GridBagLayout();
> Container contentPane = getContentPane();
> contentPane.setLayout(gridBag);
> GridBagConstraints c = new GridBagConstraints();
> c.gridwidth = GridBagConstraints.REMAINDER;

> c.fill = GridBagConstraints.HORIZONTAL;
> gridBag.setConstraints(textField, c);
> contentPane.add(textField);

> c.fill = GridBagConstraints.BOTH;
> c.weightx = 1.0;
> c.weighty = 1.0;
> gridBag.setConstraints(scrollPane, c);
> contentPane.add(scrollPane);
> }

> public void actionPerformed(ActionEvent evt) {
> String text = textField.getText();

> // try
> // {
> // Load the driver class
> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

> // Define the data source for the driver
> String sourceURL = "jdbc:odbc:LocalSQLServer";

> // Create a connection through the DriverManager
> Connection databaseConnection =
> DriverManager.getConnection(sourceURL);

> Statement statement = databaseConnection.createStatement ();

> /*ResultSet authorNames = statement.executeQuery("SELECT Name
> FROM Film");*/

> // Output the resultset data
> /*while(authorNames.next())
> System.out.println(authorNames.getString("Name"));*/
> // }

> /*catch(ClassNotFoundException cnfe)
> {
> System.err.println(cnfe);
> }
> catch(SQLException sqle)
> {
> System.err.println(sqle);
> }*/

> textArea.append(text + newline);
> textField.selectAll();
> }

> }


 
 
 

Problem with using JDBC in Applet (AccessDenied ! )

Post by Stefan Hob » Sat, 10 Mar 2001 07:50:53


Hi,

I think unsigned applets are not allowed to make database connections,
whereas signed applets and applications are viewed as secure enough to
do so. See the JDBC FAQ on http://www.javasoft.com/products/jdbc/ for
details.

Regards,
Stefan


> Hi,

> I'm a Java (JDK 1.3) primer and trying to create an applet that
> can connect to my database and make some queries. I was trying
> to convert an example from Ivor Horton's Java2 book. It was a
> normal application and I customised it according to my database.
> But I couldn't convert it to an applet.

> So I tried a simpler applet and put the SQL connection into it,
> it compiled but when it worked I received the following error
> messages from the console:

> --------------------------------------------------------------
> C:\jdk1.3\bin>appletviewer applet.html

> Exception occurred during event dispatching:

> java.security.AccessControlException: access denied
> (java.lang.RuntimePermission
> accessClassInPackage.sun.jdbc.odbc)
> at java.security.AccessControlContext.checkPermission
> (AccessControlConte
> xt.java:272)
> at java.security.AccessController.checkPermission
> (AccessController.java:
> 399)
> at java.lang.SecurityManager.checkPermission
> (SecurityManager.java:545)
> at java.lang.SecurityManager.checkPackageAccess
> (SecurityManager.java:150
> 1)
> at sun.applet.AppletSecurity.checkPackageAccess
> (AppletSecurity.java:169)
> ---------------------------------------------------------------

> I'm a little bit confused because the similar code works OK as a
> Java application but I have this trouble when it is in an
> applet.

> What must I do so that my applet can connect to my database?

> Please bear in mind that I'm a primer and didn't use that
> policytool, etc. :-)

> Thanks in advance.

> The sample code:
> ----------------------------------------------------------
> /*
> * Swing Version
> */

> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import java.sql.*; // For JDBC classes

> public class TextDemo extends JApplet implements ActionListener
> {
> JTextField textField;
> JTextArea textArea;
> String newline = "\n";

> //Hack to avoid annoying error message (1.1).
> public TextDemo() {
> getRootPane().putClientProperty
> ("defeatSystemEventQueueCheck",
> Boolean.TRUE);
> }

> public void init() {
> textField = new JTextField(20);
> textField.addActionListener(this);

> textArea = new JTextArea(5, 20);
> textArea.setEditable(false);
> JScrollPane scrollPane = new JScrollPane(textArea,

> JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

> JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

> //Add Components to the Applet.
> GridBagLayout gridBag = new GridBagLayout();
> Container contentPane = getContentPane();
> contentPane.setLayout(gridBag);
> GridBagConstraints c = new GridBagConstraints();
> c.gridwidth = GridBagConstraints.REMAINDER;

> c.fill = GridBagConstraints.HORIZONTAL;
> gridBag.setConstraints(textField, c);
> contentPane.add(textField);

> c.fill = GridBagConstraints.BOTH;
> c.weightx = 1.0;
> c.weighty = 1.0;
> gridBag.setConstraints(scrollPane, c);
> contentPane.add(scrollPane);
> }

> public void actionPerformed(ActionEvent evt) {
> String text = textField.getText();

> // try
> // {
> // Load the driver class
> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

> // Define the data source for the driver
> String sourceURL = "jdbc:odbc:LocalSQLServer";

> // Create a connection through the DriverManager
> Connection databaseConnection =
> DriverManager.getConnection(sourceURL);

> Statement statement = databaseConnection.createStatement ();

> /*ResultSet authorNames = statement.executeQuery("SELECT Name
> FROM Film");*/

> // Output the resultset data
> /*while(authorNames.next())
> System.out.println(authorNames.getString("Name"));*/
> // }

> /*catch(ClassNotFoundException cnfe)
> {
> System.err.println(cnfe);
> }
> catch(SQLException sqle)
> {
> System.err.println(sqle);
> }*/

> textArea.append(text + newline);
> textField.selectAll();
> }

> }

 
 
 

1. using JDBC to DB2 on OS/390 , and JDBC Applet Server

Hi.  We are using WebSphere Advanced Edition on Solaris8.  Our Java
applications run in this environment, WebSphere AE 3.5.5 / Solaris 8.
 Usually, we pull our data from Sybase via their JDBC driver, jConnect.
 I am now trying to figure out how to make a connection to our other
database, DB2, which runs on OS/390.

We are trying to solve this using IBM's JDBC Drivers.  My understanding
thus far is this:

* IBM has type 2 and type 3 drivers -- app driver, and net driver,
respectively.  
 - The app driver is typically select and used if WebSphere (or other
JDBC application) runs on the same local box as the database server
- The net driver is used of the WebSphere / JDBC application does not
reside on the same box as the DB2 application.

We are NOT looking to purchase a type 4 driver from a third-party vendor.

I've gotten some suggestions that the best way to make your JDBC
connection to DB2 6.1 on OS/390 (if your JDBC application is NOT on the
same box as OS/390), that "DB2 Connect" should be installed on the
Java/JDBC-enabled application.  This is an accpetable solution for us,
only that I haven't gotten it to work yet.  We are using JDBC 2.0
drivers from db2java.zip (after running usejdbc2.bat) from sqllib on my
windows machine.  I have gotten my Java application to successfully
connect and perform queries against DB2 running on Windows 2000, but not
on OS/390.

My question is this (the piece that I am missing) -- what is the link
between JDBC and DB2 Connect?  How do I get my Java application to talk
to DB2 Connect?  Do I somehow define my datasource in DB2 Connect.  If
so, how do I "register" it so that when do a lookup on the datasource,
it is able to find the definition created in DB2 Connect?  Under this
suggested setup, would I be using the net or app driver?

Also, on a separate but related note, I see two services on my Windows
2000 machine running DB2 6.1 -- "JDBC Applet Server" and "JDBC Applet
Server -- Control Center" .  They are listening on their default ports
6789, and 6790 respectively.  These services must have gotten installed
along with DB2 when I chose DB2 as my admin repository during my
WebSphere install.  Now, my question.  What exactly is this JDBC Applet
Server?  Is this on Windows only?  I've noticed that my Java code (using
JDBC) fails as soon as I turn off this server (it doesn't seem to matter
whether it's the net driver or app driver).  What does this JDBC
AppletServer play, and does it fit into any of this OS/390 I'm trying to
solve?

I've looked through a lot of documentation and links, other references,
both on the web and on IBM's site.  

Any help is greatly appreciated.

Thanks,
Tommy Tam

2. Windows 2000 is here - ADO 2.5 is ?

3. Test Data Generator - FREE Demo

4. Security problem with Java Applet using Oracle Thin JDBC

5. US-PA-GXPHARMA DOCUMENTUM CONSULTANT / ORACLE

6. Problem with running applet using JDBC!

7. Multiple Step OLE DB Operation generated errors

8. problem with Applets using JDBC

9. problem using JDBC proxy as applet

10. Applet Security in IE/Netscape when using JDBC

11. applet using ODBC-JDBC bridge