[英]What will be the output of following Java program? And why I am getting error
[英]What is missing in the following steps to test db connection from eclipse using tomcat and java ?am getting 404 error
My code to test db connection from eclipse is given below
導入 java.io.IOException; 導入 java.io.PrintWriter;
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 java.sql.*;
@WebServlet("/TestDbServlet")
public class TestDbServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user="user01";
String pass="pass01";
String jdbcUrl = "jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimezone=UTC";
String driver = "com.mysql.cj.jdbc.Driver";
// get connection to database
try {
PrintWriter out = response.getWriter();
out.println("Connecting to database: " + jdbcUrl);
Class.forName(driver);
Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);
out.println("SUCCESS!!!");
myConn.close();
}
catch (Exception exc) {
exc.printStackTrace();
throw new ServletException(exc);
}
}
當我在服務器上運行此程序時出現 404 錯誤。我已使用帶有 java14 的 tomcat9 將 mysql-connector-java8.0.11.jar 添加到 webinf.am 的 lib 文件夾中。
通常,如果您收到 404 錯誤,則表示未找到該頁面(可能是您的 url 不正確),如果 doGet 方法“與數據庫部分的連接”中存在錯誤,服務器將響應500 錯誤。
https://www.codejava.net/java-ee/servlet/solved-tomcat-error-http-status-404-not-found
讓我知道這是否有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.