Skip navigation
Currently Being Moderated

how to insert a Java object into MySQL via ColdFusion

Mar 4, 2012 8:45 AM

Hi everyone,

  I have searched everywhere on the Internet best I can trying to find the answer. Still no result yet. I did find some code for how to insert a Java object into MySQL using pure Java. It goes like this: first you make a connection with the MySQL database by using Connection object. Then you use PreparedStatement object to prepare the sql statement. Then you call its executeUpdate() method. Done! This is how it is done in Java. But I am trying to see if I can do this directly in ColdFusion. Say I have a Java object myJavaOjbect, which implemens Serializable interface already. I want to insert it into java_objects_t table, which contains a column with BLOB type called my_java_data. Then in ColdFusoin I try to do this:

 

<cfquery name="insertJavaObject" dataSource="#Application.ds#">

          INSERT INTO java_objects_t (my_java_data) values (#myJavaObject#)

</cfquery>

 

This query does not work because #myJavaObject# turn out to be a String object, because it automatically calls its toString() method.

Is there a way to insert a Java object into MySQL database table inside ColdFusion page? Or do I need to use pure Java to do this?

 

Thank you for helping, everyone!

 
Replies
  • Currently Being Moderated
    Mar 5, 2012 12:09 AM   in reply to ffcai

    Use CFQueryParam, as you should be already. Job done. Pow pow pow.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 7, 2012 1:52 PM   in reply to ffcai

    <cfinvokeargument name="theObject" value="his"/>

     

    You need pound signs around the variable name. Otherwise, you are just passing in the literal characters "his".

     

    <cfqueryPARAM value = "#his#" CFSQLType = 'CF_SQL_OTHER'>

     

    The correct type for MySQL blobs is usually CF_SQL_LONGVARBINARY or CF_SQL_BLOB.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 7, 2012 3:05 PM   in reply to ffcai

    Good to see you too :-)

    Also, #his# is a Java object. So using CF_SQL_LONGVARBINARY or CF_SQL_BLOB as the type does not work. The system reports can't convert to '[B' if I use CF_SQL_BLOB, and it reports data invalid error if I use SQL_LONGVARBINARY.

     

    Ohh.. I thought #his# contained an array of bytes, which is compatible with longvarbinary and blob. I  have never tried inserting an object with cfqueryparam.

     

     

    Obviously it treats the integer 1 as "1". Do you know a way to work around it?

     

    ResultSet should accept both a column name or index. You probably just need to add a javacast so it knows which method you want.  ie javacast("int", 1) or javacast("string", "columnName").  But do you need to use java for the query? It seems like you could just query the table and reconstruct the object from the bytes.

     

    Message was edited by: -==cfSearching==-

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 7, 2012 3:10 PM   in reply to ffcai

    Apparently there is ObjectLoad() and ObjectSave() in CF9. Learn something new every day...

    http://www.cfinsider.com/index.cfm/2010/5/4/Serializing--Deserializing -in-ColdFusion-9

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 7, 2012 3:30 PM   in reply to ffcai

    Well with all the new functions, I overlooked it too

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points