问题
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
回答1:
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
回答2:
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.
回答3:
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.
回答4:
Add the Connector/J jar file to the server's classpath (or lib folder)
回答5:
add my sql connection jar file to your project. Its name will be something like "mysql-connector-java-5.1.13-bin.jar"
回答6:
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();
来源:https://stackoverflow.com/questions/11154065/connect-mysql-with-java-using-eclipse