使用Java从数据库到文件的多个图像检索



我一直在尝试从我的表中检索所有图像,image第二列作为image.代码看起来不错,但它会产生错误invalid column index。请帮忙。

package p1;
import java.sql.*;  
import java.io.*;  
public class test 
{  
public static void main(String[] args) 
{  
try
{  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","admin");       
PreparedStatement ps=con.prepareStatement("select image from imagetable");  
ResultSet rs=ps.executeQuery(); 
int i=0; 
while(rs.next())
{           
Blob b=rs.getBlob(2);  
byte barr[]=b.getBytes(1,(int)b.length());       
FileOutputStream fout=new FileOutputStream("d:\img"+i+".png"); i++; 
fout.write(barr);               
fout.close();  
}//end of while  
System.out.println("ok");                
con.close();  
}
catch (Exception e)    
{
e.printStackTrace(); 
}  
}  
}  

尝试

Blob b=rs.getBlob("image"); 

而不是

Blob b=rs.getBlob(2);

异常的原因是您正在检索单个值,这就是列索引无效的原因。

最新更新