gpt4 book ai didi

mysql - 使用 Java Servlet 访问 MySQL?

转载 作者:行者123 更新时间:2023-11-29 02:00:41 27 4
gpt4 key购买 nike

解决方案:

我添加了这段代码

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

Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");

谢谢大家回答我的问题

====================

我有问题,我尝试使用 servlet 将数据插入 mysql 数据库,但我无法访问 MySQL

  • 数据库名称:test
  • 表名:test
  • 我已经将 jdbc 连接器添加到项目库中
  • 我正在使用 JDK 1.7、NetBeans 7.3、MySQL 5.6、Tomcat 7.0、Connector/J 5.1.24

1- 这是 sign_up.jsp 页面中的“表单操作”:

<form action="RegisterUser" method="post">
<td><input type="submit" value="Submit"></td>
</form>

2- 这是 RegisterUser.java servlet:

import java.io.IOException;
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.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

@WebServlet(urlPatterns = {"/RegisterUser"})
public class RegisterUser extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {

try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root");
Statement s = (Statement) con.createStatement();

String name = "Hassan3";
int phone = 123456;

String insert = "INSERT INTO test VALUES ('\" + name + \"', \" + phone + \")";

s.executeUpdate(insert);

s.close();
con.close();

}catch(Exception e){
throw new SecurityException("Class not found " + e.toString());
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(RegisterUser.class.getName()).log(Level.SEVERE, null, ex);
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(RegisterUser.class.getName()).log(Level.SEVERE, null, ex);
}
}

@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

3-异常结果:

HTTP Status 500 - Class not found java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/test

type Exception report

message Class not found java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/test

description The server encountered an internal error that prevented it from fulfilling this request.

exception
java.lang.SecurityException: Class not found java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/test
RegisterUser.processRequest(RegisterUser.java:66)
RegisterUser.doPost(RegisterUser.java:173)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.39 logs.

4- 但是当我使用相同的代码但在“没有 servlet 或 web 应用程序”的 java 文件中时它工作正常:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Test {
public static void main(String[] args){
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root");
Statement s = (Statement) con.createStatement();

String name = "Hassan4";
int phone = 8985895;

String insert = "INSERT INTO test VALUES ('" + name + "', " + phone + ")";

s.executeUpdate(insert);

s.close();
con.close();
System.out.println("done");
}catch(Exception e){
throw new SecurityException("Class not found " + e.toString());
}
}

那么servlet有什么问题呢? 为什么代码适用于 java 应用程序。但它不适用于网络应用程序。?

最佳答案

你得到 Class not found java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/test

这意味着当您从 Web 应用程序运行它时,JRE 在类路径中找不到类。

如果此代码在您的独立系统中运行,则意味着您需要在某个地方有一个包含 com.mysql.jdbc.Driver 类(所谓的 JDBC 驱动程序)的 JAR 文件.这个 JAR 需要在 Tomcat 中可见。因此,我建议将 mysql-jdbc.jar 放在项目的 /WEB-INF/lib 目录的物理位置。

或者,您可以在此处使用 JDBC 驱动程序等第三方库

Right Click Project Name--> Properties

enter image description here

从您的 NetBeans IDE

然后重启 Tomcat 就可以了。

其次,你不需要

import com.mysql.jdbc.Driver;

在你的 Servlet 中。

关于mysql - 使用 Java Servlet 访问 MySQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15799841/

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