Java applets and SQL

N

Neil Bisby

Hi,

I would like to connect to a Mysql server which is on the same website
as the applet. I am using the mysql driver and loading it as follows

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

It works fine on the Development platform (Forte for java release 2),
but when I deploy it to the website, the class does not seem to ge
loaded.

Class.forName give a message "can't find class anywhere" if it really
cant find the class which in this case is in a jar file, but it does
not give this message.

Any ideas about how I cane sort this?

Thanks

Neil
 
E

Erwin Moller

Neil Bisby wrote:

Hi Neil,
Hi,

I would like to connect to a Mysql server which is on the same website
as the applet.

That is the same machine, not website, I guess...
I am using the mysql driver and loading it as follows

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

It works fine on the Development platform (Forte for java release 2),
but when I deploy it to the website, the class does not seem to ge
loaded.

Class.forName give a message "can't find class anywhere" if it really
cant find the class which in this case is in a jar file, but it does
not give this message.

Any ideas about how I cane sort this?

Yeah, simply: Put the jar-file containing the drivers in a place where your
container can find it.
For example: If your servletcontainer is Tomcat, put the mysql.jar (or
whatever its name is) in the lib-directory under your web-apps.
That will be: somepath/webapps/yourwebapp/WEB-INF/lib

Tomcat should be able to find it there.

Good luck,
Erwin
 
I

Ike

Wait, then you mean to say that an applet, that accesses a MySQL database
residing on the same server as the Applet itself MUST be running Java? -Ike

"Erwin Moller"
 
E

Erwin Moller

Ike said:
Wait, then you mean to say that an applet, that accesses a MySQL database
residing on the same server as the Applet itself MUST be running Java?
-Ike

No Ike,

An applet doesn't access a database directly.
Applets are intentionally crippled so they can do no harm when residing in a
browserwindow.

Sorry to put you in the wrong direction. :)

But let me explain:

If you want your applet to access a database, you should take this route:

database <--> server <--> applet

Because your applet can only communicate with the server that is hosting it,
you should make the server responsible for the database-access.
Where your database actually resides is not important as long as the server
can find it.

If your serverside-language is Java, you are settled because the
communication with the applet can be relatively easy coded.

Here is some code:


location = "someurl";
URL yourURL = new URL( location );

URLConnection servletConnection = yourURL.openConnection();

InputStream inputStreamFromServlet = servletConnection.getInputStream();

ObjectInputStream someFileInputStream = new
ObjectInputStream(inputStreamFromServlet);

Object theResult = themeFileInputStream.readObject();

// here try to cast theResult to the object that it actually is
// eg String strServerAnswer = (String)theResult;
// put it in a try/catch
// Of course the STring is just a simple example



theResult = null;
someFileInputStream.close();
 
N

Neil Bisby

Dear Java Community,

THanks for all your help.

I am realising that my original question was incomplete.

I am trying to run an applet rather than a servlet.

The applet seems to know where the mysql driver is because it is in
mysql.jar file which is referenced in the html using
ARCHIVE="mysql.jar"

This works ok on my test system which is an intranet. I have installed
a mysql server on the same machine as the Microsoft Personal Web
Server. I note that when I run the mysql server, it knows that the
intranet is running and recognises the IP address of that machine.

The Java development software is on another machine.

I tested to make sure the applet was joading the jar file as an
archive by removing the jar file. This gives me an exception from the
Class.forName("com.mysql.jdbc.Driver").newInstance(); code and the con
= DriverManager.getConnection(url, "root", ""); states that Class
<com.mysql.jdbc.Driver> not foound anywhere.

Put the jar file back and it works.

However, when I copy the files to the Personal web server which is on
the same machine as the sql server and access the html file from my
browser, I get an exception from
Class.forName("com.mysql.jdbc.Driver").newInstance(); and a messgae
from DriverManager.getConnection(url, "root", ""); saying "No suitable
driver".

I guess that the driver class is not being loaded rather than it being
a security issue from not having the sql server on the same host.

Also, I assume that the protocol for accessing the mysql server is a
bit more complex than SMTP, otherwise I don't see the point in having
a driver as an intermeduary. However, if anyone can point me to a
mysql protocol reference, I would circumvent the drive and talk to the
mysql server directly using java code.

I have seen some comments about the class path, I assume that this
solution will get the system running on my development platform, but
won't solve the problem that I have which is about deployment. Is this
correct?

Thanks for your comments to date. If you have any additional comments
following this posting I would be very interested to hear from you.



Regards

Neil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top