Java Servlet中的PostgreSQL連接,用于從數(shù)據(jù)庫(kù)檢索信息。遇到錯(cuò)誤
我收到以下錯(cuò)誤,任何人都知道為什么?
為什么你得到的是錯(cuò)誤的身體的原因是,你的處理程序ClassNotFoundException被 原始異常堆棧跟蹤。您正在將原始異常的消息寫(xiě)入,System.err但是(顯然)不會(huì)寫(xiě)入普通系統(tǒng)日志中。(您可能會(huì)在“catalina.out”文件中找到該消息……這取決于Tomcat的配置/啟動(dòng)方式。)
正確的方法是要么鏈接異常;要么鏈接異常。例如
throw new servletexception('Class not found Error', e);
或在您將其捕獲時(shí)將其明確記錄。并且如果確實(shí)記錄了日志,請(qǐng)確保記錄異常,而不僅僅是異常消息。
除非我們看到原始異常stacktrace,否則我們無(wú)法告訴您問(wèn)題的真正根本原因,但是最可能的兩個(gè)原因是:
該類(lèi)加載器找不到所需的類(lèi)。例如,因?yàn)镴AR文件不在正確的位置。在您嘗試加載的類(lèi)的初始化期間,某些類(lèi)的類(lèi)初始化失敗。解決方法我很難完成這項(xiàng)工作。我可以毫無(wú)問(wèn)題地連接到數(shù)據(jù)庫(kù),但是我無(wú)法使其顯示html頁(yè)面。它不會(huì)運(yùn)行。
import java.io.*;import java.sql.*;import java.text.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;public class ShowBedrock extends HttpServlet { public String getServletInfo() { return 'Servlet connects to PostgreSQL database and displays result of a SELECT'; } private Connection dbcon; // Connection for scope of ShowBedrock // 'init' sets up a database connection public void init(ServletConfig config) throws ServletException {String loginUser = 'postgres';String loginPasswd = 'supersecret';String loginUrl = 'jdbc:postgresql://localhost/bedrock';// Load the PostgreSQL drivertry { Class.forName('org.postgresql.Driver'); dbcon = DriverManager.getConnection(loginUrl,loginUser,loginPasswd);}catch (ClassNotFoundException ex){ System.err.println('ClassNotFoundException: ' + ex.getMessage()); throw new ServletException('Class not found Error');}catch (SQLException ex){ System.err.println('SQLException: ' + ex.getMessage());} } // Use http GET public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException {response.setContentType('text/html'); // Response mime type// Output stream to STDOUTPrintWriter out = response.getWriter();out.println('<HTML><Head><Title>Bedrock</Title></Head>');out.println('<Body><H1>Bedrock</H1>');try{// Declare our statementStatement statement = dbcon.createStatement();String query = 'SELECT name,dept,';query += ' jobtitle ';query += 'FROM employee ';// Perform the queryResultSet rs = statement.executeQuery(query);out.println('<table border>');// Iterate through each row of rswhile (rs.next()){ String m_name = rs.getString('name'); String m_dept = rs.getString('dept'); String m_jobtitle = rs.getString('jobtitle'); out.println('<tr>' +'<td>' + m_name + '</td>' + '<td>' + m_dept + '</td>' + '<td>' + m_jobtitle + '</td>' + '</tr>');}out.println('</table></body></html>');statement.close();}catch(Exception ex){out.println('<HTML>' + '<Head><Title>' + 'Bedrock: Error' + '</Title></Head>n<Body>' + '<P>SQL error in doGet: ' + ex.getMessage() + '</P></Body></HTML>');return;}out.close(); }}
任何機(jī)構(gòu)都知道為什么會(huì)出現(xiàn)以下錯(cuò)誤:
javax.servlet.ServletException: Class not found Error ShowBedrock.init(ShowBedrock.java:42) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) java.lang.Thread.run(Unknown Source)note The full stack trace of the root cause is available in the Apache Tomcat/6.0.35 logs.
