1

i can connect mysql with java using eclipse in a java application with these statements

String unicode = "?useUnicode=yes&characterEncoding=UTF-8";
Class.forName("com.mysql.jdbc.Driver");
            con = (Connection) DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/ams-competation" + unicode,
                    username, password);

and it works good

but my problem is when i tried to connect to mysql with a server application i got this exception

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver what am i doing wrong?thank you all

Edit

i have added mysql-connector already

William Kinaan
  • 28,059
  • 20
  • 85
  • 118

6 Answers6

3

I think you have not included mysql drive jar in your web project.

put mysql-connector-java.. jar file in your web projects lib folder

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
1

for that you need to add the mysql-connector jar in the classpath.

This is because eclipse is not able to find the jar file you specified.

Hence put the jar file in the server`s lib directory

another way is to put jar file it in /WEB-INF/lib eclipse itself will notice it and get it.

Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
1

Did you add the MySQL driver to the server?

It depends on the application server where the jar file is needed to be added:

  • Apache Tomcat: $CATALINA_HOME/common/lib
  • GlassFish server: GLASS_FISH_INSTALL_DIR\lib

Or add it the WEB-INF/lib folder of your web application

Try adding the mysql-connector-java-5.1.20-bin.jar file (downloadable from: http://dev.mysql.com/downloads/connector/j/ ), and restart the server.

hpityu
  • 891
  • 10
  • 21
  • did you add it to the correct location AND restart the server afterwards? – hpityu Jun 22 '12 at 10:08
  • i add it on this way , right click on the project , buid path, configuration build path , add external jar file , then select `mysql-connector-java-5.1.13` i can see the jar file on references libraries tab , but not on WEB-INF – William Kinaan Jun 22 '12 at 10:11
  • If you just reference it to your project it will NOT be uploaded to the server. You need to add it to the correct folder too. – hpityu Jun 22 '12 at 10:12
  • and how can i do that ? and what is the correct folder , thank you – William Kinaan Jun 22 '12 at 10:13
  • It is in my answer... It depends on your server. If you don't want to use it from multiple applications then you can also put it in the `WEB-INF/lib` folder of your web application – hpityu Jun 22 '12 at 10:16
0

Add the Connector/J jar file to the server's classpath (or lib folder)

hage
  • 5,966
  • 3
  • 32
  • 42
0

add my sql connection jar file to your project. Its name will be something like "mysql-connector-java-5.1.13-bin.jar"

Fathah Rehman P
  • 8,401
  • 4
  • 40
  • 42
0

connect connector/J 8.0.11 with eclipse oxygen by following line of code.

 Class.forName("com.mysql.cj.jdbc.Driver");
           conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testing?autoReconnect=true&useSSL=false", "AF", "birru123");

        Statement st = conn.createStatement();