java.sql.SQLException:列索引超出范围,0 < 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20442194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.sql.SQLException: Column Index out of range, 0 < 1
提问by rajeev pani..
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
我想显示数据库中的所有图像。我已经编写了代码,但显示错误 java.sql.SQLException: Column Index out of range, 0 < 1. 下面是我的数据库表
| 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
我只想显示图像。下面是我的 servlet 代码
IconDownload.java
图标下载.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();
}
}
采纳答案by codingbiz
Column Index should start from 1 and not 0
列索引应该从 1 开始而不是 0
http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getBlob(int)
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, ...
参数: columnIndex - 第一列是 1,第二列是 2,...
Should be
应该
resultSet.getBlob(1) //first column
回答by svz
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
您正在尝试按索引 0 访问列,而枚举以 1 开头
回答by svz
the statement should be like this
声明应该是这样的
while (resultSet.next())
而 (resultSet.next())
resultSet.getBlob(1);
结果集.getBlob(1);
col index from 1 to ...
col 索引从 1 到 ...