• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem conversion to String , java.sql.Date, java.util.Date  RSS feed

 
bilal haider
Ranch Hand
Posts: 47
Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to do it in java. Its more than an hour i am still unlucky to find any solution.





in the output i get

DOB : oracle.sql.TIMESTAMP@164b9b6

which is not the desired output.

I want to convert it into string representation and java.util.Date as well.

like

if( value instanceof oracle.sql.TIMESTAMP)
-- conversion to String , java.sql.Date, java.util.Date
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you provided does not produce the output you posted.

However, if the column is of SQL type DATETIME (or DATE or TIME)


If the column is not a date or time type, then you'll have to do some work to interpret the value as appropriate.

Note that you don't need to do println(X.toString()). Simply println(X) will have the same result, since println() calls toString() if the reference isn't null.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are displaying the Object type toString().

You need to cast and extract the timestamps contents if allowed.

WP
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:You are displaying the Object type toString().

You need to cast and extract the timestamps contents if allowed.

WP


No, casting will not help. Calling X.toString() will produce exactly the same results as calling ((Y)X).toString(). This is true of any overridden method--casting the reference to some other type doesn't change whose version of the method gets called.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bilal haider wrote:
I want to convert it into string representation and java.util.Date as well.


If you call getDate() or getTime() or getTimestamp(), then the returned object will be a subclass of java.util.Date, so it already IS-A java.util.Date, so there's no conversion to be done.

Calling its toString() method will give you a String representation of that object.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you should use a DateFormat to get more control over how to display that date.
 
bilal haider
Ranch Hand
Posts: 47
Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jeff:" The code you provided does not produce the output you posted."
Sorry, I didn't copy the output from console. I typed that in the post.



and i can't use getDate() or getTime() or getTimestamp() as i want to get object value and then i want to cast it?
How can I do that.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bilal haider wrote:
and i can't use getDate() or getTime() or getTimestamp() as i want to get object value and then i want to cast it?
How can I do that.



Why would you want to do that? What are you actually trying to accomplish?

According to the docs for getObject(), it looks like it should return you the type you already want, so all you have to do is cast it:


If that doesn't work, then you'll have to check what class is actually returned, and do some research into how that class exposes its state. If it's not one of the expected java.sql types, it will probably be a vendor-specific class.

Even if it does work, it seems a fragile approach.
 
bilal haider
Ranch Hand
Posts: 47
Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the time and suggestion.

Actulay I am trying to accomplish the below activity:

I am querying a table, selecting some values, like Customer(ID Number, DOB Timestamp(6), Email varchar, active boolean)

and i want to return the resultset via webservice which is not possible , because resultset is not serializable. so i decided to save this result set in the xml. but in xml value of dob is oracle timestamp. when it is saved in xml it is like

<DOB>
oracle.sql.TIMESTAMP@164b9b6
</DOB>

after converting the whole resultset into xml i converted this to string.
Webservice client receive this string and converts back to resultset.

Problem is that how to store null values in the xml?
how to store long and date values in the xml?

so that at client end i should not be converting the strings to respective data types again.

I hope i am clear now.
Result:
ID : 1234
DOB: 12- MAR-1992
Email: Null
Active:Null
XML i want to return from web service method
<Result>
<Customer>
<ID>1234</ID>
<DOB>12- MAR-1992</DOB>
<Email></Email>
<Active></Active>
<Result>
<Customer>
At client end:
i want to convert this xml to resultset again:
Result:
ID : 1234
DOB: 12- MAR-1992
Email: Null
Active:Null

Suppose there are more columns whose data type and names are not known to developer.
Is this the right approach? If not what is the right approach?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should read the data from a recordset using the right methods (such as getTimestamp()) and store them into a class which would be serializable. This class will travel over the webservice and will be used instead of the ResultSet in the rest of your code. You would therefore use a serializable representation of the date inside that class, such as a Calendar. (You'd therefore have to create a Calendar instance from java.sql.Timestamp.) If you do lots of date-time manipulations, you might also want to explore the JodaTime library, which offers excellent support for this kind of operations.

If you want to make your code generic, you should still somehow obtain the datatypes of the columns to be read and use the proper getter methods of the ResultSet interface to obtain them. This information can be read either using the ResultSet.getMetaData method or it could be provided by the caller of your generic method, since the caller should know what kind of data it is expecting to get. I'd strongly suggest the latter approach, or better, I'd suggest to reconsider whether all this generification of your code does not bring up more problems than it actually solves. For example, there are tools which create the classes corresponding to tables in your database schema, or you might want to use an ORM framework.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!