ILikeTurtles
ILikeTurtles

Reputation: 1022

Cannot make database connection due to servlet errors

In the below code I have two files. One is for my database connectivity and the other is for my html based jsp file. I am attempting to check the version of my database and print it to the screen. I am trying to return the value of CheckVersion and use out.println to put it on the webpage.

I am getting the follow errors.

type Exception report

message

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

exception

org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP

PWC6199: Generated servlet error: package com.sun.xml.rpc.processor.modeler.j2ee.xml does not exist

PWC6199: Generated servlet error: package databaseFiles does not exist

PWC6197: An error occurred at line: 18 in the jsp file: /index.jsp PWC6199: Generated servlet error: cannot find symbol symbol: class DatabaseManagement location: class org.apache.jsp.index_jsp

PWC6197: An error occurred at line: 18 in the jsp file: /index.jsp PWC6199: Generated servlet error: cannot find symbol symbol: class DatabaseManagement location: class org.apache.jsp.index_jsp

What am I doing wrong? I am using netbeans and it is not reporting any problems with the way I created my file. index.jsp is in the root area and I created a folder named databaseFiles that is holding my DatabaseManagement.java file.

Thanks.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package databaseFiles;

import com.sun.corba.se.impl.util.Version;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Aaron
 */
public class DatabaseManagement {
    private final String urlDB = "jdbc:mysql://correct:3306/javaBBS";
    private final String userDB = "correct";
    private final String passwordDB = "correct";
    Connection conDB = null;
    Statement st = null;
    ResultSet rs = null;

    /*
     * 
     * 
     */
    public DatabaseManagement() {

    }

    /*
     * 
     */
    public void OpenConnection()
    {
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch(Exception e) {
                System.out.println("Could not load database driver");
            }
            conDB = DriverManager.getConnection(urlDB, userDB, passwordDB);
        } catch(SQLException ex) {
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

    /*
     * Closes the current database object
     */
    public void CloseConnection() {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (conDB != null) {
                conDB.close();
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.WARNING, ex.getMessage(), ex);
        }
    }

     /**
     * Checks the database version
     */
    public String CheckVersion() throws SQLException {
        try {
            st = conDB.createStatement();
            rs = st.executeQuery("SELECT VERSION()");
        } catch(Exception e) {
            System.out.println("Could not get the version of the DB.");
        }
        if (rs.next()) {
            System.out.println(rs.getString(1));
        }
        return rs.getString(1);
    }
}

The below is index.jsp.

<%-- 
    Document   : index
    Created on : May 30, 2013, 1:48:03 PM
    Author     : Aaron
--%>

<%@page import="com.sun.xml.rpc.processor.modeler.j2ee.xml.string"%>
<%@page import="databaseFiles.DatabaseManagement"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
<%
    out.println("<br/>Your IP address is " + request.getRemoteAddr());
    String userAgent = request.getHeader("user-agent");
    String browser = "unknown";
    out.print("<br/>and your browser is ");
    if (userAgent != null) {
    if (userAgent.indexOf("MSIE") > -1) {
    browser = "MS Internet Explorer";
    }
    else if (userAgent.indexOf("Firefox") > -1) {
    browser = "Mozilla Firefox";
    }
    else if (userAgent.indexOf("Opera") > -1) {
    browser = "Opera";
    }
    else if (userAgent.indexOf("Chrome") > -1) {
    browser = "Google Chrome";
    }
    else if (userAgent.indexOf("Safari") > -1) {
    browser = "Apple Safari";
    }
    }
    out.println(browser);

    DatabaseManagement dbConnector = new DatabaseManagement();
    dbConnector.OpenConnection();
    out.println(dbConnector.CheckVersion());
    dbConnector.CloseConnection();
 %>
    </body>
</html>

Upvotes: 0

Views: 1794

Answers (1)

austin
austin

Reputation: 5876

I think your project layout is the problem. It should be something like this:

root
    WEB-INF
        classes
            databaseFiles
                DatabaseManagement.class
    index.jsp

So you should have your compiled .class files in the WEB-INF/classes directory and your .jsp files can go anywhere in the web application's root directory. Also, be sure that you're using the .class file and not the .java file.

Upvotes: 1

Related Questions