[英]class not found exception com.mysql.jdbc.driver even I added the jar file
Sory for my weak english, I want to connect mysql connection with java, I added the mysql-connector.jar but class not found error still continue. Sory对于我的弱英语,我想用java连接mysql连接,我添加了mysql-connector.jar但是找不到类错误仍然继续。 Here is the screenshot of build path. 这是构建路径的屏幕截图。
What should I do for the connect mysql 我应该怎么做连接mysql
Here is the code 这是代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
String sql1 = "INSERT INTO test (name) VALUES(?)";
stmt = conn.prepareStatement(sql1);
stmt.setString(1, okey);
}
// execute insert SQL stetement
stmt .executeUpdate();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
}//end finally try
}//end try
I don't know what IDE you are using there, but... are you sure you shouldn't have added the connector jar under 'Libraries' instead of 'Order and Export'? 我不知道你在那里使用什么IDE,但是......你确定你不应该在'Libraries'而不是'Order and Export'下添加连接器jar吗? Also, is the connector jar available to your deployed application (server container, EJB server, whatever) and not just available to you IDE? 此外,连接器jar是否可用于已部署的应用程序(服务器容器,EJB服务器等),而不仅仅适用于IDE?
Strictly speaking you probably don't need the connector jar to be known to your IDE: it will know about the JDBC interfaces and classes from java.sql.*
(assuming you have configured it to know about the JDK it is to use) and that is all it should need to be able to compile your JDBC code. 严格来说,您可能不需要 IDE知道连接器jar:它将从java.sql.*
了解JDBC接口和类(假设您已将其配置为了解它将使用的JDK)和这就是它需要能够编译 JDBC代码的全部内容。 However, at runtime your application will need the classes from the connector jar available. 但是,在运行时,您的应用程序将需要可用连接器jar中的类。 How you achieve this depends in what kind of application you are building (standalone, webapp etc) and where you are deploying it (webserver, EJB container, etc). 如何实现这一点取决于您正在构建的应用程序类型(独立,Web应用程序等)以及部署它的位置(Web服务器,EJB容器等)。
将连接器jar文件放在“WEB-INF / lib”文件夹中将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.