This discussion is archived
9 Replies Latest reply: Sep 18, 2012 2:47 AM by EJP RSS

How to Convert Java String variable to oracle RAW ?

Ajay Sharma Newbie
Currently Being Moderated
Hi..

I need to convert a string into oracle RAW data type.

How to do it ?



Regards,
Ajay
  • 1. Re: How to Convert Java String variable to oracle RAW ?
    EJP Guru
    Currently Being Moderated
    In what context? PreparedStatement.setObject() should do it for you in an appropriate PreparedStatement.
  • 2. Re: How to Convert Java String variable to oracle RAW ?
    rp0428 Guru
    Currently Being Moderated
    >
    I need to convert a string into oracle RAW data type.

    How to do it ?
    >
    The same way you create an instance of any other class: use a constructor

    See the JDBC Java API Reference
    http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/sql/RAW.html
            String myString = "Convert this string to RAW";
            oracle.sql.RAW myRaw  = new oracle.sql.RAW(myString.getBytes());
  • 3. Re: How to Convert Java String variable to oracle RAW ?
    dsurber Explorer
    Currently Being Moderated
    Actually using the RAW constructor is not best practice. In general there isn't much need for RAW objects at all. Everything you can do with a RAW you can do equally well and in a Java standard way using byte[]. The driver handles RAWs by converting them to byte[]s.

    Across the board, if there is a way to do something using standard Java and standard JDBC, that's the right way to do it. Only use proprietary Oracle features if there is no standard way to do the same thing. Many/most Oracle proprietary features in JDBC were added before standard support was available in Java. When standard support was added, the driver was modified to support the standard and the proprietary feature was reimplemented as a wrapper around the standard feature. In other cases the proprietary feature is just a historical leftover. RAW is such a case.
  • 4. Re: How to Convert Java String variable to oracle RAW ?
    rp0428 Guru
    Currently Being Moderated
    OP is the one with the issue or question. So unless you are responding directly to something I said you should be directing your response to OP if you have information that you think will help them.

    The only part of your comment that applies to what I said was this
    >
    Actually using the RAW constructor is not best practice.
    >
    And that statement is wrong. The RAW constructor IS best practice for constructing instances of oracle.sql.RAW which is what OP was asking.

    If you have a different method of constructing an instance of that class that you consider best pracice please post it here.

    All of your other comments were related to whether OP 'should be' using RAW, needs to use RAW or whether use of RAW is best practice. Those comments should be directed to OP.

    Assuming OP has a table with a RAW column that he want to populate then using an OraclePreparedStatement is appropriate and the API has an example of how to do that
    http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OraclePreparedStatement.html#setRAW_int__oracle_sql_RAW_
    For example, if you have a Connection object named conn and a RAW named my_raw, then write the RAW to the database as follows:
    
      PreparedStatement ps = conn.prepareStatement
      ("INSERT INTO raw_table VALUES(?)");
      ((OraclePreparedStatement)ps).setRAW(1, my_raw);
      ps.execute();
    That code above assumes that the 'my_raw' variable is an instance of oracle.sql.RAW and such as instance can be created as I showed in my reply.

    If you have a way to populate that same column 'using standard Java and standard JDBC' then please post that solution here for OP to consider and use.
  • 5. Re: How to Convert Java String variable to oracle RAW ?
    Ajay Sharma Newbie
    Currently Being Moderated
    Hi..

    I got following Exception when using this code
    String myString = "Convert this string to RAW";
            oracle.sql.RAW myRaw  = new oracle.sql.RAW(myString.getBytes());
    java.lang.UnsupportedClassVersionError: Bad version number in .class file
    Regards,
    Ajay

    Edited by: Ajay Sharma on Sep 18, 2012 2:07 PM
  • 6. Re: How to Convert Java String variable to oracle RAW ?
    Ajay Sharma Newbie
    Currently Being Moderated
    Hi..

    Scenario is that we are trying to perform Encryption Decryption on Database side. There we are generating KEY and its in RAW format.

    Some Encryption Decryption I am doing at Web Service side.

    At database side and at web service side we generating key from same String. but both values are different.



    Regards,
    Ajay
  • 7. Re: How to Convert Java String variable to oracle RAW ?
    EJP Guru
    Currently Being Moderated
    I got following Exception when using this code
    Did you get that before you posted? If so, you should have said so in the first place.
    java.lang.UnsupportedClassVersionError: Bad version number in .class file
    That simply means that you are executing say 1.7 classes on a 1.6 JVM, or 1.6 classes on a 1.5 JVM, etc. Either update your JVM or get an earlier version of whatever class is involved.
  • 8. Re: How to Convert Java String variable to oracle RAW ?
    Ajay Sharma Newbie
    Currently Being Moderated
    Hi.
    EJP Wrote: 
    
    Did you get that before you posted? If so, you should have said so in the first place.
    No, I didn't used this code before posting. I tested the code given by rp0428 and then I got an error.
    That simply means that you are executing say 1.7 classes on a 1.6 JVM, or 1.6 classes on a 1.5 JVM, etc. Either update your JVM or get an earlier version of whatever class is involved.
    I Created one class in jdevloper and there i got this error. In my Environment variable I already added jdk 1.6.
    Is it possible that even after adding jdk 1.6 I will get this error ?


    Regards,
    Ajay
  • 9. Re: How to Convert Java String variable to oracle RAW ?
    EJP Guru
    Currently Being Moderated
    If you are using a .class file or a JAR file that was compiled with a later JDK than the JVM you are using to execute, you will get this error.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points