无法在intellij中使用maven连接mysql数据库

时间:2017-09-25 09:13:35

标签: java mysql maven jsp intellij-idea

当我尝试在我的应用程序中登录时,JSP和IntelliJ IDE中的新功能遇到错误java.lang.ClassNotFoundException:com.mysql.jdbc.Driver。我在开发JSP项目时使用IntelliJ作为我的IDE。我尝试使用mysql-connector-java版本6.0.4,5.1.28,5.1.23。但它仍然没有工作,并提醒同样的错误。

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>SmartRec</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.4</version>
        </dependency>

    </dependencies>

</project>

EmployeeRegistration.java

public class EmployeeRegistration extends HttpServlet {

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

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String n=request.getParameter("name");
        String l=request.getParameter("address");
        String cwp = request.getParameter("cwposition");
        String ph=request.getParameter("phone");
        String m=request.getParameter("mail");
        String psw=request.getParameter("password");

        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/smartrecruit","root","");

            System.out.println("conect");
            PreparedStatement ps=con.prepareStatement("insert into employee(name,address,cwposition,phone,email,password) values(?,?,?,?,?,?)");

            ps.setString(1,n);
            ps.setString(2,l);
            ps.setString(3,cwp);
            ps.setString(4,ph);
            ps.setString(5,m);
            ps.setString(6,psw);

            int i=ps.executeUpdate();
            if(i>0){
                String name = n;
                request.setAttribute("message", name);

            }





        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        out.close();
    }

1 个答案:

答案 0 :(得分:0)

查看此question。它与你的有关。

他们基本上会说:

  1. 从Maven中删除依赖项。
  2. here手动下载。
  3. 将jar添加到/WEB-INF/lib$TOMCAT\lib文件夹。
  4. 同样为了将来参考,请查看官方Tomcat文档:http://tomcat.apache.org/tomcat-8.0-doc/jndi-datasource-examples-howto.html