当我想使用JSP连接到MySql时,我输入了该错误" org.apache.jasper.JasperException: Unable to compile class for JSP, An error occurred at line: 14 in the jsp file: /index.jsp DbClass cannot be resolved
"而在我的代码中,一切看起来都很正常。首先,我认为它将由MySQL jar文件引起,但没有。它不是我项目中的类文件(DbClass.class)吗?
的index.jsp
<%@ page import="com.sampleWeb.*,java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>sampleWeb</title>
</head>
<body>
<%
Connection c = DbClass.connect();
out.println(c);
DbClass.closeConnection(c);
%>
</body>
</html>
DbClass.java
package com.sampleWeb;
import java.sql.*;
public class DbClass {
public DbClass(){}
public static Connection connect() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
return DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
} catch() {
return null;
}
}
public static boolean closeConnection(Connection conn) {
try{
conn.close();
return true;
} catch(Exception e) {
return false;
}
}
}
答案 0 :(得分:0)
您的代码有效。我测试了它。
唯一的问题是空的括号} catch() {
我可以想象DbClass中的编译问题可能使其无法解决
为了测试这个问题并将问题本地化,请尝试将DbClass简化为简单的返回:
package com.sampleWeb;
public class DbClass {
public static String connect() {
return "test";
}
}