Exception: java.lang.IllegalStateException: getWriter() has already been called for this response

Exception: java.lang.IllegalStateException: getWriter() has already been called for this response

View Answers

November 11, 2008 at 6:48 AM

Hi friend,


package javacode;

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.Blob;
import java.io.*;

class RetriveImage {
FileOutputStream image;
Connection con = null;
PreparedStatement pstmt = null;
Statement stmt= null;
ResultSet res = null;
StringBuffer query=null;
String filename = "javacode/rose.jpg";
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.10.211:3306/";;
String dbName = "amar";
String userName = "amar";
String password = "amar123";

public RetriveImage(){
try{
query=new StringBuffer("insert into Image(id,imgName,image) values (?,?,?)");
File file= new File(filename);
Class.forName(driverName);
con = DriverManager.getConnection(url+dbName,userName,password);
stmt=con.createStatement();
pstmt=con.prepareStatement(query.toString());
pstmt.setInt(1, 124);
pstmt.setString(2,"rose");
pstmt.setBinaryStream(3, new FileInputStream(filename),(int)file.length());
pstmt.executeUpdate();
System.out.println("Successfully display image from database .....");
ResultSet rs=stmt.executeQuery("select * from Image where image='image'");
if (rs.next()) {
Blob test=rs.getBlob("image");
InputStream x=test.getBinaryStream();
int size=x.available();
OutputStream out=new FileOutputStream("javacode/rose.jpg");
byte b[]= new byte[size];
x.read(b);
out.write(b);
}
}
catch(ClassNotFoundException cnfe){
System.out.println("ClassNotFoundException occured :"+cnfe);
}
catch(SQLException se){
System.out.println("SQLException occured :"+se);
}
catch(FileNotFoundException fe){
System.out.println("File Not Found Exception occured :"+fe);
}
catch(Exception e){
System.out.println("Exception occured :"+e);
}
finally{
try{
stmt.close();
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
public static void main(String args[]) throws IOException{
RetriveImage ri = new RetriveImage();
}
}
----------------------------------
Visit for more information.

http://www.roseindia.net/servlets/retreiveimage.shtml

Thanks.









Related Tutorials/Questions & Answers:

Ads