gpt4 book ai didi

mysql - Servlet 无法连接到 mysql 数据库

转载 作者:行者123 更新时间:2023-11-28 23:41:15 26 4
gpt4 key购买 nike


我是 servlet 的新手。我正在尝试使用 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 的模式名称。我应该删除它,还是使用其他东西。根——用户名infy@123——密码

以下是更新后的代码文件:
1。注册.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>

请提出建议。

谢谢
拉胡尔

最佳答案

您的 jsp 正在向“/register”发送请求,您的 servlet 有 2 个映射,注释中的“/RegisterServlet”(假设您取消注释)和 web.xml 中的“/index.jsp”。

要使表单正常工作,请将表单操作更改为这些映射之一。

关于mysql - Servlet 无法连接到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34315399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com