UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Java
  • » Plz help me in solution for this error......

#1 2006-07-18 01:05 PM

surya_jc
Member
Registered: 2006-07-18
Posts: 8

Re: Plz help me in solution for this error......

I working in a project which retieve the contents from the url specified as a BLOB and has to put it into the MYSQL database..... but i'm getting the error

Exception in thread "main" java.lang.ClassCastException: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream
        at URLConndb.main(URLConndb.java:19)

This is the part of the program where i'm facing the issue....

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

public class URLConndb {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://172.16.10.125:8080/nbeii/pdf/Priyan%20abstract.pdf");
    //    URL yahoo = new URL("http://www.cis.ohio-state.edu:13/index.html#here ");
    //    Object o = null;
        Blob o;
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        java.sql.PreparedStatement pstmt;

        URLConnection yc = yahoo.openConnection();
        Object ob = yc.getContent();
        o =(Blob)ob;
        System.out.println("Perfect upto this.....");
//         o = yc.getContent();
         if(o!= null)
        {
            System.out.println("object present");
            System.out.println("Content type : "+yc.getContentType());

            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;
            while(rs.next())
            {
                id=rs.getInt("slno");
            }
            rs.close();
               
            id++;        //Got the last record.
            System.out.println("Got the last record.." + id);
            stmt.executeUpdate("insert into objdatabase (slno,content) values("+id+",'"+o+"')");

        }
        else
            System.out.println("not present");
    }
}

If somebody can help me I'll be so thankfull.....

Offline

#2 2006-07-21 10:27 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Plz help me in solution for this error......

You are attempting to directly to cast from a base Object to a Blob... and you really cannot do that...  simply because the object returned by a call to getContent( ) is NOT a Blob... and not a child from Blob...

Other notes...

Blobs normally take clusters / arrays of bytes etc. as their input... so whatever you get from getContent( ) has to be serialized ( after all - its a full blow object of the class type determined from the returned mime type ) and then that needs to be fed into the Blob...

Ah - but there is a potential problem... there is nothing that REQUIRES the object type returned by getContent( ) to be serializable... so um - how do you convert the darn thing into something which can be stored in an array?!

So an alternative to what you are doing might be to simply grab the input stream - feed it into a byte array - then stuff that into the Blob... and read out / instantiate via a factory as needed...

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2006-07-21 10:46 AM

surya_jc
Member
Registered: 2006-07-18
Posts: 8

Re: Plz help me in solution for this error......

Thanks for ur suggestion

also sorry for asking the simple questions.....
Bcoz i'm totally messed up....:mad:

I converted the object i got into array of bytes...

here is the code

[color="Blue"]

             ByteArrayOutputStream byteos = new ByteArrayOutputStream() ;
             ObjectOutputStream out;
             out = new ObjectOutputStream(byteos) ;   
             out.writeObject(o);   // o is the object i got by o= yc.getContent();
             out.close();     
             byte[] buf = bos.toByteArray();
[/color]

Now how to convert byte of array into blob.

Could u plz guide me... if possible giv me some example

Offline

#4 2006-07-21 01:01 PM

surya_jc
Member
Registered: 2006-07-18
Posts: 8

Re: Plz help me in solution for this error......

Thanks for ur valid suggestion...

I think i had done what u hav suggested me to do....
[color="Blue"]
    .           
    .
    .
    .
            Object o = null;
            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); [COLOR="SeaGreen"]//making it a blob[/color]
                     
            ///////////////////////////////
             
    .
    .
    .
          try    {
                        System.out.println("insert try........");
                        pstmt=con.prepareStatement("insert into srchurl values(?)") ;   
                       
                        pstmt.setBlob(1,b);
                       
                        pstmt.executeUpdate() ;
                        System.out.println("Udated ........");
                        }
.           
    .
    .
    .
[/COLOR]

But i'm getting the runtime error

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

Offline

#5 2006-07-21 07:38 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Plz help me in solution for this error......

As I had mentioned in the last message... there is nothing that says the object type returned by the getContent( ) call has to be serializable...

In fact, it is allowed to do things such as return just a stream type instead of the final object itself... when using a URLConnection for an HTTP URL thats exactly what you get... you can verify this by doing a print( ob )

You don't want to try and store a producer or stream... so... once you do getContent... verify that it is a input stream type ( isinstance etc. )... then read data into your byte array while the stream.available() != 0... take THAT byte array and store it...

If you do this then for a regular html page you should have read the start and stop <html> tags and everything in between... for a pdf you should get the binary file itself... and so forth...

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#6 2006-07-24 08:05 AM

surya_jc
Member
Registered: 2006-07-18
Posts: 8

Re: Plz help me in solution for this error......

[color="Indigo"]Thanks i checked what u have suggested....
> I'm getting the input stream
> & i'd converted into array of bytes
> now trying to put it into the database as blob

Here ... at this point i'm getting the exception [/color]

[color="Red"]java.sql.SQLException: Insert value list does not match column list,  message from server: "Column count doesn't match value count at row 1"[/color]

