Tomcat无法连接mysql数据库

问题描述 投票:0回答:1

我有一个带有管理员登录页面的 Web 应用程序。

当我尝试登录管理页面时,我收到此错误消息:

Type Exception Report

Message Unable to compile class for JSP:

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: [13] in the jsp file: [/adminlogact.jsp]
databasecon cannot be resolved
10:        
11:        // String user=null;
12:         //String password=null;
13:     Connection con=databasecon.getconnection();
14:         Statement st = con.createStatement();
15:         ResultSet rs = st.executeQuery("select * from admin where user='"+username+"' and pwd='"+password+"'");
16:        if(rs.next())


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:214)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:531)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:398)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:368)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:379)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:327)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
Note The full stack trace of the root cause is available in the server logs.

这是管理页面代码:

<%@page import="java.sql.*"%>
<%@page import="databaseconnection.*"%>
<%@ page session="true" %>
<%
    String username = request.getParameter("username");
    System.out.println(username);
    String password = request.getParameter("password");
    System.out.println(password);
    try{
       
       // String user=null;
        //String password=null;
    Connection con=databasecon.getconnection();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from admin where user='"+username+"' and pwd='"+password+"'");
       if(rs.next())
        {
         String  user = rs.getString(2);
           session.setAttribute("user",user);
           System.out.println("User:"+user);
            response.sendRedirect("adminhome.jsp");
        }
       else 
        {
            response.sendRedirect("admin.html?m1=LoginFail");
                }
    }
    catch(Exception e)
    {
        System.out.println("Error in adminlogact"+e.getMessage());
    }
%>

这是数据库的java代码:

package databaseconnection;
import java.sql.*;

public class databasecon
{
static Connection co;
public static Connection getconnection()
{


    try
    {
        Class.forName("com.mysql.cj.jdbc.Driver");
        co = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","mypass");


    }
    catch(Exception e)
    {
        System.out.println("Database Error"+e);
    }
    return co;
}

}

我能够登录数据库并查看表,但似乎 tomcat 找不到数据库。 catalina日志没有任何错误

目录结构:

C:\PROGRAMDATA\TOMCAT9
├───conf
│   └───Catalina
│       └───localhost
├───logs
├───temp
├───webapps
│   ├───docs
│   │   ├───annotationapi
│   │   ├───api
│   │   ├───appdev
│   │   │   └───sample
│   │   │       ├───docs
│   │   │       ├───src
│   │   │       │   └───mypackage
│   │   │       └───web
│   │   │           ├───images
│   │   │           └───WEB-INF
│   │   ├───architecture
│   │   │   ├───requestProcess
│   │   │   └───startup
│   │   ├───config
│   │   ├───elapi
│   │   ├───images
│   │   │   └───fonts
│   │   ├───jaspicapi
│   │   ├───jspapi
│   │   ├───META-INF
│   │   ├───servletapi
│   │   ├───tribes
│   │   ├───WEB-INF
│   │   │   └───jsp
│   │   └───websocketapi
│   ├───examples
│   │   ├───jsp
│   │   │   ├───async
│   │   │   ├───cal
│   │   │   ├───checkbox
│   │   │   ├───colors
│   │   │   ├───dates
│   │   │   ├───error
│   │   │   ├───forward
│   │   │   ├───images
│   │   │   ├───include
│   │   │   ├───jsp2
│   │   │   │   ├───el
│   │   │   │   ├───jspattribute
│   │   │   │   ├───jspx
│   │   │   │   ├───misc
│   │   │   │   ├───simpletag
│   │   │   │   └───tagfiles
│   │   │   ├───jsptoserv
│   │   │   ├───num
│   │   │   ├───security
│   │   │   │   └───protected
│   │   │   ├───sessions
│   │   │   ├───simpletag
│   │   │   ├───snp
│   │   │   ├───tagplugin
│   │   │   └───xml
│   │   ├───META-INF
│   │   ├───servlets
│   │   │   ├───images
│   │   │   └───nonblocking
│   │   ├───WEB-INF
│   │   │   ├───classes
│   │   │   │   ├───async
│   │   │   │   ├───cal
│   │   │   │   ├───checkbox
│   │   │   │   ├───colors
│   │   │   │   ├───compressionFilters
│   │   │   │   ├───dates
│   │   │   │   ├───error
│   │   │   │   ├───examples
│   │   │   │   ├───filters
│   │   │   │   ├───http2
│   │   │   │   ├───jsp2
│   │   │   │   │   └───examples
│   │   │   │   │       ├───el
│   │   │   │   │       └───simpletag
│   │   │   │   ├───listeners
│   │   │   │   ├───nonblocking
│   │   │   │   ├───num
│   │   │   │   ├───sessions
│   │   │   │   ├───trailers
│   │   │   │   ├───util
│   │   │   │   ├───validators
│   │   │   │   └───websocket
│   │   │   │       ├───chat
│   │   │   │       ├───drawboard
│   │   │   │       │   └───wsmessages
│   │   │   │       ├───echo
│   │   │   │       └───snake
│   │   │   ├───jsp
│   │   │   ├───lib
│   │   │   └───tags
│   │   └───websocket
│   ├───Feedback_Analysis
│   │   ├───images
│   │   ├───META-INF
│   │   └───WEB-INF
│   │       ├───classes
│   │       │   ├───conf
│   │       │   └───java
│   │       │       ├───databaseconnection
│   │       │       └───opt
│   │       │           └───databse
│   │       │               └───conn
│   │       └───lib

网络应用程序是 Feedback_Analysis

java mysql tomcat jdbc
1个回答
0
投票

感谢@stdunbar 为我指明了正确的方向。 我使用“javac”手动编译了java类databaseconnection,并将databaseconnection.class文件放在databaseconnection.java文件的位置,这解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.