简体   繁体   English

class not found exception com.mysql.jdbc.driver甚至我添加了jar文件

[英]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.

相关问题 即使添加了mysql-con.jar文件,“ com.mysql.jdbc.Driver”的ClassNotFound异常 - ClassNotFound Exception for “com.mysql.jdbc.Driver” even after adding mysql-con.jar file 在Runnable jar文件中找不到com.mysql.jdbc.Driver - com.mysql.jdbc.Driver not found in Runnable jar file 找不到com.mysql.jdbc.Driver的类 - class not found exception for com.mysql.jdbc.Driver com.mysql.jdbc.driver类未找到异常 - com.mysql.jdbc.driver class not found exception Eclipse:我已经添加了JAR文件。 java.lang.ClassNotFoundException:com.mysql.jdbc.Driver - Eclipse: I have added the JAR file. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 为什么即使在类路径中添加了mysql.jar,也为何调用Class.forname(“ com.mysql.jdbc.Driver”) - Why we call Class.forname(“com.mysql.jdbc.Driver”) even if mysql.jar is added in class path 找不到类com.mysql.jdbc.Driver - Class not found com.mysql.jdbc.Driver 未找到指定的 JDBC 驱动程序 com.mysql.jdbc.Driver 类 - Specified JDBC Driver com.mysql.jdbc.Driver class not found MySql getConnection.Exception,com.mysql.jdbc.Driver类未找到 - MySql getConnection.Exception, Class com.mysql.jdbc.Driver Not found Class.forName("com.mysql.jdbc.Driver") 没有在该类的 jar 文件中查找 - Class.forName("com.mysql.jdbc.Driver") is not looking in jar file for the class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM