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
- the htdocs folder where the website is located
- it should be exported in the jar I use for my applet
- 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
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");
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)
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.
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.
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.