java.sql.SQLException:找不到合适的驱动程序。已经将连接器jar添加到Tomcat的lib文件夹中

时间:2016-12-06 04:27:17

标签: java html mysql eclipse servlets

将Tomcast 6.0与Eclipse Java Jee Neon结合使用。我已经把jar放在tomcast的lib中了:" C:\ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ lib \ mysql-connector-java-5.1.40-bin.jar"

还有什么可能导致此问题?真的很感激帮助!

我的代码(Servlet):

package Servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub

        String name = req.getParameter("name");
        String lastname = req.getParameter("lastname");
        String cedula = req.getParameter("cedula");

        try {
            InsertClient(name, lastname, cedula);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        resp.getWriter().println("<html>");
        resp.getWriter().println("<head>"); 
        resp.getWriter().println("<title>Cliente agregado!</title>");
        resp.getWriter().println("</head>");
        resp.getWriter().println("<body>");
        resp.getWriter().println("Cliente fue agregado.");
        resp.getWriter().println("</body>");
        resp.getWriter().println("</html>");
    }

    public static void InsertClient(String Name, String Lastname, String Cedula) throws Exception{      
        try{
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "1990");

            PreparedStatement insert = con.prepareStatement("INSERT INTO Cliente(Nombre, Apellido, Cedula) VALUES ('"+Name+"', '"+Lastname+"', '"+Cedula+"')");

            insert.executeUpdate();

        }catch(Exception e){System.out.println(e);}
        finally{System.out.println("Insertion complete.");}


    }   

}

2 个答案:

答案 0 :(得分:0)

如果您使用Dynamic Web Project创建项目,则必须在Web-Inf lib文件夹下添加所有jar文件。希望这会有所帮助。

答案 1 :(得分:-1)

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

只需添加以上代码,然后将“mysql.jar”文件添加到Webapp libraries即可。它对我有用。它也可以帮助你。