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错误。我已经使用tomcat9和java14将mysql-connector-java8.0.11.jar添加到webinf.am的lib文件夹中。
1条答案
按热度按时间qmelpv7a1#
通常,如果您遇到404错误,则意味着找不到该页面(可能是您的url不正确),如果doget方法“连接到数据库部分”中存在错误,服务器将以500错误响应。
https://www.codejava.net/java-ee/servlet/solved-tomcat-error-http-status-404-not-found
如果有帮助,请告诉我。