【发布时间】:2015-12-10 05:15:49
【问题描述】:
我正在处理一个 JSP 项目并遇到 MySQL 和 JDBC 连接错误。我已经尝试过如下方式。
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%!
// mysql driver
String driver = "com.mysql.jdbc.Driver";
// the "url" to our DB, the last part is the name of the DB
String url = "jdbc:mysql://localhost/askvaidy_test";
// the default DB username and password may be the same as your control panel login
String name = "askvaidy_test";
String pass = "h4R9-+9}Pe]w";
%>
<html>
<head>
<title>testServlet</title>
</head>
<body>
<p>Attempting to open JDBC connection to:... </p> <%=url%>
<%
try
{
// Test the DB connection by making an empty table
String tableStr = "CREATE.....table test (testid mediumint(8), name varchar(100))";
Class.forName( driver );
// initialize the Connection, with our DB info ...
Connection con = DriverManager.getConnection( url, name, pass );
Statement stat = con.createStatement();
%>
<p> executing: <%=tableStr%></p>
<%
stat.executeUpdate( tableStr );
%>
<p> success.... </p>
<%
// close connection
con.close();
}
catch (SQLException sqle)
{ out.println("<p> Error opening JDBC, cause:</p> <b> " + sqle + "</b>"); }
catch(ClassNotFoundException cnfe)
{ out.println("<p> Error opening JDBC, cause:</p> <b>" + cnfe + "</b>"); }
%>
<% exception.printStackTrace(response.getWriter()); %>
</body>
</html>
我正在使用 cPanel 11.x 和 Apache tomcat 7 并且拥有 MySQL 5.x.x
我将浏览器显示的完整错误放在这里:
HTTP Status 500 - Unable to compile class for JSP:
type Exception report
message Unable to compile class for JSP:
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [15] in the generated java file: [/usr/local/easy/share/easy-tomcat7/work/Catalina/askvaidya.com/_/org/apache/jsp/jsp_005ftest_jsp.java]
Only a type can be imported. com.mysql.jdbc.Driver resolves to a package
An error occurred at line: 52 in the jsp file: /jsp_test.jsp
exception cannot be resolved
49: { out.println("<p> Error opening JDBC, cause:</p> <b>" + cnfe + "</b>"); }
50:
51: %>
52: <% exception.printStackTrace(response.getWriter()); %>
53: </body>
54: </html>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:463)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
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.42 logs.
Apache Tomcat/7.0.42
【问题讨论】:
-
如果你能告诉我我的错误,请不要投反对票
-
请发布您的错误堆栈跟踪。
-
我已经发布了我的浏览器显示的完整错误和完整代码
-
我不认为这是 DB 错误,这是您的 jsp 错误