By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

Chart Applet "Applet not initialized"

JustRun
100+
P: 127
Hi,

I'm new to Java "I loved it" and i'm stuck with a Chart Applet
i'm trying to give the pie chart its values from my database.
Actually the database class works when i tested it in main method buut when i put it inside the applet it gives me this
"java.security.AccessControlException: access denied (java.util.PropertyPermission file.encoding read)"

Also for the pie chart itself, it works fine as an application, but when i put it in the applet it says: "The applet is sot initialized"

I tried to make it as Thread and put the connection in the run() but the same error.

// Database Class
Expand|Select|Wrap|Line Numbers
  1. import java.sql.*;
  2.  
  3. public class DBConnection {
  4.     String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  5.     String url = "jdbc:odbc:BrokerSystemDSN";
  6.     String query = "SELECT CompanyName, ExchangeRate FROM StochHistoryTable";
  7.     String text;
  8.     public Connection cnn;
  9.  
  10.     public DBConnection(){
  11.         try{
  12.             Class.forName(driver).newInstance();
  13.             System.out.println("Driver Loaded");    
  14.         }catch(Exception ex){
  15.             System.out.println("Failed to load driver");
  16.         }
  17.     }
  18.  
  19.     public void OpenConnection() {
  20.         try{
  21.             cnn = DriverManager.getConnection(url);
  22.             System.out.println("Connection Established");
  23.         }catch(SQLException e){
  24.             System.out.println("Failed to establish Connection");
  25.         }
  26.     }    
  27.  
  28.     public void CloseConnection() {
  29.         try{
  30.             cnn.close();
  31.         }catch(SQLException eClose){
  32.             System.out.println("cannot close database");
  33.         }
  34.     }
  35.  
  36.     public Connection getConnection(){
  37.         return cnn;
  38.     }
  39.  
  40.     public void TestStatment()
  41.     {
  42.         try {
  43.             Statement stmt = cnn.createStatement();
  44.               ResultSet rs = stmt.executeQuery(query);
  45.               while (rs.next()){
  46.                 String tmpName = rs.getString("CompanyName");
  47.                   float tmpPrice = rs.getFloat("ExchangeRate");
  48.                   text += tmpName + "     " + tmpPrice;
  49.                   System.out.println(text);
  50.               }        
  51.               stmt.close();
  52.               cnn.close();
  53.         }
  54.         catch (Exception ex) {
  55.             System.out.println("cannot create statement");
  56.         }
  57.     }
  58.     // Tests
  59.     public static void main (String args[]){
  60.         DBConnection dBConnection = new DBConnection();
  61.         dBConnection.OpenConnection();
  62.         dBConnection.TestStatment();
  63.     }    
  64. }
  65.  
// APPLET Code

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * @(#)OutputApplet.java
  3.  *
  4.  * OutputApplet Applet application
  5.  *
  6.  * @author 
  7.  * @version 1.00 2008/9/13
  8.  */
  9.  
  10. import java.awt.*;
  11. import java.applet.*;
  12. import java.awt.Graphics;
  13. import java.util.Vector;
  14. import java.sql.*;
  15.  
  16. public class OutputApplet extends Applet implements Runnable{
  17.     DBConnection dBConnection = new DBConnection();
  18.     private String text;
  19.     private Statement stmt;
  20.     private ResultSet rs;
  21.     private String query = "SELECT CompanyName, ExchangeRate FROM StochHistoryTable";
  22.     private Thread worker;
  23.     private Vector queryResults;
  24.     private String message = "Initializing";
  25.  
  26.  
  27.     public synchronized void start() {
  28.       // Every time "start" is called we create a worker thread to
  29.       // re-evaluate the database query.
  30.       if (worker == null) {
  31.           message = "Connecting to database";
  32.         worker = new Thread(this);
  33.         worker.start();
  34.         }
  35.     }
  36.  
  37.      /**
  38.      * The "run" method is called from the worker thread.  Notice that
  39.      * because this method is doing potentially slow databases accesses
  40.      * we avoid making it a synchronized method.
  41.      */
  42.  
  43.     public void run() {
  44.  
  45.     }
  46.     public void init() {
  47.         dBConnection.OpenConnection();
  48.         try {
  49.             stmt = dBConnection.cnn.createStatement();
  50.             rs = stmt.executeQuery(query);
  51.             while (rs.next()){
  52.                 String tmpName = rs.getString("CompanyName");
  53.                   float tmpPrice = rs.getFloat("ExchangeRate");
  54.                   text += tmpName + "     " + tmpPrice;
  55.                   //System.out.println(text);
  56.               }        
  57.         }
  58.         catch (Exception ex) {
  59.             System.out.println("cannot create statement");
  60.         }
  61.         finally {
  62.             dBConnection.CloseConnection();
  63.         }
  64.     }
  65.  
  66.     public void paint(Graphics g) {
  67.  
  68.         g.drawString(text, 10, 65);
  69.     }
  70. }
  71.  
Any ideas.
Sep 14 '08 #1
Share this Question
Share on Google+
2 Replies


Dököll
Expert 100+
P: 2,364
If this does not do it for you, I don't know what will, let us know if it works:
http://www.javaworld.com/community/node/1168

In a bit!

Dököll
Nov 18 '08 #2

10K+
P: 13,264
If this does not do it for you, I don't know what will, let us know if it works:
http://www.javaworld.com/community/node/1168

In a bit!

Dököll
That link won't help much there.
The problem is that applets are run in browsers and therefore are client side programs with the same restrictions as other client side scripts e.g Javascript.

The restrictions imposed upon applets and how to get past some of them are well explained in Sun's applets tutorial

P.S Fewer and fewer people are using applets these days.
Nov 18 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.