even though the datatype is set to Blob in the database.

Plz check for the hightlighted lines whether i'm doing any thing wrong....

[color="Blue"]import java.net.*;
import java.io.*;
import java.sql.*;
import java.sql.Blob;
import java.io.OutputStream;
import java.io.InputStream;

public class urlconndb1  {
   
    public urlconndb1() {}
   
    public static void main(String[] args) {
           try{
           // URL yahoo = new URL("http://172.16.15.100/amc");
            URL yahoo = new URL("http://172.16.10.125:8080/nbeii/pdf/B.Ravi%20Prasad%20Rao.pdf");
   
            InputStream in,in1 ;
                                     
            Connection con = null;
            Statement stmt = null;
            ResultSet rs = null;
            ResultSet rs1 = null;
            java.sql.PreparedStatement pstmt;
           
            URLConnection yc = yahoo.openConnection();
            System.out.println("Perfect upto this.....");
           
            in = yc.getInputStream();
           
            Blob b = null;
            int len1 = in.available();
            byte[] tobytes=new byte[len1];
                               
            if(len1!= 0)
            {
    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 where slno = "+1+"");
                   int id=0;
           while(rs.next())       
                   {//id++;        //Got the last record.
                       [COLOR="Blue"] int no = rs.getInt(1);
                        b = rs.getBlob(2);
                        System.out.println("slno : "+ no);[/color]
                   }
     
                    [color="Blue"]int len = b.setBytes(1,tobytes);[/color]                try    {
            System.out.println("insert try........ len : "+len );
            pstmt=con.prepareStatement("insert into srchurl values(?,?)") ;    //////17/7
                        pstmt.setInt(1,id);
           [color="blue"]pstmt.setBlob(2,b);[/color]                        pstmt.executeUpdate() ;
            System.out.println("Udated ........");
            }
            catch(Exception e)
            {
                            System.out.println("Excepiton  for pstmt : "+e);
            }


                    }
                    catch(Exception e)
                    {
                        System.out.println("Excepiton for database: "+e);
                    }
        }
        else
                    System.out.println("not present");
            }
            catch(Exception e){
        System.out.println("Excepiton for main: "+e);
    }
    }
   
}[/COLOR]

Output i'm getting

[color="DarkOrange"]Perfect upto this.....
object present
Content type : application/pdf
insert try..
Driver Registered
Connection Established........
slno : 1
insert try........ len : 13098
[COLOR="Red"]Excepiton  for pstmt : java.sql.SQLException: Insert value list does not match column list,  message from server: "Column count doesn't match value count at row 1"[/color]
BUILD SUCCESSFUL (total time: 0 seconds)[/COLOR]
Waiting for ur help....

Offline

#7 2006-07-24 08:52 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Plz help me in solution for this error......

Well...

I guess I will answer you on this forum... its my favorite... and my replies elsewhere don't seem to have detered you... lol

So you didn't provide a lot of information... so the following is a guess based on common errors I have seen...

Folks who see that particular error seem to focus on thinking that somehow they are 'missing' a column in the database...  and that is why they are having problems... but the truth is typically its the exact opposite situation...

To explain...

You have a database... you have a table... you have an int column... you have a blob column... you have it set up to generate a unique id for each row in the table... that last item is probably the issue ( do you have an unique id generated ? )...

The thing is that the automatic ( unique ) id is assigned to column 1... the int would be column 2... the blob would be column 3...  so explicit assignments like you have would cause a fault i.e. you are trying to set the id to an int of your own chosing when the db thinks that it is going to be performing the task... and the blob is being assigned to an int...!

So thats just a guess... but try changing the indexes by incrementing them by 1... see if the error clears up / vanishes...

Michael


P.S. Please please please... here and elsewhere... if there is a code block option, use it...  you are hurting my brain... ;-)


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#8 2006-07-24 12:44 PM

surya_jc
Member
Registered: 2006-07-18
Posts: 8

Re: Plz help me in solution for this error......

[color="DarkOrchid"]Thanks a lot .... that worked.......

Now i can store pdf files .... and retrive it also....

Thanks a lot for ur help.....[/color]

Offline

#9 2006-07-27 07:17 AM

surya_jc
Member
Registered: 2006-07-18
Posts: 8

Re: Plz help me in solution for this error......

[color="Blue"]Thanks a lot for ur suggestion....

Now i can download the pdf files from the intranet..... & can save it as blob in the database.... and can also retrieve it. No issues....

But when i do the same thing for a public documents for example

"http://www.ncbi.org.in/workshop/pdf/Workshop_on_IndOBIS.pdf"

i can save  it no issues....

But when i'm trying to retrieve it .....
It is giving the error .....
[COLOR="Red"]"There was an error opening this document. The file is damaged and could not be repaired..."[/color]
I'm not getting why i'm getting the error....

Could u plz help me....[/COLOR]

Offline

  • Index
  • » Java
  • » Plz help me in solution for this error......

Board footer

Powered by FluxBB