java.sql.SQLException:列索引超出范围,0 < 1
Posted
技术标签:
【中文标题】java.sql.SQLException:列索引超出范围,0 < 1【英文标题】:java.sql.SQLException: Column Index out of range, 0 < 1 【发布时间】:2013-12-24 21:13:17 【问题描述】:我想显示数据库中的所有图像。我已经编写了代码,但是显示错误 java.sql.SQLException: Column Index out of range, 0
| application_name | varchar(45) |
| application_id | varchar(10) |
| application_path | varchar(500) |
| application_icon | blob |
我只想显示图像。下面是我的 servlet 代码
图标下载.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】:列索引应该从 1 开始,而不是 0
http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getBlob(int)
参数:columnIndex - 第一列为1,第二列为2,...
应该是
resultSet.getBlob(1) //first column
【讨论】:
【参考方案2】:语句应该是这样的
while (resultSet.next())
resultSet.getBlob(1);
col 索引从 1 到 ...
【讨论】:
【参考方案3】: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
您尝试按索引 0 访问列,而枚举从 1 开始
【讨论】:
以上是关于java.sql.SQLException:列索引超出范围,0 < 1的主要内容,如果未能解决你的问题,请参考以下文章
获取 java.sql.SQLException:插入表时列索引无效