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

Java-com.MySQL.jdbc.Driver中的MySQL连接错误,java,mysql,jsp,jdbc,Java,Mysql,Jsp,Jdbc,我一直在尝试将java应用程序连接到MySQL数据库,并使用了以下代码行: import java.sql.*; public class AcceptValues extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String

我一直在尝试将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());
             }
        }
}
我还将类名设置为mysql-connector-java-5.1.29-bin.jar,这是我从mysql网站下载的。但是,我仍然无法使用上述代码行连接到数据库,并且将exeptioncom.mysql.jdbc.Driver作为错误抛出


我是java开发新手,非常感谢您的帮助。谢谢。

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

这是你的答案

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

不要在servlet文件中设置连接信息,为连接创建单独的文件

需要的时候随时给全班打电话

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();
  }
}
您还可以使用以下代码在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();
}
}
这是示例代码,但您可以使其更有效这只是示例

请将exeption com.mysql.jdbc.Driver的完整错误/异常信息作为错误提出来。
我遇到了这个问题,在大多数情况下,这个问题背后有两个原因。一个是缺少mysql jar文件,两个都看不到启动mysql服务。

仅使用e打印堆栈跟踪,将e.getMessage()更改为eUse e.printStackTrace(),然后发布错误堆栈跟踪。并解释“我还将类名设置为mysql-connector-java-5.1.29-bin.jar”的意思。这个jar在您的WEB-INF/lib文件夹中,对吗?我没有在WEB-INF/lib文件夹中包含mysql连接器文件。现在我做到了,一切都很顺利。感谢JB Nizet发现错误!WEB-INF/lib中的JAR是您的webapp的类路径。如果你的webapp需要一个库,那么这个库必须存在。或者,更好的是,学习如何使用maven、gradle或其他一些构建管理工具来为你完成所有这些。