我想显示数据库中的所有图像。我已经编写了代码,但是显示错误java.sql.SQLException:列索引超出范围,0

| application_name | varchar(45)  |
| application_id   | varchar(10)  |
| application_path | varchar(500) |
| application_icon | blob         |

我只想显示图像。下面是我的servlet代码

IconDownload.java
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setContentType("image/jpeg");
            PrintWriter out=response.getWriter();
            try {
                Connection connection= DBUtil.getConnection();
                PreparedStatement preparedStatement=connection.prepareStatement("select application_icon  from application_master");
                ResultSet resultSet=preparedStatement.executeQuery();
                System.out.println("resultSet"+resultSet);
                out.print("<h1>photo</h1>");
                while (resultSet.next()) {
                    out.print("<img width='200' height='200' src="+resultSet.getBlob(0)+ ">  </img>" );
}
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

最佳答案

列索引应从1开始而不是0

http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getBlob(int)

参数:columnIndex-第一列为1,第二列为2,...

应该

resultSet.getBlob(1) //first column

07-26 01:19