Java 中的 MySQL 连接错误 - com.mysql.jdbc.Driver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21980881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:48:55  来源:igfitidea点击:

MySQL Connection Error in Java - com.mysql.jdbc.Driver

javamysqljspjdbc

提问by Amit Nandan Periyapatna

I have been trying to connect my java application to a MySQL database and have used the following lines of code:

我一直在尝试将我的 java 应用程序连接到 MySQL 数据库并使用以下代码行:

import java.sql.*; 
public class AcceptValues extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

             String url = "jdbc:mysql://localhost:3306/db_name";
             String driver = "com.mysql.jdbc.Driver";
             String userName = "root";
             String password = "";
             try {
             Class.forName(driver);
             Connection conn = DriverManager.getConnection(url,userName,password);
             out.print("Connection estd");
             Statement st = conn.createStatement();
             st.executeQuery("insert into table_name values(2, 'testing');");
             }
             conn.close();
             } catch (Exception e) {
                 out.print("Error : " +e.getMessage());
             }
        }
}

I have also set the classname to mysql-connector-java-5.1.29-bin.jar which I downloaded from the mysql website. But I still am not able to connect to the database using the above lines of code and is throwing the exeption com.mysql.jdbc.Driveras an error.

我还将类名设置为我从 mysql 网站下载的 mysql-connector-java-5.1.29-bin.jar。但是我仍然无法使用上述代码行连接到数据库,并且将com.mysql.jdbc.Driver作为错误抛出。

I'm new to java development and any help would be deeply appreciated. Thank you.

我是 Java 开发的新手,任何帮助将不胜感激。谢谢你。

采纳答案by Standin.Wolf

Add the mysql-connector-java-5.1.29-bin.jar in the WEB-INF/libfolder. Its not enough to just add the jar in your built path. Even I had the same issue at the start.

WEB-INF/lib文件夹中添加mysql-connector-java-5.1.29-bin.jar 。仅在构建的路径中添加 jar 是不够的。即使我一开始也有同样的问题。

回答by Bhupendra Piprava

Here is you answer,

这是你的答案,

right click on project properties --> Java Build Path --> Libraries --> add Jar to your project(which you already downloaded)

右键单击项目属性 --> Java 构建路径 --> 库 --> 将 Jar 添加到您的项目(您已经下载)

don't right you connection information in servlet file create separate file for connection

不正确您在servlet文件中的连接信息为连接创建单独的文件

call the class whenever you need

需要时给班级打电话

import java.sql.*;
public class DBConn {

private String url = "jdbc:mysql://localhost:3306/test";
private String driver = "com.mysql.jdbc.Driver";
private String userName = "root";
private String password = "root";
private Connection con = null;

private void getConnection() {

     try {
         Class.forName(driver);
         if(con == null){
             con = DriverManager.getConnection(url,userName,password);
         }
         System.out.print("Connection estd");
     }catch (Exception e) {
         System.out.print("Error : " +e.getMessage());
    }
}


/**for desktop application */
public static void main(String[] arg){

    DBConn con = new DBConn();
    con.getConnection();
  }
}

You can also getconnection in your sevelet using following code

您还可以使用以下代码在您的 sevelet 中获取连接

import java.sql.*; 
public class AcceptValues extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

   DBConn con = new DBConn();
   con.getConnection();
}
}

this sample code but you can make it more efficient this is just sample,

此示例代码,但您可以使其更高效,这只是示例,

please mention full error/exception information

请提供完整的错误/异常信息

回答by pretty_monk

the exeption com.mysql.jdbc.Driver as an error.

将 com.mysql.jdbc.Driver 作为错误排除。

i met this problem, in most case, there are two reasons behind this problem. one is lack of the mysql jar file, the two is losing sight of starting the the mysql service.

我遇到了这个问题,在大多数情况下,这个问题背后有两个原因。一是缺少mysql jar文件,二是没有启动mysql服务。