MySQL jdbc连接错误

时间:2015-12-10 05:15:50

标签: 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
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

2 个答案:

答案 0 :(得分:0)

这种从jsp获取连接的方式并不总是很好的做法。我请求您遵循一些教程和&amp;学习jsp语法。代替您的代码尝试使用以下代码:

注意:执行前请确保mysql-connector-java-ver-bin.jar文件夹中有lib个文件夹。 class-path

<%@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);
          }

    %>

答案 1 :(得分:0)

错误在这里:

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>
找不到

异常

使用

 cnfe.printStackTrace(response.getWriter());