如何向Eclipse项目添加mysql JDBC驱动程序?(复制)

时间:2023-01-24 16:53:19

This question already has an answer here:

这个问题已经有了答案:

  1. I have downloaded the mysql-connector-java-5.1.24-bin.jar
  2. 我已经下载了mysql-connector-java-5.1.24 bin.jar
  3. I have created a lib folder in my project and put the jar in there.
  4. 我在项目中创建了一个lib文件夹,并将jar放在其中。
  5. properties of project->build path->add JAR and selected the JAR above.
  6. 项目的属性->构建路径->添加JAR并选择上面的JAR。
  7. I still get java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/mysql
  8. 我仍然得到java.sql。SQLException:没有找到适合jdbc的驱动程序:mysql/ localhost:3306/mysql

I am using mysql 5.5 The code:

我使用的是mysql 5.5代码:

package DBTest;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.*;

/**
 * Servlet implementation class TenUsers
 */
@WebServlet("/TenUsers")
public class TenUsers extends HttpServlet {
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        String mySqlUrl = "jdbc:mysql://localhost:3306/mysql";

        Properties userInfo = new Properties();
        userInfo.put("user", "root");
        userInfo.put("password", "SabababArba");
        try{
            Connection connection = DriverManager.getConnection(mySqlUrl, userInfo);
        }catch(Exception e) {
            out.println(e);
        }      
    }
}

If I add Class.forName("com.mysql.jdbc.Driver"); before Connection connection = DriverManager.getConnection(mySqlUrl, userInfo); I get java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

如果我添加forname(“com.mysql.jdbc.Driver”);连接前=驱动管理器。getConnection(mySqlUrl、用户信息);. lang。ClassNotFoundException:com.mysql.jdbc.Driver

7 个解决方案

#1


10  

Try to insert this:

试着插入:

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

before getting the JDBC Connection.

在获取JDBC连接之前。

#2


5  

1: I have downloaded the mysql-connector-java-5.1.24-bin.jar

1:我已经下载了mysql-connector-java-5.1.24-bin.jar。

Okay.

好吧。


2: I have created a lib folder in my project and put the jar in there.

2:我在我的项目中创建了一个lib文件夹,并将jar放在其中。

Wrong. You need to drop JAR in /WEB-INF/lib folder. You don't need to create any additional folders.

错了。您需要将JAR放入/WEB-INF/lib文件夹。您不需要创建任何额外的文件夹。


3: properties of project->build path->add JAR and selected the JAR above.

3:项目的属性——>构建路径——>添加JAR,选择上面的JAR。

Unnecessary. Undo it all to avoid possible conflicts.

不必要的。取消一切,以避免可能的冲突。


4: I still get java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/mysql

4:我还是用java.sql。SQLException:没有找到适合jdbc的驱动程序:mysql/ localhost:3306/mysql

This exception can have 2 causes:

这种例外有两个原因:

  1. JDBC driver is not in runtime classpath. This is to be solved by doing 2) the right way.
  2. JDBC驱动程序不在运行时类路径中。这要用正确的方法来解决。
  3. JDBC URL is not recognized by any of the loaded JDBC drivers. Indeed, the JDBC URL is wrong, there should as per the MySQL JDBC driver documentation be another colon between the scheme and the host.

    JDBC URL不被任何已加载的JDBC驱动程序识别。实际上,JDBC URL是错误的,因为MySQL JDBC驱动程序文档应该是scheme和主机之间的另一个冒号。

    jdbc:mysql://localhost:3306/mysql
    

#3


3  

You can paste the .jar file of the driver in the Java setup instead of adding it to each project that you create. Paste it in C:\Program Files\Java\jre7\lib\ext or wherever you have installed java.

您可以在Java安装中粘贴驱动程序的.jar文件,而不是将它添加到创建的每个项目中。粘贴在C:\Program Files\Java\jre7\lib\ext或无论你已经安装了java。

After this you will find that the .jar driver is enlisted in the library folder of your created project(JRE system library) in the IDE. No need to add it repetitively.

在此之后,您将发现在IDE中创建的项目(JRE系统库)的library文件夹中有.jar驱动程序。不需要重复添加。

#4


2  

  1. copy mysql-connector-java-5.1.24-bin.jar

    复制mysql-connector-java-5.1.24-bin.jar

  2. Paste it into \Apache Software Foundation\Tomcat 6.0\lib\<--here-->

    粘贴到\Apache Software Foundation\Tomcat 6.0\lib <- here——>

  3. Restart Your Server from Eclipes.

    从eclipse重新启动服务器。

  4. Done

    完成

#5


1  

you haven't loaded driver into memory. use this following in init()

你没有把驱动载入内存。在init()中使用以下命令

Class.forName("com.mysql.jdbc.Driver");

Also, you missed a colon (:) in url, use this

另外,您在url中漏掉了一个冒号(:),请使用这个。

String mySqlUrl = "jdbc:mysql://localhost:3306/mysql";

#6


1  

if you are getting this exception again and again then download my-sql connector and paste in tomcat/WEB-INF/lib folder...note that some times WEB-INF folder does not contains lib folder, at that time manually create lib folder and paste mysql connector in that folder..definitely this will work.if still you got problem then check that your jdk must match your system. i.e if your system is 64 bit then jdk must be 64 bit

