我无法为MySQL加载JDBC驱动程序 [英] I can't load the JDBC driver for MySQL

查看:497
本文介绍了我无法为MySQL加载JDBC驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用以下代码加载JDBC MySQL连接器:

  import java.sql。*; 

公共类dbTest {
public static void main(String [] args)抛出SQLException,ClassNotFoundException
{
Class.forName(com.mysql.jdbc。驱动程序);
}
}

我一直得到一个未找到的课程异常:

  java.lang.ClassNotFoundException 
at edu.rice.cs.plt.reflect.PathClassLoader.findClass(PathClassLoader.java :148)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method) )
at java.lang.Class.forName(Unknown Source)
at dbTest.main(dbTest.java:6)

我已将驱动程序的路径(mysql-connector-java-3.1.14-bin.jar)添加到我的类路径中,并仔细检查以确保它是正确的。我还根据我从本文中读到的内容,将jar的副本添加到我的Java安装的ext文件夹中: http://www.developer.com/java/data/jdbc-and-mysql-installation-and-preparation-of-mysql.html



我还搜索了遇到此问题的其他人的帖子,但到目前为止所有的回复都说要将连接器jar添加到类路径中,我已经完成了。



我们非常感谢任何帮助。

解决方案

< blockquote>

我已将路径添加到驱动程序
(mysql-connector-java-3.1.14-bin.jar)
到我的类路径


该异常告诉您没有正确执行。



您是如何设置CLASSPATH的?如果它是一个环境变量,您将学习IDE和应用服务器忽略它。 不要使用它。



也不要将它放在Java JDK的/ ext目录中。



正确的方法取决于你如何使用它:


  1. 如果您在Eclipse或IntelliJ等IDE中运行,则必须将JAR添加到库中。

  2. 如果您在命令shell中运行,请在运行时对javac.exe使用-p选项,在运行时使用java.exe。

  3. 如果您在Web应用程序中使用它,可以先将它放在WAR文件的WEB-INF / lib目录中。如果您正在使用像Tomcat 6这样的servlet / JSP引擎,请将它放在Tomcat / lib目录中。


I've been trying to load the JDBC MySQL connector with the following code:

import java.sql.*;

public class dbTest{
   public static void main(String[] args) throws SQLException, ClassNotFoundException
   {
    Class.forName("com.mysql.jdbc.Driver"); 
   }
}

And I keep getting a class not found exception:

java.lang.ClassNotFoundException
    at edu.rice.cs.plt.reflect.PathClassLoader.findClass(PathClassLoader.java:148)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at dbTest.main(dbTest.java:6)

I have added the path to the driver (mysql-connector-java-3.1.14-bin.jar) to my classpath and double checked to make sure it was correct. I also added copies of the jar to the ext folder of my Java installation based on what I read from this article: http://www.developer.com/java/data/jdbc-and-mysql-installation-and-preparation-of-mysql.html

I also searched through posts of others who have had this problem, but all of the responses so far have been saying to add the connector jar to the classpath, which I have already done.

Any help would be greatly appreciated.

解决方案

I have added the path to the driver (mysql-connector-java-3.1.14-bin.jar) to my classpath

The exception tells you that you didn't do it correctly.

How are you setting CLASSPATH? If it's an environment variable, you're going to learn that IDEs and app servers ignore it. Don't use it.

Don't put it in the /ext directory of your Java JDK, either.

The right way to do it depends on how you're using it:

  1. If you're running inside an IDE like Eclipse or IntelliJ, you have to add the JAR to a library.
  2. IF you're running in a command shell, use the -p option for javac.exe when you compile and java.exe when you run.
  3. If you're using it in a web app, you can start by putting it in the WEB-INF/lib directory of your WAR file. If you're using a servlet/JSP engine like Tomcat 6, put it in the Tomcat /lib directory.

这篇关于我无法为MySQL加载JDBC驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