aspose file tools*
The moose likes Applets and the fly likes How to put JDBC driver in applet jar? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "How to put JDBC driver in applet jar?" Watch "How to put JDBC driver in applet jar?" New topic
Author

How to put JDBC driver in applet jar?

Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
Hello.

I have a problem with my applet and mysql jdbc driver. So here's the thing.

I have website with my applet. Applet connects to the mysql DB. When I use it on my computer - everything works fine, but when I put my applet jar file to the server - applet can't connect to the mysql server (it's on the same machine as website). I know that this is jdbc driver problem. I don't know how to put it to the jar file and make that applet use this jdbc driver from himself (from the jar file with applet). Is it possible? I don't have full access to the server, and I can't put the jdbc driver in the classpath of this server.

Please, tell me is it possible to put the jdbc driver into *.jar file with my applet?

Best regards.

MK
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Of course it's possible to put the JDBC driver into the applet jar. You could just do that when you create the applet jar.

But why did you ask that question? Your real problem is that you can't connect to the database, isn't it? So you should be looking for solutions to that problem. And the solution to that problem does not involve putting the JDBC driver into the applet jar. It involves using the "archive" attribute of the <applet> tag in your HTML to point to the jar containing the JDBC driver.

Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
Thank you for your reply.

I think that applet don't have the MySQL driver, because when I will delete the driver from my classpath on my local computer I have the same result as now on server. Anyway:





Here is the html code and driver implementation code. It still doesn't work. Do I have to add mysql driver to the Libraries? In "build path" ? Or if I got this html code i should delete it from the libraries?



Best regards.

MK
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You are asking about an applet, are you not? Then all of that stuff you mentioned which is on your computer is irrelevant.

All you need is for your server to be able to serve the MySQL jar. The way you have set up your <applet> tag, it should be in the same server directory as the applet class.
Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
So i putted StartChecker.class (name of applet class) and mysql jar file in the same directory on the server, i used <applet> tag like in my previous post, mysql server is up, and it doesn't work. This is what i want to say and that is my problem :/
When I enter to the site with my applet, I can see it, I can click button. When i click the button it should connect with mysql server and list everything from one table, The effect is that I don't get any data from mysql.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So take the URL for your applet page and replace the part which says "something.html" by "mysql-connector-java-5.1.18-bin.jar". Paste that into your browser's address bar and see what it retrieves.
Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
it asking me to save the mysql driver jar - so this is ok I think
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, that's right. So now is the time to go back and find out why it isn't working right. Error messages and stack traces would be the best thing.
Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
At the first use of my applet I have:
Exception in thread "AWT-EventQueue-2" java.lang.ExceptionInInitializerError
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:286)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at StartChecker$1.actionPerformed(StartChecker.java:79)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.security.AccessControlException: access denied (java.util.PropertyPermission file.encoding read)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPropertyAccess(Unknown Source)
at java.lang.System.getProperty(Unknown Source)
at com.mysql.jdbc.StringUtils.<clinit>(StringUtils.java:70)
... 39 more


At the second and next use of the applet I have this error:

Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError: Could not initialize class com.mysql.jdbc.StringUtils
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:286)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at StartChecker$1.actionPerformed(StartChecker.java:79)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Aha. So this isn't just an ordinary "I can't set my classpath" question. You are getting an applet security exception:
Caused by: java.security.AccessControlException: access denied (java.util.PropertyPermission file.encoding read)

So you're going to have to sign your applet if you want to use MySQL in it.

But before you go ahead and do that, let me warn you: accessing a database directly from your applet is not a good practice from a data security point of view. To make that work you have to configure your database server so that it can be accessed directly from the web. And once you have done that, your database server can be accessed by any evildoer on the web.

So what can they do? Well, they know you're using MySQL because they can see what jar you downloaded. And they know the user ID and password which your applet uses to connect, because they used a packet-sniffer to observe that. So now they have open access to all kinds of things and can cause all kinds of problems.

The preferred way to access a database from an applet is to run a web application on your server which exposes only the specific services you want to use from your database. Your applet would then send requests to the web application for those services, rather than sending SQL requests direct to the database. The evildoers can certainly duplicate that, but they can only use those specific services.
Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
Thank you for your reply.

The login and password which I use to log in to the mysql server has only right to select to the one table, so even if they will get it, then they won't do anything terrible I think, but anyway, I won't set up the mysql server to be accessed directly from the web.

Hmm web application, you mean for example servlet or JSP/JSF website? It's more complicated then I thought, isn't it ? So the schemma of this solution is something like that:

MYSQL Server -> servlet gets data for sql -> applet gets data form servlet

right?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Marek Krokosinski wrote:Hmm web application, you mean for example servlet or JSP/JSF website? It's more complicated then I thought, isn't it ? So the schemma of this solution is something like that:

MYSQL Server -> servlet gets data for sql -> applet gets data form servlet

right?


Yes, that's the idea. If it's just "applet sends request, servlet returns response containing data" then there's no HTML involved and so probably JSP etc. would be unnecessary. That just leaves you to design the request/response protocol you're going to use.
Marek Krokosinski
Ranch Hand

Joined: Jun 10, 2011
Posts: 64
That just leaves you to design the request/response protocol you're going to use.


You mean in servlet?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to put JDBC driver in applet jar?