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() {Quote:}
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) {Quote:}
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)Quote:}
{
System.err.println(sqle);
textArea.append(text + newline);Quote:}*/
textField.selectAll();
Quote:}
}