If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > java.sql.date with PreparedStatement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-24-03, 10:19
Leonets Leonets is offline
Registered User
 
Join Date: Oct 2003
Location: florence-italy
Posts: 9
Question java.sql.date with PreparedStatement

I am not aware of the concept 'typed parameters markers'

I am trying to execute this prepared statement:

select S0.MOD_ID AS MODULO_ID from MODULI S0
LEFT OUTER JOIN CARTA S1 ON (S1.CARTA_MOD_ID = S.MOD_ID)
WHERE (S1.DATA_RICEZIONE) >= ?

Then I use

pst.setDate(new java.sql.Date(ecc..))

and then

pst.execute();

I have this error:
Invalid conversion

I searched in this forum and I found that this QUERY gave the same error:
PreparedStatement stmt = conn.prepareStatement ("select ACI.NAME
FROM ACI WHERE ACI.NAME = Upper(?);");

and this was the suggested solution:
PreparedStatement stmt = conn.prepareStatement ("select ACI.NAME FROM ACI WHERE ACI.NAME = Upper(CAST(? AS VARCHAR(10)));");
I am trying to browse db2 docs but I've not found a similar thing for date:
CAST(? AS DATE)



the same SQL works if I don't use a PreparedStament like this

select S0.MOD_ID AS MODULO_ID from MODULI S0
LEFT OUTER JOIN CARTA S1 ON (S1.CARTA_MOD_ID = S.MOD_ID)
WHERE ( char(S1.DATA_RICEZIONE) >= '01-01-2001')

thks in advance for any tips !!

Leonardo
Reply With Quote
  #2 (permalink)  
Old 11-24-03, 10:48
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
You can use CAST(character-string AS DATE) so long as the character string is in the form of YYYY-MM-DD or MM-DD-YYYY (slashes or periods may also be used as separators). The manual does not show all possibilities of the CAST function.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 11-24-03, 11:32
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Re: java.sql.date with PreparedStatement

Quote:
Originally posted by Leonets
I am not aware of the concept 'typed parameters markers'

I am trying to execute this prepared statement:

select S0.MOD_ID AS MODULO_ID from MODULI S0
LEFT OUTER JOIN CARTA S1 ON (S1.CARTA_MOD_ID = S.MOD_ID)
WHERE (S1.DATA_RICEZIONE) >= ?

Then I use

pst.setDate(new java.sql.Date(ecc..))

and then

pst.execute();

I have this error:
Invalid conversion

Are you sure that CARTA.DATA_RICEZIONE has the DATE type? If it's a TIMESTAMP you'll have to use setTimestamp() method...
Reply With Quote
  #4 (permalink)  
Old 11-24-03, 11:41
Leonets Leonets is offline
Registered User
 
Join Date: Oct 2003
Location: florence-italy
Posts: 9
Re: java.sql.date with PreparedStatement

Quote:
Originally posted by n_i
Are you sure that CARTA.DATA_RICEZIONE has the DATE type? If it's a TIMESTAMP you'll have to use setTimestamp() method...
absolutely.

I checked ten times.

Data_Ricezione is a DATE

***********
describe table fidescarte.carta


Nome Schema Nome

colonna tipo tipo Lungh. Scala Nulli

------------------------------ --------- ------------------ -------- ----- ------

ID SYSIBM INTEGER 4 0 No
CARTA_MOD_ID SYSIBM INTEGER 4 0 No
DATA_INS SYSIBM TIMESTAMP 10 0 No
DATA_MOD SYSIBM TIMESTAMP 10 0 No
NUM_MODULI SYSIBM INTEGER 4 0 Sė
NUM_DOCUMENTI SYSIBM INTEGER 4 0 No
DATA_INVIO SYSIBM DATE 4 0 No
DATA_RICEZIONE SYSIBM DATE 4 0 No
NOTE SYSIBM VARCHAR 250 0 Sė

8 record selezionato/i.
***********
Reply With Quote
  #5 (permalink)  
Old 11-24-03, 11:48
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
I don't know much about Java, but any date that is used in a SQL statement (unless referenced directly as a DB2 table column) must be in character string format of length 10. The internal representation of date stored in DB2 is 4 bytes (2 numeric digits per byte), but you cannot access it that way in a SQL statement.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #6 (permalink)  
Old 11-24-03, 12:37
Leonets Leonets is offline
Registered User
 
Join Date: Oct 2003
Location: florence-italy
Posts: 9
Quote:
Originally posted by Marcus_A
I don't know much about Java, but any date that is used in a SQL statement (unless referenced directly as a DB2 table column) must be in character string format of length 10. The internal representation of date stored in DB2 is 4 bytes (2 numeric digits per byte), but you cannot access it that way in a SQL statement.

1)

If my SQL is
VARCHAR(S0.MOD_DATA_COMPILAZIONE) >= CAST(? as VARCHAR(10))
and my JAVA executes
pst.setString(1,'01-01-2001')
I have this error
CLI0112E Errore nell'assegnazione. SQLSTATE=22005


2)

If my SQL is
VARCHAR(S0.MOD_DATA_COMPILAZIONE) >= CAST(? as VARCHAR(10))
and my JAVA executes
pst.setDate(1, new java.sql.Date(System.currentTimemillis() ))
I have this error
CLI0102E Conversione non valida. SQLSTATE=07006


3)

If my SQL is
VARCHAR(S0.MOD_DATA_COMPILAZIONE) >= CAST(? as DATE)
and my JAVA executes
pst.setDate(1, new java.sql.Date(System.currentTimemillis() ))
I have this error
CLI0102E Conversione non valida. SQLSTATE=07006

4)

If my SQL is
VARCHAR(S0.MOD_DATA_COMPILAZIONE) >= CAST(? as DATE)
and my JAVA executes
pst.setString(1,'01-01-2001')
I have this error
CLI0102E Conversione non valida. SQLSTATE=07006

5)

If my SQL is
S0.MOD_DATA_COMPILAZIONE) >= CAST(? as DATE)
and my JAVA executes
pst.setString(1,'01-01-2001')
I have this error
CLI0102E Conversione non valida. SQLSTATE=07006

6)

If my SQL is
S0.MOD_DATA_COMPILAZIONE) >= CAST(? as DATE)
and my JAVA executes
pst.setDate(1, new java.sql.Date(System.currentTimemillis() ))
I have this error
CLI0102E Conversione non valida. SQLSTATE=07006
Reply With Quote
  #7 (permalink)  
Old 11-24-03, 13:28
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally posted by Marcus_A
I don't know much about Java, but any date that is used in a SQL statement (unless referenced directly as a DB2 table column) must be in character string format of length 10. The internal representation of date stored in DB2 is 4 bytes (2 numeric digits per byte), but you cannot access it that way in a SQL statement.
The method setDate() should work for a DATE comparison. I suspect there may be something wrong with the driver stack; e.g. db2 client having fixpack different from the server; or wrong JDBC driver; or something like that.
Reply With Quote
  #8 (permalink)  
Old 11-25-03, 05:02
Leonets Leonets is offline
Registered User
 
Join Date: Oct 2003
Location: florence-italy
Posts: 9
Quote:
Originally posted by n_i
The method setDate() should work for a DATE comparison. I suspect there may be something wrong with the driver stack; e.g. db2 client having fixpack different from the server; or wrong JDBC driver; or something like that.
both db2client and server have:

db2 8.1.2.169

and I am using

sqllib\java\db2java.zip as driver
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On