2013-05-31 66 views
0

在下面的代碼中我有兩個文件。一個用於我的數據庫連接,另一個用於我的基於html的jsp文件。我正在嘗試檢查我的數據庫的版本並將其打印到屏幕上。我正在嘗試返回CheckVersion的值,並使用out.println將其放在網頁上。由於servlet錯誤無法建立數據庫連接

我收到以下錯誤。

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

我在做什麼錯?我正在使用netbeans,它不報告我創建文件的方式有任何問題。 index.jsp位於根區域,我創建了一個名爲databaseFiles的文件夾,它持有我的DatabaseManagement.java文件。

謝謝。

/* 
* 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); 
    } 
} 

以下是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> 
+0

你可以發佈'index.jsp'嗎?我認爲它可能缺少一個導入聲明。 – austin

+0

@austin我已經更新了上面的內容來打破它們。對於那個很抱歉。沒有意識到它把這兩個文件合併爲一個大項目。 – ILikeTurtles

回答

1

我認爲你的項目佈局是問題。它應該是這樣的:

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

所以,你應該有你的編譯的.class文件中WEB-INF/classes目錄和您的.jsp文件可以在Web應用程序的根目錄去任何地方。另外,請確保您使用.class文件而不是.java文件。

+0

我在netbeans中創建了上述內容。但是,如何確保將我的課程構建在該文件夾內?當我按F6運行它;有沒有辦法強制java文件編譯並將該類文件放在該文件夾方案中? – ILikeTurtles

+0

如果您使用的是Netbeans Web應用程序項目,Netbeans應將這些類放在正確的目錄中。如果你只是使用普通的Java項目,它可能會把它們放在類似「build」的東西中。試着看看你的項目設置,看看是否有一個輸出目錄的選項。 – austin

+0

我明白了。現在剩下的就是我的錯誤,如上所述,包com.sun.xml.rpc.processor.modeler.j2ee.xml不存在 – ILikeTurtles

相關問題