William Kinaan
William Kinaan

Reputation: 28819

connect mysql with java using eclipse

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

Upvotes: 1

Views: 3736

Answers (6)

Arslan Farooq
Arslan Farooq

Reputation: 21

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();

Upvotes: 0

hpityu
hpityu

Reputation: 891

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.

Upvotes: 1

Hemant Metalia
Hemant Metalia

Reputation: 30698

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.

Upvotes: 1

Fathah Rehman P
Fathah Rehman P

Reputation: 8761

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

Upvotes: 0

Pramod Kumar
Pramod Kumar

Reputation: 8014

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

Upvotes: 3

hage
hage

Reputation: 6163

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

Upvotes: 0

Related Questions