【问题标题】:MySQL jdbc connection errorMySQL jdbc连接错误
【发布时间】: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 错误

标签: java mysql jsp jdbc


【解决方案1】:

这种从jsp 获取连接的方式始终不是一个好习惯。我要求您遵循一些教程并学习jsp 语法。代替您的代码尝试下面的代码工作:

注意:在执行之前确保您的lib文件夹中有mysql-connector-java-ver-bin.jarclass-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);
          }

    %>

【讨论】:

    【解决方案2】:

    错误在这里:

    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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-07
      • 2014-06-20
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 2015-09-05
      相关资源
      最近更新 更多