www.webdeveloper.com
Results 1 to 7 of 7

Thread: Why Can't I Connect to MySQL with JSP?

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Location
    USA
    Posts
    352

    Unhappy Why Can't I Connect to MySQL with JSP?

    Here is the code I'm trying to use:

    Code:
    <&#37;
    String mysql_DRIVER = "com.mysql.jdbc.Driver";
    String mysql_USERNAME = "hre3oadmin";
    String mysql_PASSWORD = "@Room15";
    String mysql_STRING = "jdbc:mysql://10.10.100.200:3306/hreo_main_db";
    
    Connection connection = null;
    
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection(mysql_STRING,mysql_USERNAME,mysql_PASSWORD);
    
    if(!connection.isClosed())
    	out.println("Successfully connected to " + "MySQL server using TCP/IP...");
    	connection.close();
    }
    catch(Exception ex){
    	out.println("Unable to connect to database.");
    }
    
    %>
    The error I get is as follows:
    HTTP Status 500 -

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to load class for JSP
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:156)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    root cause

    org.apache.jasper.JasperException: Unable to load class for JSP
    org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:620)
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:144)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    root cause

    java.lang.ClassNotFoundException: org.apache.jsp.questionnaire.classes.mysql_jsp
    java.net.URLClassLoader$1.run(Unknown Source)
    java.security.AccessController.doPrivileged(Native Method)
    java.net.URLClassLoader.findClass(Unknown Source)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:134)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
    org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:618)
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:144)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
    I'm thinking I'm not pointing to the mysql driver correctly.

    I have mysql-connector-java-5.1.7-bin.jar in the following directories:
    • /exit/WEB-INF/classes/lib
    • /exit/WEB-INF/lib
    • /exit/WEB-INF


    Any help I can get would be incredibly appreciated. I've never touched JSP a day in my life... it's a lot more difficult than PHP it seems .

    Thanks

  2. #2
    Join Date
    Jan 2005
    Location
    USA
    Posts
    352
    Here's another error I'm getting if I refresh enough:

    HTTP Status 500 -

    type Exception report

    message

    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: 7 in the jsp file: /questionnaire/classes/mysql.jsp
    Connection cannot be resolved to a type
    4: String mysql_PASSWORD = "@Room1";
    5: String mysql_STRING = "jdbc:mysql://10.10.100.200:3306/hreo_main_db";
    6:
    7: Connection connection = null;
    8:
    9: Class.forName("com.mysql.jdbc.Driver").newInstance();
    10: connection = DriverManager.getConnection(mysql_STRING,mysql_USERNAME,mysql_PASSWORD);


    An error occurred at line: 10 in the jsp file: /questionnaire/classes/mysql.jsp
    DriverManager cannot be resolved
    7: Connection connection = null;
    8:
    9: Class.forName("com.mysql.jdbc.Driver").newInstance();
    10: connection = DriverManager.getConnection(mysql_STRING,mysql_USERNAME,mysql_PASSWORD);
    11:
    12: if(!connection.isClosed())
    13: out.println("Successfully connected to " + "MySQL server using TCP/IP...");


    An error occurred at line: 73 in the generated java file
    Syntax error on token "catch", Identifier expected

    An error occurred at line: 75 in the generated java file
    out cannot be resolved

    An error occurred at line: 75 in the generated java file
    _jspx_out cannot be resolved

    An error occurred at line: 76 in the generated java file
    out cannot be resolved

    An error occurred at line: 76 in the generated java file
    out cannot be resolved

    An error occurred at line: 77 in the generated java file
    out cannot be resolved

    An error occurred at line: 78 in the generated java file
    _jspx_page_context cannot be resolved

    An error occurred at line: 78 in the generated java file
    _jspx_page_context cannot be resolved

    An error occurred at line: 80 in the generated java file
    Syntax error on token "finally", { expected

    An error occurred at line: 81 in the generated java file
    _jspx_page_context cannot be resolved

    Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:317)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
    Apache Tomcat/6.0.18
    Thanks for any help I can receive.

  3. #3
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    you need to have import statements in your jsp.

  4. #4
    Join Date
    Jan 2005
    Location
    USA
    Posts
    352
    Quote Originally Posted by chazzy View Post
    you need to have import statements in your jsp.
    How do I go about doing this?

    I'm new to JSP (I use PHP heavily).

    Thanks for your help

  5. #5
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634

  6. #6
    Join Date
    Jan 2005
    Location
    USA
    Posts
    352

    Red face Getting closer...

    Quote Originally Posted by chazzy View Post
    Ok, I was able to get a little further, but now I'm at a roadblock again.

    Here is my current code:

    Code:
    <&#37;@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
    <%
    
    
    
    String mysql_DRIVER = "com.mysql.jdbc.Driver";
    String mysql_USERNAME = "hreoadmin";
    String mysql_PASSWORD = "@Room1";
    String mysql_STRING = "jdbc:mysql://10.10.100.200:3306/hreo_main_db";
    
    Connection connection=null;
    ResultSet rs;
    
    response.setContentType("text/html");
    
    try{
    	// Load the database driver
    	Class.forName(mysql_DRIVER);
    }  
    finally{
    }
    
    %>
    Here is the error page I get:

    org.apache.jasper.JasperException: An exception occurred processing JSP page /questionnaire/classes/mysql.jsp at line 18

    15:
    16: try{
    17: // Load the database driver
    18: Class.forName(mysql_DRIVER);
    19: }
    20: finally{
    21: }


    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    root cause

    javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:852)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
    org.apache.jsp.questionnaire.classes.mysql_jsp._jspService(mysql_jsp.java:83)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    root cause

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:128)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
    java.lang.ClassLoader.loadClassInternal(Unknown Source)
    java.lang.Class.forName0(Native Method)
    java.lang.Class.forName(Unknown Source)
    org.apache.jsp.questionnaire.classes.mysql_jsp._jspService(mysql_jsp.java:72)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    Thanks again for all your help .
    Last edited by tgrk35; 05-13-2009 at 09:47 AM. Reason: Revised the error message I get and the code...

  7. #7
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    is the mysql driver in your WEB-INF/lib directory?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles