Java Servlet中的PostgreSQL连接从数据库中检索信息。得到错误 [英] PostgreSQL connection in Java Servlet to Retrieve Information from the DB. Getting Error

查看:111
本文介绍了Java Servlet中的PostgreSQL连接从数据库中检索信息。得到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难完成这项工作。我可以毫无问题地连接到数据库,但是我不能让它向我显示html页面。它不会运行。

I am having difficulty with getting this work. I can connect to the database without problem, however I can not make it show me the html page. It does not run.

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 driver
        try 
        {
              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 STDOUT
        PrintWriter out = response.getWriter();

        out.println("<HTML><Head><Title>Bedrock</Title></Head>");
        out.println("<Body><H1>Bedrock</H1>");

        try
        {
                // Declare our statement
                Statement statement = dbcon.createStatement();

                String query = "SELECT name, dept, ";
                query +=       "       jobtitle ";
                query +=       "FROM   employee ";

                // Perform the query
                ResultSet rs = statement.executeQuery(query);

                out.println("<table border>");

                // Iterate through each row of rs
                while (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();
    }
}

我得到以下错误,任何人都知道为什么?:

I get the following error any body knows why?:

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.


推荐答案


我得到以下信息错误任何人都知道为什么?

I get the following error any body knows why?

你得到那个错误主体的原因是你的 ClassNotFoundException的处理程序 丢弃原始异常堆栈跟踪。您正在将原始异常的消息写入 System.err ,但(显然)不会进入正常的系统日志。 (您可能会在catalina.out文件中找到该消息...取决于Tomcat的配置/启动方式。)

The reason why you are getting that error body is that your handler for ClassNotFoundException is throwing away the original exception stacktrace. You are writing the original exception's message to System.err but (obviously) that won't go into the normal system logs. (You'll probably find find the message in the "catalina.out" file ... depending on how Tomcat is configured / launched.)

正确的方法这是要么排除异常;例如。

The right way to do this is to either chain the exception; e.g.

    throw new ServletException("Class not found Error", e);

或在您捕获它时明确记录。如果你明确地记录它,请确保记录异常,而不仅仅是异常消息。

or log it explicitly at the point that you catch it. And if you do log it explicitly, make sure that you log the exception, and not just the exception message.

我们除非我们看到原始异常堆栈跟踪,否则无法告诉您问题的实际根本原因...但最可能的两个原因是:

We can't tell you the actual root cause of the problem unless we see the original exception stacktrace ... but the two most likely causes are:


  • 类加载器找不到所需的类;例如因为JAR文件不在正确的位置。

  • 在您尝试加载的类的初始化期间,某些类的类初始化失败。

这篇关于Java Servlet中的PostgreSQL连接从数据库中检索信息。得到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