[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mysql-java
Subject:    Re: Students needing help: applet + db : unable to connect to any
From:       Paul Richards <Paul.Richards () Sun ! COM>
Date:       2004-04-19 18:36:36
Message-ID: 1082399795.3330.48.camel () drusilla
[Download RAW message or body]

Applets run client side on the browser.  So when you try to connect to
localhost (127.0.0.1) you are in effect trying to connect to the machine
the browser is on, probably not what you want.



On Mon, 2004-04-19 at 04:37, koen.swolfs@ace.groept.be wrote:
> Hello,
> 
> For school we're working on a Java applet which retrieves information 
> from a MySQL database.  The database is on the same server as where the 
> applet (code below) is.  When I run my applet from Eclipse (on my 
> computer, not the server) everything works fine.  But when I upload the 
> applet to the server and want to run it in my browser (Mozilla Firefox) 
> I get following error: "SQLException: java.sql.SQLException: Unable to 
> connect to any hosts due to exception: 
> java.security.AccessControlException: access denied 
> (java.net.SocketPermission 127.0.0.1:3306 connect, resolve)".  I already 
> searched the mailing list for some answers, a lot of people are talking 
> about jar signing, which I already did: no difference...
> 
> Can someone please help us out...
> 
> Thanks a lot!!!
> 
> This is the code we're testing (found somewhere on the net and adapted 
> with our server settings):
> -----
> import java.applet.Applet;
> import java.awt.Graphics;
> import java.sql.Connection;
> import java.sql.DriverManager;
> import java.sql.ResultSet;
> import java.sql.SQLException;
> import java.sql.Statement;
> import java.util.Vector;
> 
> public class MySqlQueryApplet extends Applet implements Runnable {
>     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() {
>        
>         String url2 = "jdbc:mysql://localhost/ourdatabasename";
>         String usr2 = "ourusername";
>         String pwd2 = "ourpassword";
> 
>         String query = "SELECT gebr_id, gebr_naam FROM tblGebruiker";
> 
>         try {
>             Class.forName("com.mysql.jdbc.Driver");
>         } catch (Exception ex) {
>             setError("Can't find Database driver class: " + ex);
>             return;
>         }
> 
>         try {
>             Vector results = new Vector();
>             Connection con =
>                 (Connection) DriverManager.getConnection(url2, usr2, pwd2);
>             Statement stmt = (Statement) con.createStatement();
>             ResultSet rs = (ResultSet) stmt.executeQuery(query);
>             while (rs.next()) {
>                 String s = rs.getString("gebr_naam");
>                 int i = rs.getInt("gebr_id");
>                 String text = i + "     " + s;
>                 results.addElement(text);
>             }
> 
>             stmt.close();
>             con.close();
> 
>             setResults(results);
> 
>         } catch (SQLException ex) {
>             setError("SQLException: " + ex);
>         }
>     }
> 
>     /**
>      * The "paint" method is called by AWT when it wants us to
>      * display our current state on the screen.
>      */
> 
>     public synchronized void paint(Graphics g) {
>         // If there are no results available, display the current message.
>         if (queryResults == null) {
>             g.drawString(message, 5, 50);
>             return;
>         }
> 
>         // Display the results.
>         g.drawString("PaperWizard gebruikers:  ", 5, 10);
>         int y = 30;
>         java.util.Enumeration enum = queryResults.elements();
>         while (enum.hasMoreElements()) {
>             String text = (String) enum.nextElement();
>             g.drawString(text, 5, y);
>             y = y + 15;
>         }
>     }
> 
>     /**
>      * This private method is used to record an error message for
>      * later display.
>      */
> 
>     private synchronized void setError(String mess) {
>         queryResults = null;
>         message = mess;
>         worker = null;
>         // And ask AWT to repaint this applet.
>         repaint();
>     }
> 
>     /**
>      * This private method is used to record the results of a query, for
>      * later display.
>      */
> 
>     private synchronized void setResults(Vector results) {
>         queryResults = results;
>         worker = null;
>         // And ask AWT to repaint this applet.
>         repaint();
>     }
> }
> -----
-- 
Paul Richards <Paul.Richards@Sun.COM>


-- 
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe:    http://lists.mysql.com/java?unsub=mysql-java@progressive-comp.com

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic