简体   繁体   中英

MySQL jdbc connection error

I am working on a JSP project and getting MySQL and JDBC connection error. i have tried myself as below.

<%@ 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>

I am using cPanel 11.x and Apache tomcat 7 and have MySQL 5.xx

I am putting here full error that my browser show:

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

This way of obtaining connection from jsp is not a good practice always.My request to you follow some tutorials & learn jsp syntax.In place of your code Try Below code to work:

Note: Before executing make sure that you have mysql-connector-java-ver-bin.jar in your lib folder & 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);
          }

    %>

Error is here:

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>

exception is not found.

use

 cnfe.printStackTrace(response.getWriter());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM