Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
servlet和mysql连接错误ClassNotFoundException_Mysql_Servlets - Fatal编程技术网

servlet和mysql连接错误ClassNotFoundException

servlet和mysql连接错误ClassNotFoundException,mysql,servlets,Mysql,Servlets,-->大家好,我正在尝试将servlet与mysql连接起来,但我不知道为什么会出现ClassNotFoundException?它发生在class.forName行中。数据库名为trial1,表名为data2,列ID、名称、薪水。请帮忙 *import java.io.IOException; import java.io.PrintWriter; import java.sql.DriverManager; import jav

-->大家好,我正在尝试将servlet与mysql连接起来,但我不知道为什么会出现ClassNotFoundException?它发生在class.forName行中。数据库名为trial1,表名为data2,列ID、名称、薪水。请帮忙

        *import java.io.IOException;
        import java.io.PrintWriter;
        import java.sql.DriverManager;
        import java.sql.SQLException;
        import java.util.Properties;
        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 com.mysql.jdbc.Connection;
        import com.mysql.jdbc.ResultSet;
        import com.mysql.jdbc.Statement;
        /**
         * Servlet implementation class dbHandling
         */
        @WebServlet("/dbHandling")
        public class dbHandling extends HttpServlet {
            private static final long serialVersionUID = 1L;

            /**
             * @see HttpServlet#HttpServlet()
             */
            public dbHandling() {
                super();
                // TODO Auto-generated constructor stub
            }
            /**
             * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
             */
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                  final String JDBC_DRIVER="com.mysql.jdbc.Driver";  
                  final String DB_URL="jdbc:mysql://localhost/trial1";
                  //  Database credentials
                  final String USER = "root";
                  final String PASS = "root";
                  Connection conn = null ; 
                  // Set response content type
                  response.setContentType("text/html");
                  PrintWriter out = response.getWriter();
                  String title = "Database Result";
                  String docType =
                    "<!doctype html public \"-//w3c//dtd html 4.0 " +
                     "transitional//en\">\n";
                     out.println(docType +
                     "<html>\n" +
                     "<head><title>" + title + "</title></head>\n" +
                     "<body bgcolor=\"red\">\n" +
                     "<h1 align=\"center\">" + title + "</h1>\n");
                     Statement stmt = null;

                     try{

                     Class.forName("com.mysql.jdbc.Driver").newInstance();/*This is    where exception is occuring*/
                     conn = (Connection) DriverManager.getConnection(DB_URL,USER,PASS);
                     stmt = (Statement) conn.createStatement();
                     String sql;
                     sql = "SELECT * from data2";
                     System.out.println(sql);
                     ResultSet rs = (ResultSet) stmt.executeQuery(sql);
                     while(rs.next()){
                        int id  = rs.getInt("id");
                        int age = rs.getInt("age");
                        String salary = rs.getString("salary");
                        out.println("ID: " + id + "<br>");
                        out.println(", Age: " + age + "<br>");
                        out.println(", First: " + salary + "<br>");
                     }
                     out.println("</body></html>");
                     rs.close();
                     stmt.close();
                     conn.close();
                  }catch(SQLException se){
                     se.printStackTrace();
                  }catch(Exception e){
                     e.printStackTrace();
                  }finally{
                     try{
                        if(stmt!=null)
                           stmt.close();
                     }catch(SQLException se2){
                     }
                     try{
                        if(conn!=null)
                        conn.close();
                     }catch(SQLException se){
                        se.printStackTrace();
                     }
                  } 
            }
        }

这个错误意味着应用程序正在寻找一个它找不到的类,正如您正确地指出的那样,这个错误就出现了

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

这是因为系统正在尝试加载您尚未放置在类路径中的mysql连接器类。请下载最新的Jar文件并将其放置在类路径中。有关如何将jar添加到类路径的更多信息,请参见

下载最新的mysql驱动程序jar文件,并将其放置在项目类路径中。请添加完整的stacktrace,以便查看未找到的类。