0

I'm using the following code to upload files into database the code is working fine with images where as for other files it is not working i'm posting stack trace at last.

My second value is a blob

   String strFilePath = null;
   Hashtable<Object,Object> fileTable = null; 
   InputStream input = null; 
   CosUploadFile file = null;

fileTable = multiPartFormData.getFiles();
file = (CosUploadFile)fileTable.get("filepath");
input =file.getInpuStream();

prepare = connection.prepareStatement("insert into all_files values(?,?,?)");
prepare.setString(1, strFileSplit[0]);
prepare.setBinaryStream(2,input);
prepare.setString(3,strFileSplit[1]);
prepare.execute();

Error :

J2CA0206W: A connection error occurred.  To help determine the problem, enable the Diagnose Connection Usage option on the Connection Factory or Data Source.
J2CA0056I: The Connection Manager received a fatal connection error from the Resource Adapter for resource datasource. The exception is: java.sql.SQLRecoverableException: Io exception: Connection reset by peer: socket write error:java.net.SocketException: Connection reset by peer: socket write error
     com.ibm.websphere.ce.cm.StaleConnectionException: Io exception: Connection reset by peer: socket write error

this is the stack trace while i'm trying to upload a doc file. What should I do Regarding this.

Edit: the following is my connection code

DBConnect dbConnect = new DBConnect();
Connection connection = dbConnect.connect();

DbConnect Class

  public Connection connect() 
    {   
    Connection con = null;
    try
    {
    InitialContext context = new InitialContext();
    DataSource datasource = (DataSource)context.lookup("datasource");
    con = datasource.getConnection("TRAIN2012", "xyz123");
    return con;
    }
sagar
  • 112
  • 9
  • Are you trying to connect to Oracel database? Else what database? – bonCodigo Nov 19 '12 at 10:41
  • 1
    It shouldn't make a difference if you are storing images or _other files_. Please test your code for one file at once only. – Kai Nov 19 '12 at 10:45
  • If I remember correct inserting BLOBs in 10g is a bit more complicated. You first have to insert a row and then selecting it for update. Now you can insert your BLOB. You should google for that. – Kai Nov 19 '12 at 10:48
  • I've just tried updating insert went well with null but same error is repeated with updation @user714965 . – sagar Nov 19 '12 at 10:54
  • @sagar have you tried this for one file only? – Kai Nov 19 '12 at 10:57
  • ... does one work? The problem could be before or after the code that you have posted. I can't see there any connection handling code. – Kai Nov 19 '12 at 11:13

1 Answers1

1

Try with the following, however it is always better to store images in file system and store the location of the file in database. Lengthy discussions about this can be found here.

FileInputStream fis = null;

File image = new File("\\yourpath\test.PNG");
fis = new FileInputStream(image);
prepare.setBinaryStream(2,fis,(int)image.length());
prepare.execute();
Community
  • 1
  • 1
Jacob
  • 14,463
  • 65
  • 207
  • 320
  • I'm using a web application and I cannot get the client side address so I'm unable to use it – sagar Nov 19 '12 at 11:02
  • @sagar What you mean by client side address? Could you elaborate more? – Jacob Nov 19 '12 at 11:03
  • users local system as it won't be allowed in any new browsers – sagar Nov 19 '12 at 11:25
  • @sagar Check what is getting printed in `strFilePath`. And for testing try to upload one file at a time and see how it works. – Jacob Nov 19 '12 at 11:35
  • @sagar So try uploading with one file from your path mentioned using the code I have posted and see whether it is working or not. – Jacob Nov 20 '12 at 13:11