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
- import java.sql.*;
- public class DBConnection {
- String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
- String url = "jdbc:odbc:BrokerSystemDSN";
- String query = "SELECT CompanyName, ExchangeRate FROM StochHistoryTable";
- String text;
- public Connection cnn;
- public DBConnection(){
- try{
- Class.forName(driver).newInstance();
- System.out.println("Driver Loaded");
- }catch(Exception ex){
- System.out.println("Failed to load driver");
- }
- }
- public void OpenConnection() {
- try{
- cnn = DriverManager.getConnection(url);
- System.out.println("Connection Established");
- }catch(SQLException e){
- System.out.println("Failed to establish Connection");
- }
- }
- public void CloseConnection() {
- try{
- cnn.close();
- }catch(SQLException eClose){
- System.out.println("cannot close database");
- }
- }
- public Connection getConnection(){
- return cnn;
- }
- public void TestStatment()
- {
- try {
- Statement stmt = cnn.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- while (rs.next()){
- String tmpName = rs.getString("CompanyName");
- float tmpPrice = rs.getFloat("ExchangeRate");
- text += tmpName + " " + tmpPrice;
- System.out.println(text);
- }
- stmt.close();
- cnn.close();
- }
- catch (Exception ex) {
- System.out.println("cannot create statement");
- }
- }
- // Tests
- public static void main (String args[]){
- DBConnection dBConnection = new DBConnection();
- dBConnection.OpenConnection();
- dBConnection.TestStatment();
- }
- }
Expand|Select|Wrap|Line Numbers
- /**
- * @(#)OutputApplet.java
- *
- * OutputApplet Applet application
- *
- * @author
- * @version 1.00 2008/9/13
- */
- import java.awt.*;
- import java.applet.*;
- import java.awt.Graphics;
- import java.util.Vector;
- import java.sql.*;
- public class OutputApplet extends Applet implements Runnable{
- DBConnection dBConnection = new DBConnection();
- private String text;
- private Statement stmt;
- private ResultSet rs;
- private String query = "SELECT CompanyName, ExchangeRate FROM StochHistoryTable";
- private Thread worker;
- private Vector queryResults;
- private String message = "Initializing";
- public synchronized void start() {
- // Every time "start" is called we create a worker thread to
- // re-evaluate the database query.
- if (worker == null) {
- message = "Connecting to database";
- worker = new Thread(this);
- worker.start();
- }
- }
- /**
- * The "run" method is called from the worker thread. Notice that
- * because this method is doing potentially slow databases accesses
- * we avoid making it a synchronized method.
- */
- public void run() {
- }
- public void init() {
- dBConnection.OpenConnection();
- try {
- stmt = dBConnection.cnn.createStatement();
- rs = stmt.executeQuery(query);
- while (rs.next()){
- String tmpName = rs.getString("CompanyName");
- float tmpPrice = rs.getFloat("ExchangeRate");
- text += tmpName + " " + tmpPrice;
- //System.out.println(text);
- }
- }
- catch (Exception ex) {
- System.out.println("cannot create statement");
- }
- finally {
- dBConnection.CloseConnection();
- }
- }
- public void paint(Graphics g) {
- g.drawString(text, 10, 65);
- }
- }