【问题标题】:null data for second access用于第二次访问的空数据
【发布时间】:2014-09-28 21:00:42
【问题描述】:

好吧,遗憾的是我什至不知道如何描述这个错误大声笑:(

我想显示一张我已将其路径存储在数据库中的图像。我的代码在第一次访问数据库时运行良好。但是,我的代码第二次进入数据库时​​检索到 null,依此类推。我盯着我的代码看了几个小时,但找不到错误大声笑:(

getimage.jsp

<form action="getimage" method="get">
    <input type="text">
    <input type="submit" name="getimage">
</form>

<c:forEach var="image" items="${image_path}">
    <img src="${image}"/>
</c:forEach>

小服务程序:

    HttpSession session = request.getSession();
    session.removeAttribute("image_path");
    List<String> list_image_path = new ArrayList<String>();

    ImagePath img = ImageDao.getInstance().getPath(8);
    list_image_path.add(img.getImagePath());

    session.setAttribute("image_path", list_image_path);

    System.out.println(img);

    request.getRequestDispatcher("getimage.jsp").forward(request, response);

ImageDao.java

private ImageDao() {
    try {
        conn = MyDatabase.getConnection();
    } catch (SQLException e) {
        System.out.println( e.getClass() + " - Can not connect to the databse");
        e.printStackTrace();
    }
}

public static ImageDao getInstance() {
    if(instance == null) {
        synchronized( UsersDao.class) {
            if( instance == null ){
                instance = new ImageDao();
            }
        }
    }
    return instance;
}
public static ImagePath getPath( long id) {
    ImagePath path = new ImagePath();
    String sql = "SELECT * FROM item_image WHERE id=?";

    try {
        System.out.println(id + ": " + conn);
        PreparedStatement pstmt = conn.prepareStatement( sql );

        pstmt.setLong(1, id);
        ResultSet rs = pstmt.executeQuery();             // Fail right here

        if( rs.next() ) {
            System.out.println( rs.getLong("id") + ": " + rs.getString("path") + " " + id);
            path.setId( rs.getLong( "id" ));
            path.setImagePath( rs.getString("path"));
        }

        pstmt.close();
        rs.close();
        conn.close();
        return path;
    } catch( SQLException e ) {
        System.out.println("Cant get it");
    } finally {
        if( conn != null ) { try { conn.close(); } catch( SQLException e) {}};
    }
    return path;
}

这是我嵌入 ImageDao.java 的控制台上显示的消息: 首次访问:

8: org.postgresql.jdbc4.Jdbc4Connection@3d3cdc3b
8: upload/download.jpg 8
Id: 8 path: upload/download.jpg

第二次访问:

8: org.postgresql.jdbc4.Jdbc4Connection@3d3cdc3b
Cant get it
Id: 0 path: null

【问题讨论】:

  • 是不是因为你在单例创建中创建了连接,然后在查询之后你关闭它并且不再创建它?
  • 你知道我在哪里可以了解更多关于并发和线程安全技术的信息吗?
  • 有很多关于它的好书(这本是一种参考 - stackoverflow.com/questions/10202768/…),其中一些在你买二手的时候真的很便宜。但具体在这种情况下,我建议你使用连接池来获取和重用数据库连接,然后你的代码就可以正常运行了。
  • 不客气。欢迎来到堆栈溢出 ;-)
  • 如果您使用的是单例数据库连接,请不要在事务后关闭它。当服务器关闭或终止时关闭它。你可以用 servletcontext 来实现这一点

标签: java postgresql jsp


【解决方案1】:

您只在单例构造函数中创建了一次连接,并在第一个查询结束时将其关闭。

然后,在第二个查询中,连接关闭,您无法执行查询。

改为在查询之前创建连接。

【讨论】:

    猜你喜欢
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    相关资源
    最近更新 更多