如果您一次又一次地遇到这个异常,那么请下载my-sql连接器并将其粘贴到tomcat/WEB-INF/lib文件夹中……注意,有时WEB-INF文件夹不包含lib文件夹,这时手动创建lib文件夹并将mysql连接器粘贴到该文件夹中。如果仍然有问题,请检查jdk必须与系统匹配。我。如果您的系统是64位的,那么jdk必须是64位的

#7


1  

Try this tutorial it has the explanation and it will be helpful http://www.ccs.neu.edu/home/kathleen/classes/cs3200/JDBCtutorial.pdf

尝试这个教程,它有一个解释,它将有助于http://www.ccs.neu.edu/home/en/classes/cs3200/jdbctutories.pdf。

#1


10  

Try to insert this:

试着插入:

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

before getting the JDBC Connection.

在获取JDBC连接之前。

#2


5  

1: I have downloaded the mysql-connector-java-5.1.24-bin.jar

1:我已经下载了mysql-connector-java-5.1.24-bin.jar。

Okay.

好吧。


2: I have created a lib folder in my project and put the jar in there.

2:我在我的项目中创建了一个lib文件夹,并将jar放在其中。

Wrong. You need to drop JAR in /WEB-INF/lib folder. You don't need to create any additional folders.

错了。您需要将JAR放入/WEB-INF/lib文件夹。您不需要创建任何额外的文件夹。


3: properties of project->build path->add JAR and selected the JAR above.

3:项目的属性——>构建路径——>添加JAR,选择上面的JAR。

Unnecessary. Undo it all to avoid possible conflicts.

不必要的。取消一切,以避免可能的冲突。


4: I still get java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/mysql

4:我还是用java.sql。SQLException:没有找到适合jdbc的驱动程序:mysql/ localhost:3306/mysql

This exception can have 2 causes:

这种例外有两个原因:

  1. JDBC driver is not in runtime classpath. This is to be solved by doing 2) the right way.
  2. JDBC驱动程序不在运行时类路径中。这要用正确的方法来解决。
  3. JDBC URL is not recognized by any of the loaded JDBC drivers. Indeed, the JDBC URL is wrong, there should as per the MySQL JDBC driver documentation be another colon between the scheme and the host.

    JDBC URL不被任何已加载的JDBC驱动程序识别。实际上,JDBC URL是错误的,因为MySQL JDBC驱动程序文档应该是scheme和主机之间的另一个冒号。

    jdbc:mysql://localhost:3306/mysql
    

#3


3  

You can paste the .jar file of the driver in the Java setup instead of adding it to each project that you create. Paste it in C:\Program Files\Java\jre7\lib\ext or wherever you have installed java.

您可以在Java安装中粘贴驱动程序的.jar文件,而不是将它添加到创建的每个项目中。粘贴在C:\Program Files\Java\jre7\lib\ext或无论你已经安装了java。

After this you will find that the .jar driver is enlisted in the library folder of your created project(JRE system library) in the IDE. No need to add it repetitively.

在此之后,您将发现在IDE中创建的项目(JRE系统库)的library文件夹中有.jar驱动程序。不需要重复添加。

#4


2  

  1. copy mysql-connector-java-5.1.24-bin.jar

    复制mysql-connector-java-5.1.24-bin.jar

  2. Paste it into \Apache Software Foundation\Tomcat 6.0\lib\<--here-->

    粘贴到\Apache Software Foundation\Tomcat 6.0\lib <- here——>

  3. Restart Your Server from Eclipes.

    从eclipse重新启动服务器。

  4. Done

    完成

#5


1  

you haven't loaded driver into memory. use this following in init()

你没有把驱动载入内存。在init()中使用以下命令

Class.forName("com.mysql.jdbc.Driver");

Also, you missed a colon (:) in url, use this

另外,您在url中漏掉了一个冒号(:),请使用这个。

String mySqlUrl = "jdbc:mysql://localhost:3306/mysql";

#6


1  

if you are getting this exception again and again then download my-sql connector and paste in tomcat/WEB-INF/lib folder...note that some times WEB-INF folder does not contains lib folder, at that time manually create lib folder and paste mysql connector in that folder..definitely this will work.if still you got problem then check that your jdk must match your system. i.e if your system is 64 bit then jdk must be 64 bit

如果您一次又一次地遇到这个异常,那么请下载my-sql连接器并将其粘贴到tomcat/WEB-INF/lib文件夹中……注意,有时WEB-INF文件夹不包含lib文件夹,这时手动创建lib文件夹并将mysql连接器粘贴到该文件夹中。如果仍然有问题,请检查jdk必须与系统匹配。我。如果您的系统是64位的,那么jdk必须是64位的

#7


1  

Try this tutorial it has the explanation and it will be helpful http://www.ccs.neu.edu/home/kathleen/classes/cs3200/JDBCtutorial.pdf

尝试这个教程,它有一个解释,它将有助于http://www.ccs.neu.edu/home/en/classes/cs3200/jdbctutories.pdf。