Blob using MySQL

Discussion in 'Java' started by Moerderin, Jul 30, 2004.

  1. Moerderin

    Moerderin Guest

    i've been doing ton's of reading these past couple of days about how
    to create a blob and this is about as far as i was able to get. (the
    code still doesn't work) I was wondering if any computer experts out
    there could help me figure out what my code is lacking or what kind
    of revisions it might need, thank you

    public static void main(String [] args) {
    int id = 1;
    byte [] data = new byte[255];
    int i = 0;
    Access test = new Access();
    for (i=0;i<255;i++)
    data = 'x';
    try {
    Blob blob = null;
    test.connect("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","bfaul","123");
    ResultSet rs = test.query("Select * from FrameData where
    ID="+id);

    rs.next();

    blob = rs.getBlob("RawData");
    PreparedStatement pstmt = test.dbConn.prepareStatement(
    "UPDATE FrameData SET RawData = ? WHERE ID = "+id);

    blob.setBytes(0,data);
    pstmt.setBlob(1, blob);

    pstmt.executeUpdate();
    } catch (Exception error) {
    error.printStackTrace();
    }
    }
     
    Moerderin, Jul 30, 2004
    #1
    1. Advertising

  2. Moerderin

    Sudsy Guest

    Moerderin wrote:
    > i've been doing ton's of reading these past couple of days about how
    > to create a blob and this is about as far as i was able to get. (the
    > code still doesn't work) I was wondering if any computer experts out
    > there could help me figure out what my code is lacking or what kind
    > of revisions it might need, thank you
    >
    > public static void main(String [] args) {
    > int id = 1;
    > byte [] data = new byte[255];
    > int i = 0;
    > Access test = new Access();
    > for (i=0;i<255;i++)
    > data = 'x';
    > try {
    > Blob blob = null;
    > test.connect("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","bfaul","123");
    > ResultSet rs = test.query("Select * from FrameData where
    > ID="+id);


    Add "FOR UPDATE" at the end of your SQL statement. Also, turn off
    auto-commit beforehand, manually commit, reset auto-commit after.
    For some sample code using Oracle, see the following:
    <http://www.sudsy.net/technology/clobs.html>
     
    Sudsy, Jul 30, 2004
    #2
    1. Advertising

  3. Moerderin

    Ike Guest

    google up (by searching groups) on all keywords in the following line:

    blob ike rxv java "ObjectInput/Output streams and byte[]"

    -Ike

    "Moerderin" <> wrote in message
    news:...
    > i've been doing ton's of reading these past couple of days about how
    > to create a blob and this is about as far as i was able to get. (the
    > code still doesn't work) I was wondering if any computer experts out
    > there could help me figure out what my code is lacking or what kind
    > of revisions it might need, thank you
    >
    > public static void main(String [] args) {
    > int id = 1;
    > byte [] data = new byte[255];
    > int i = 0;
    > Access test = new Access();
    > for (i=0;i<255;i++)
    > data = 'x';
    > try {
    > Blob blob = null;
    >

    test.connect("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","bfaul",
    "123");
    > ResultSet rs = test.query("Select * from FrameData where
    > ID="+id);
    >
    > rs.next();
    >
    > blob = rs.getBlob("RawData");
    > PreparedStatement pstmt = test.dbConn.prepareStatement(
    > "UPDATE FrameData SET RawData = ? WHERE ID = "+id);
    >
    > blob.setBytes(0,data);
    > pstmt.setBlob(1, blob);
    >
    > pstmt.executeUpdate();
    > } catch (Exception error) {
    > error.printStackTrace();
    > }
    > }
     
    Ike, Jul 30, 2004
    #3
  4. You did not say in what way the code fails, so my advice may not apply.
    Having said this: try setting the blob bytes in your PreparedStatement like
    this:

    pstmt.setBytes(index, buffer);

    HTH

    Alex Molochnikov
    Gestalt Corporation
    www.gestalt.com

    "Moerderin" <> wrote in message
    news:...
    > i've been doing ton's of reading these past couple of days about how
    > to create a blob and this is about as far as i was able to get. (the
    > code still doesn't work) I was wondering if any computer experts out
    > there could help me figure out what my code is lacking or what kind
    > of revisions it might need, thank you
    >
    > public static void main(String [] args) {
    > int id = 1;
    > byte [] data = new byte[255];
    > int i = 0;
    > Access test = new Access();
    > for (i=0;i<255;i++)
    > data = 'x';
    > try {
    > Blob blob = null;
    >

    test.connect("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","bfaul",
    "123");
    > ResultSet rs = test.query("Select * from FrameData where
    > ID="+id);
    >
    > rs.next();
    >
    > blob = rs.getBlob("RawData");
    > PreparedStatement pstmt = test.dbConn.prepareStatement(
    > "UPDATE FrameData SET RawData = ? WHERE ID = "+id);
    >
    > blob.setBytes(0,data);
    > pstmt.setBlob(1, blob);
    >
    > pstmt.executeUpdate();
    > } catch (Exception error) {
    > error.printStackTrace();
    > }
    > }
     
    Alexandr Molochnikov, Jul 30, 2004
    #4
  5. Moerderin

    Moerderin Guest

    "Alexandr Molochnikov" <NOBODY@NO_SPAM.com> wrote in message news:<%UhOc.138052$eO.126140@edtnps89>...
    > You did not say in what way the code fails, so my advice may not apply.
    > Having said this: try setting the blob bytes in your PreparedStatement like
    > this:
    >
    > pstmt.setBytes(index, buffer);
    >
    > HTH
    >
    > Alex Molochnikov
    > Gestalt Corporation
    > www.gestalt.com
    >
    > "Moerderin" <> wrote in message
    > news:...
    > > i've been doing ton's of reading these past couple of days about how
    > > to create a blob and this is about as far as i was able to get. (the
    > > code still doesn't work) I was wondering if any computer experts out
    > > there could help me figure out what my code is lacking or what kind
    > > of revisions it might need, thank you
    > >
    > > public static void main(String [] args) {
    > > int id = 1;
    > > byte [] data = new byte[255];
    > > int i = 0;
    > > Access test = new Access();
    > > for (i=0;i<255;i++)
    > > data = 'x';
    > > try {
    > > Blob blob = null;
    > >

    > test.connect("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","bfaul",
    > "123");
    > > ResultSet rs = test.query("Select * from FrameData where
    > > ID="+id);
    > >
    > > rs.next();
    > >


    // first line of interest
    > > blob = rs.getBlob("RawData");
    > > PreparedStatement pstmt = test.dbConn.prepareStatement(
    > > "UPDATE FrameData SET RawData = ? WHERE ID = "+id);
    > >


    // second line of interest
    > > blob.setBytes(0,data);
    > > pstmt.setBlob(1, blob);
    > >
    > > pstmt.executeUpdate();
    > > } catch (Exception error) {
    > > error.printStackTrace();
    > > }
    > > }


    crap, i forgot to tell you where my code fails. at the "first line of
    interest" no value is stored in the blob, it gets the value of "null",
    then at the second line of interest it throws a null pointer exception
    which makes sence because blob has a value of "null"
     
    Moerderin, Jul 30, 2004
    #5
  6. "Moerderin" <> wrote in message
    news:...
    > // first line of interest
    > > > blob = rs.getBlob("RawData");
    > > > PreparedStatement pstmt = test.dbConn.prepareStatement(
    > > > "UPDATE FrameData SET RawData = ? WHERE ID = "+id);
    > > >

    >
    > // second line of interest
    > > > blob.setBytes(0,data);
    > > > pstmt.setBlob(1, blob);
    > > >
    > > > pstmt.executeUpdate();
    > > > } catch (Exception error) {
    > > > error.printStackTrace();
    > > > }
    > > > }

    >
    > crap, i forgot to tell you where my code fails. at the "first line of
    > interest" no value is stored in the blob, it gets the value of "null",
    > then at the second line of interest it throws a null pointer exception
    > which makes sence because blob has a value of "null"


    So, are you saying that the blob comes as null when it is not supposed to
    be? Because if it is null _legitimately_, then your code should handle this
    condition separately:

    if (buffer != null)
    pstmt.setBytes(index, buffer);
    else
    pstmt.setNull(index, Types.BLOB);

    AM
     
    Alexandr Molochnikov, Jul 30, 2004
    #6
  7. Moerderin

    John Fereira Guest

    Sudsy <> wrote in
    news::

    > Moerderin wrote:
    >> i've been doing ton's of reading these past couple of days about how
    >> to create a blob and this is about as far as i was able to get. (the
    >> code still doesn't work) I was wondering if any computer experts out
    >> there could help me figure out what my code is lacking or what kind
    >> of revisions it might need, thank you
    >>
    >> public static void main(String [] args) {
    >> int id = 1;
    >> byte [] data = new byte[255];
    >> int i = 0;
    >> Access test = new Access();
    >> for (i=0;i<255;i++)
    >> data = 'x';
    >> try {
    >> Blob blob = null;
    >> test.connect("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdar
    >> s","bfaul","123"); ResultSet rs = test.query("Select * from
    >> FrameData where
    >> ID="+id);

    >
    > Add "FOR UPDATE" at the end of your SQL statement. Also, turn off
    > auto-commit beforehand, manually commit, reset auto-commit after.
    > For some sample code using Oracle, see the following:
    > <http://www.sudsy.net/technology/clobs.html>


    If you're using the default table types for MySQL the commit/rollback
    methods are just noops anyway.
     
    John Fereira, Jul 31, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markusek Peter

    save BLOB to file from MySQL using c#

    Markusek Peter, Feb 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    24,219
    chemacecilia
    Jun 9, 2007
  2. Roedy Green
    Replies:
    0
    Views:
    4,143
    Roedy Green
    Aug 23, 2003
  3. bobb
    Replies:
    0
    Views:
    358
  4. ASP.NET mySQL BLOB

    , Aug 27, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,027
    Cuyler
    Aug 28, 2006
  5. Pasha Aryana

    BLOB - classic ASP using MySql

    Pasha Aryana, Dec 27, 2007, in forum: ASP General
    Replies:
    2
    Views:
    214
    Anthony Jones
    Dec 29, 2007
Loading...

Share This Page