Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to save an object to MYSQL database as a blob.....

807569Jul 21 2006 — edited Jul 21 2006
I'm trying to store an object getting from a particular url to the database(MySQL) as a blob

Step i'm following is like this...
> I'm getting the object from the url which can be a pdf, image....
> I'm converting that in to the array of bytes....
> array of bytes are then converted to blob & putting into the database...

> in my database the datatype is blob.

I don't whether i'm following write method........

I'm just copying the code i'm using & the error i'm getting just below this

So if any body can help me in this i'll be so thankful.....


import java.net.*;
import java.io.*;
import java.sql.*;
import java.sql.Blob;
import java.io.OutputStream;

public class URLConndb implements java.io.Serializable {

public static void main(String[] args) {
// TODO code application logic here
try{
URL yahoo = new URL("http://172.16.15.100/amc");
// URL yahoo = new URL("http://google.co.in");
Object o = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
java.sql.PreparedStatement pstmt;
URLConnection yc = yahoo.openConnection();
System.out.println("Perfect upto this.....");
o = yc.getContent();
Blob b= null;

////////////////
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;

ObjectOutputStream out;
out = new ObjectOutputStream(bos) ;
out.writeObject(o);
out.close(); // Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
int len = b.setBytes(1,buf);

///////////////////////////////




if(b!= null)
{
System.out.println("object present");
System.out.println("Content type : "+yc.getContentType());
try{
System.out.println("insert try..");
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Driver Registered") ;
con = DriverManager.getConnection ("jdbc:mysql://localhost/gapi","root","adhina");
System.out.println("Connection Established........") ;
stmt = con.createStatement();

rs =stmt.executeQuery("select * from objdatabase");
int id=0;
//Inserting values into the database.
try {
System.out.println("insert try........");
pstmt=con.prepareStatement("insert into srchurl values(?)") ;

pstmt.setBlob(1,b);

pstmt.executeUpdate() ;
System.out.println("Udated ........");
}
catch(Exception e)
{
System.out.println("Excepiton : "+e);
}
}
catch(Exception e)
{
System.out.println("Excepiton : "+e);
}
}
else
System.out.println("not present");
}
catch(Exception e)
{
System.out.println("Excepiton : "+e);
}
}

}


Error i'm getting during runtime

Excepiton : java.io.NotSerializableException: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream

Could anybody help me
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 18 2006
Added on Jul 21 2006
2 comments
953 views