Servlet无法连接到mysql数据库

时间:2015-12-16 15:04:31

标签: mysql servlets jdbc


我是servlets的新手。我试图使用jdbc将数据插入到mysql服务器表中。我得到以下例外:
java.lang.ClassNotFoundException:com.mysql.jdbc.Driver

以下是导致此错误的代码段:

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

            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "infy@123");

这里“test”是MySQL的模式名称。我要删除它,或使用其他东西。 root - 用户名 infy @ 123 - 密码

以下是更新的代码文件:
1。 register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>DB connection program</title>
</head>
<body>
    <form action = "register" method = "post">
        Name: <input type="text" name = "userName" /> <br>
        Password: <input type = "password" name = "password" /> <br>
        Email ID: <input type = "text" name = "email" /> <br>
        Language: <select name = "language">
        <option>Hindi</option>
        <option>English</option>
        <option>French</option>
        </select> <br>
        <input type="submit" value = "Submit" />
    </form>
</body>
</html>

2。 RegisterServlet.java

package com.jwt.servlet;

import java.io.IOException;
import java.io.PrintWriter; 
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 RegisterServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

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

        String n = request.getParameter("userName");
        String p = request.getParameter("password");
        String e = request.getParameter("email");
        String c = request.getParameter("language");

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

            PreparedStatement ps = con.prepareStatement("insert into USERDETAILS values(?,?,?,?)");

            ps.setString(1, n);
            ps.setString(2, p);
            ps.setString(3, e);
            ps.setString(4, c);

            int i = ps.executeUpdate();
            if (i > 0)
                out.print("You are successfully registered...");

        } catch (Exception e2) {
            System.out.println(e2);
        }

        out.close();
    }


}

3。 Web.xml

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">
 <display-name>ServletDBExample1</display-name>

        <servlet>
        <servlet-name>Register</servlet-name>
        <servlet-class>com.jwt.servlet.RegisterServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Register</servlet-name>
        <url-pattern>/register</url-pattern>
    </servlet-mapping>


  <welcome-file-list>
    <welcome-file>register.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

    </web-app>

请建议。

谢谢你 拉胡

2 个答案:

答案 0 :(得分:1)

您的jsp正在向&#34; / register&#34;发送请求并且你的servlet有2个映射,&#34; / RegisterServlet&#34;从注释(假设你取消注释)和&#34; /index.jsp"来自web.xml。

要使表单生效,请将表单操作更改为其中一个映射。

答案 1 :(得分:0)

我不得不复制&#34; mysql-connector-java jar&#34;将文件放入WebContent / WEB-INF / lib文件夹中。
从那里右键单击它 - &gt;建立路径 这解决了我的问题。