【问题标题】:Wildfly Connection pool with PostgreSQL. Getting a JdbcSQLExceptionWildfly 与 PostgreSQL 的连接池。获取 JdbcSQLException
【发布时间】:2017-04-11 07:45:40
【问题描述】:

我正在尝试为一个只有一个 servlet 的简单 Web 项目实现连接池。但由于某种原因,我得到了一个 JdbcSQLException org.h2.jdbc.JdbcSQLException:ERROR [stderr] (default task-4) Table "EMP" not found; 这是我第一次在 Wildfly 中配置连接池。经过一番研究,我仍然无法弄清楚如何解决这个问题。我想我的 Intelij 项目中可能没有正确配置某些东西。 我做了以下步骤:

  • 在 Wildfly 中部署了 JDBC 驱动程序 (postgresql-9.4-1201.jdbc4.jar);
  • 配置了 JDBC 数据源(名称:PostgresDS,JNDI:java:/PostgresDS);
  • 在Wildfly中测试了连接(连接URL:jdbc:postgresql://localhost:5433/sample_db);
  • 已成功连接到数据库 PostgresDS。

在 Intellij 项目中:

  • 创建了一个Data Source(也叫PostgresDS),成功连接数据库(URL:jdbc:postgresql://localhost:5433/sample_db)。

但是当我尝试运行 servlet 时,会发生 JdbcSQLException。

[2016-11-27 06:03:38,447] Artifact employees-jsp:war exploded: Artifact is deployed successfully
[2016-11-27 06:03:38,447] Artifact employees-jsp:war exploded: Deploy took 1 619 milliseconds
18:03:45,222 ERROR [stderr] (default task-4) org.h2.jdbc.JdbcSQLException: Таблица "EMP" не найдена
18:03:45,222 ERROR [stderr] (default task-4) Table "EMP" not found; SQL statement:
18:03:45,223 ERROR [stderr] (default task-4) SELECT * FROM emp [42102-173]

我在这里错过了什么?我需要在项目中配置任何 .xml 文件吗?

我会感谢所有提示、意见和批评。

这里是servlet:

    @WebServlet(name = "ServletDS", urlPatterns = {"/ServletEmp"})
public class ServletDS extends HttpServlet {
    @Resource(name = "PostgresDS")
    private DataSource dataSource;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Set up the Printwriter
        PrintWriter out = response.getWriter();
        response.setContentType("text/plain");

        // Get a connection to the DB
        Connection myConnection = null;
        Statement myStatement = null;
        ResultSet myResultSet = null;

        try {
            myConnection = dataSource.getConnection();

            // Create a SQL statement
            String sql = "SELECT * FROM emp";
            myStatement = myConnection.createStatement();

            // Execute SQL query
            myResultSet = myStatement.executeQuery(sql);

            // Process the result set
            while (myResultSet.next()) {
                String name = myResultSet.getString("ename");
                out.println(name);
            }

        } catch (Exception exc) {
            exc.printStackTrace();
        }

    }
}

【问题讨论】:

  • "找不到表 "EMP"" 对我来说似乎很清楚。您连接的数据库中没有表名emp。您可以通过创建表来解决此问题
  • postgresql JDBC 驱动程序永远不会抛出 org.h2.jdbc.JdbcSQLException 类型的异常。 H2 数据库驱动程序会这样做。你的配置一定有问题。一如既往地发布异常的完整堆栈跟踪会有所帮助。
  • @a_horse_with_no_name 如果数据库中没有表 emp,我不会发布这个问题。它就在那里。
  • 我刚刚解决了这个问题。我错过的是使用 JNDI 查找数据源以获取连接。这是我添加的代码:InitialContext ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup("PostgresDS"); myConnection = dataSource.getConnection();
  • 将此添加为答案,以便其他人在遇到类似问题时可以使用这些详细信息。将您的答案放在评论中不会有太大帮助!

标签: java postgresql servlets jdbc


【解决方案1】:

我刚刚解决了这个问题。我错过的是使用 JNDI 查找数据源以获取连接。这是我添加到代码中以获取连接的内容:

        InitialContext ctx = new InitialContext();
        DataSource dataSource = (DataSource) ctx.lookup("java:/PostgresDS");
        myConnection = dataSource.getConnection();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 2014-12-15
    • 2016-09-29
    • 2013-08-20
    • 2013-10-12
    相关资源
    最近更新 更多