Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...