rajeev pani..
rajeev pani..

Reputation: 5655

java.sql.SQLException: Column Index out of range, 0 < 1

I want to display all the images from database. I have written code but that is displaying error java.sql.SQLException: Column Index out of range, 0 < 1. below is the my database table

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

I want to display only images.below is my servlet code

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();
        }

    }

Upvotes: 8

Views: 28680

Answers (3)

user1675825
user1675825

Reputation:

the statement should be like this

while (resultSet.next())

resultSet.getBlob(1);

col index from 1 to ...

Upvotes: 3

svz
svz

Reputation: 4588

Blob getBlob(int columnIndex)
             throws SQLException
Retrieves the value of the designated column in the current row of this ResultSet object as a Blob object in the Java programming language.
Parameters:
columnIndex - the first column is 1, the second is 2, ...
Returns:
a Blob object representing the SQL BLOB value in the specified column

You're trying to access column by index 0, while enumeration starts with 1

Upvotes: 1

codingbiz
codingbiz

Reputation: 26396

Column Index should start from 1 and not 0

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

Parameters: columnIndex - the first column is 1, the second is 2, ...

Should be

resultSet.getBlob(1) //first column

Upvotes: 25

Related Questions