Problem with JDBC connection to mySQL-DB on Tomcat (xampp)

Hi guys, I need your help. I have to write a Java Applet with JDBC connection for a website as a project for college. (Even if using Applets sticks in my craw because it's not exactly state of the art) I coded the Applet and the connection to the database is working in Eclipse if I import the MySQL connector as an external JAR. The problem comes if I try to execute this applet on the website (after signing it and all that stuff)

I placed the connector as jar in

  1. the htdocs folder where the website is located
  2. it should be exported in the jar I use for my applet
  3. in the xampp/tomcat/lib

I don't know what aspect I have missed. I would be really glad if someone could help me out

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Test extends Applet {
  public void init(){
    Statement stmt = null;
    ResultSet rs = null;
    try {
       Class.forName("com.mysql.jdbc.Driver").newInstance();
       Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=jupiter");
       stmt = conn.createStatement();
       rs = stmt.executeQuery("SELECT * FROM quiz");
     } catch (Exception e) {
       ...
     }
 }

Posted May 21, 2014, 7:09 PM UTC

110
romehustlin•

You should be able to use that method with 3 parameters that may look something like this:

DriverManager.getConnection(url,user,password)

In your case, DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "jupiter");

2
FPJaquesOP•

Thanks but this does not change anything. I still may access the database in Eclipse but the applet still can't connect if executed via Browser (but at least it does look a lot better that way, more organized)

1
romehustlin•

Oops, sorry about that. Just went straight into the code and noticed that.

Here are some notes I had from doing something similar in NetBeans ( a couple things may be different from what you are working on, but the concept should be the same):

Place the two jar files in your Tomcat directory. Your default directory on Windows should be C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.34\lib If the Tomcat server is running, restart it by: Click on "Services" tab above the project explorer in NetBeans Expand "Servers" Right click "Apache Tomcat <version>", click restart. If the Tomcat server was not running, the jar files should be pulled in on the next deployment.

2
FPJaquesOP•

No problem and again thanks for your answer
But I only have 1 jar file (mysql-connector-java-5.1.30-bin.jar - do I need two? This would explain a lot) and because of the fact that I'm not using Netbeans, I don't have these folders (but I suppose this lib folder is the same as my E:\xampp\tomcat\lib\ where I already placed my jar file.
I did several restarts, none of those had any effect on the availability to connect.

1
romehustlin•

No, mine was for a DB2 database so there were two. It sounds like you did everything right. I use NetBeans, and one other thing I've had to do is go into my servers tab, find sources and add the jar into it like that. Eclipse may have something similar.

2
rjcarr•

If you open the java console what is the problem? As long as you're using localhosthost it should work find assuming you have the libraries (i.e., mysql driver) in the right place.

2
FPJaquesOP•

Yeah that's what I figured but I don't seem to have them placed right. I don't know what to do in a different way, though.

Java console says:

Missing Application-Name manifest attribute for: http://localhost/Database.jar
Missing Permissions manifest attribute in main jar: http://localhost/Database.jar
com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Test.init(Test.java:19)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.init(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

I don't think the missing Application Name oder missing Permissions manifest attributes break something, do they?

1
rjcarr•

Probably not, but the missing driver is definitely your problem. Read about how to bundle your driver with your applet. Do a search on "jdbc applet" and that should help. Good luck!

2
FPJaquesOP•

Well I did that and none of the described steps had any effects. Gonna try it again more thoroughly later.

1