This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Applets and the fly likes JDBC in applets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "JDBC in applets" Watch "JDBC in applets" New topic
Author

JDBC in applets

Gotham Sewani
Greenhorn

Joined: Feb 16, 2007
Posts: 15
I want to make an applet which directly accesses a MySql database. The database is on the same server from which the applet has been downloaded. Now the problem I am facing is with regard to drivers.
I use the Class.forName() method to load the drivers.
Now what changes do I need to make to the
1. Applet code
2. The Applet tag
3. The client machine
4. The server
to make this method work?
I tried changing the codebase attribute and unjarring the jar file containing the driver class file,but still it was throwing a ClassNotFound error. Please help me.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
The only changes you need to make concern the applet tag. It needs to have an archive attribute that references the jar file of the driver (which needs to be in the same directory as the applet itself). Don't unjar that file into its constituent classes.

<applet name="..." code="..." archive="myApplet.jar,mySqlDriver.jar" ...


Ping & DNS - my free Android networking tools app
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30356
    
150

Moving to our applets forum since it has more to do with applets/classpaths than JDBC.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Gotham Sewani
Greenhorn

Joined: Feb 16, 2007
Posts: 15
One more question related to applets and JDBC.
Would it be advisable to use applets for directly communicating with the database server?
What security risks would it pose?
One,which I can think of is that I am passing the root password of the database server as an argument to the getConnection() method in my code.Now since applets are run on the clients machine,it might be possible that the client gets hold of the class files and decompiles them to get the password.
Is there any remedy to this?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
You hit the nail on the head - that is precisely the one huge security problem with using JDBC in applets. It may be acceptable in an intranet setting, but I'd recommend not to use JDBC over the public Internet. (An exception may be for clients that perform read-only operations exclusively, and you're using an account that prohibits all change operations. It's debatable, though.)

No decompiling is even necessary - unless SSL is used, all SQL data is transmitted in clear text, so a simple TCP/IP sniffer can record the traffic.

And, of course, never use the DB root password - always create an account that is used only for that particular client, which has only the rights necessary for that client type.
Raj
Greenhorn

Joined: Apr 08, 2007
Posts: 20
dude are you using pure awt bcs i cant connect my databse to an awt applet
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
Using AWT or Swing has nothing to do with whether JDBC works or not. Let's continue the discussion here.
 
Consider Paul's rocket mass heater.
 
subject: JDBC in applets