Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help confused by dates!

Status
Not open for further replies.

stressball

Programmer
Mar 14, 2001
68
AU
I need to write a Class to do the following:

Get a date from a sybase stored procedure via a resultset
Change that date to a java.sql.Date
Collect the rest of the data
Send the date back via another sybase stored procedure as a sybase datetime datatype
I may also need to use methods from the java.util.Date package before the date goes back to sybase.
I have tried all sorts of things but I think I am just going about it completely the wrong way. Any suggestions would be most appreciated.
 
If you are using ResultSet and Statement then it is easy. You can use the getObject(int index) method of the ResultSet and cast it to a java.sql.Timestamp. Then to set it in the Statement object you can use setObject(int index, Object value) method. JDBC will do the proper formatting of sql for you. You set values locations in your JDBC SQL string with the ? character. So an index of 1 would refer to the first ? in the JDBC SQL string. Another thing to remember if you want to set a value to null you must use setNull(int index,int sqlType). You find the appropreate sqlType in java.sql.Types. An example of each follows:

getObject:
Code:
ResultSet rs;
/* Do all your ResultSet stuff */
java.sql.Timestamp myDate = null;
Object o;
o = rs.getObject(1);
if (!rs.wasNull())
  myDate = (java.sql.Timestamp)o;

setObject:
Code:
Statement stmt;
/* Do all you Statement stuff */
java.sql.Timestamp myDate = (java.sql.Timestamp)new java.util.Date().getTime();
stmt.setObject(1,myDate);

I know the examples are brief but I hope they help. Wushutwist
 
Hi,

You will have to use Callablestatement in this case since you want to call stored procedures.

The statement will be something like this:-

CallableStatement cstmt = myConnection.prepareCall("{CALL abc.cde(?,?,?,?)}");
cstmt.setString(1, "a");
cstmt.setString(2, "b");
cstmt.setString(3, "c");
cstmt.setString(4, "d");

- myConnection is a Connection object
- abc is the package name
- cde is the procedure name
- this procedure accepts 4 parameters and returns nothing so there are 4 '?'

If you have any other problems with date etc, feel free to ask.

Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top