applet and jdbc

hello there!

have anyone some experience with applet/jdbc developpement?

I have a simple application with jdbc that only connects with database to check if it is possible. That plain java application works fine but when I copy/paste the part that is responsible for establishing connection with database to my applet application it produces an error! I 'm using mysql 5 and jdbc driver from mysql site. I'm really pressed with this project ahave no idea why it is not working .thus giving you some duke points for helping me out. thanks!! :)

my applet code:

"

import java.awt.*;

import java.applet.*;

import java.sql.*;

public class TestJDBC extends Applet {

Connection conn = null;

public void init() {

try

{

String userName = "root";

String password = "pawelo";

String url = "jdbc:mysql://localhost:3306/DBTest";

Class.forName ("com.mysql.jdbc.Driver").newInstance ();

conn = DriverManager.getConnection (url, userName, password);

System.out.println ("Database connection established");

}

catch (Exception e)

{

System.err.println ("Cannot connect to database server"+e.getMessage());

}

finally

{

if (conn != null){

try{

conn.close ();

System.out.println ("Database connection terminated");

}catch (Exception e) {

}

}

}

}

public void paint(Graphics g) {

g.drawString("Welcome to Java!!", 50, 60 );

}

}

"

and the error produced :

java.lang.ExceptionInInitializerError

at com.mysql.jdbc.Connection.<init>(Connection.java:1175)

at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)

at java.sql.DriverManager.getConnection(Unknown Source)

at java.sql.DriverManager.getConnection(Unknown Source)

at TestJDBC.init(TestJDBC.java:17)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Caused by: java.lang.RuntimeException: Unable to initialize character set mapping tables

at com.mysql.jdbc.CharsetMapping.<clinit>(CharsetMapping.java:73)

... 7 more

Exception in thread "thread applet-TestJDBC.class" java.lang.NullPointerException

at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)

at sun.plugin.AppletViewer.showAppletException(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

[2568 byte] By [gAngela] at [2007-10-2 18:28:14]
# 1
i also had the same problem....dbase connection can run on the normal application ..however in applet...it keep prompting the same error....for who got solution for this..pls email me at susanto_wang@yahoo.com.thanks alot...
susantoa at 2007-7-13 19:49:33 > top of Java-index,Desktop,Core GUI APIs...
# 2

Sollution 1, don't connect to a database from an applet. Applets run on a

client machinge and can cause the following problems:

1-1. be decompiled

1-2. need to find the jdbc classes (com.mysql in your case) on every client

1-3. need to be signed or a special policy needs to be set up.

You've run into the 1-3rd problem because you're running the applet

locally I guess, after deploying considor problem 1-1 and 1-2.

To solve your problem quickly sign the applet or set up a policy for it:

Signing applets:

http://forum.java.sun.com/thread.jsp?forum=63&thread=524815

second post and reply 18 for the java class file using doprivileged

http://forum.java.sun.com/thread.jsp?forum=63&thread=409341

4th post explaining how to set up your own policy with your own keystore

Or better yet use a servlet to do database interaction and let the applet

communicate with this servlet using http POST request (yep, lot of work).

harmmeijera at 2007-7-13 19:49:33 > top of Java-index,Desktop,Core GUI APIs...
# 3
indeed I use Struts actions to perform request to the database. Sorry to repond so late but I'm quite busy . This applet is a part of my struts application so it's the best solution..let the java be with u!
gAngela at 2007-7-13 19:49:33 > top of Java-index,Desktop,Core GUI APIs...
# 4

it appears your database in on the same machine your using the browser on. I think browsers dont allow anything on your machine to be messed with(your db access problem), unless you do the key/signature whatever thing. Basically once you put the database on a web server(instead of localhost), it should work.

I am only guessing here as I have yet to put my epic fantasy football applet on the web yet.

jonr25a at 2007-7-13 19:49:33 > top of Java-index,Desktop,Core GUI APIs...