Java MySQL jdbc连接错误
我正在处理一个JSP项目,发现MySQL和JDBC连接错误。我已经试过了Java MySQL jdbc连接错误,java,mysql,jsp,jdbc,Java,Mysql,Jsp,Jdbc,我正在处理一个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 S
<%@ 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>
testServlet
正在尝试打开与以下对象的JDBC连接:
执行:
成功
我使用的是cPanel 11.x和ApacheTomcat 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
HTTP状态500-无法为JSP编译类:
类型异常报告
消息无法为JSP编译类:
说明服务器遇到内部错误,无法满足此请求。
例外
org.apache.jasper.jaspereException:无法为JSP编译类:
在生成的java文件:[/usr/local/easy/share/easy-tomcat7/work/Catalina/askvaidya.com/_/org/apache/jsp/jsp005ftest_jsp.java]的第[15]行出现错误
只能导入类型。驱动程序解析为一个包
jsp文件:/jsp\u test.jsp的第52行出现错误
无法解决异常
49:{out.println(“打开JDBC时出错,原因:“+cnfe+”);}
50:
51: %>
52:
53:
54:
堆栈跟踪:
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)
javaservlet.http.HttpServlet.service(HttpServlet.java:728)
注意,Apache Tomcat/7.0.42日志中提供了根本原因的完整堆栈跟踪。
ApacheTomcat/7.0.42
这种从jsp
获取连接的方法并不总是一种好的做法。我要求您遵循一些教程并学习jsp
语法。尝试以下代码来代替您的代码:
注意:在执行之前,确保您的lib
文件夹和类路径中有mysql connector java ver bin.jar
<%@page import="java.sql.*"%>
<html>
<head>
<title>Obtaining a Connection</title>
</head>
<body>
<h1>This Page Obtains a Connection to a Database and executes a query</h1>
<%
Connection conn = null;
ResultSet result = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (Exception e) {
System.out.println("Error occurred " + e);
}
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/askvaidy_test", "","*****");
}
catch (SQLException e) {
System.out.println("Error occurred " + e);
}
try {
stmt = conn.createStatement();
String command = "CREATE.....table test (testid mediumint(8), name varchar(100));";
statement.executeUpdate(command);
}
catch (SQLException e) {
System.out.println("Error occurred " + e);
}
%>
取得联系
此页获取到数据库的连接并执行查询
错误如下:
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>
如果你能告诉我我的错误,请不要往下看。请发布你的错误堆栈跟踪。我已经发布了我的浏览器显示的完整错误和完整代码。我不认为这是DB错误,这是你的jsp错误
cnfe.printStackTrace(response.getWriter());