SQLServer date / timestamp problems ?

Hi,

I am having trouble reading a timestamp field from sql server. Java 1.3/SQLServer 7 SP1, ODBC:JDBC bridge

We can access other datatypes OK but get the error

[Microsoft][ODBC SQL Server Driver]Restricted data type attribute violation

when we try to read a timestamp or datetime value, using getTimestamp or getDate.

Can anyone explain this error ?

java.sql.Date myDate;

java.sql.Timestamp myTimestamp;

java.util.Calendar cal = java.util.Calendar.getInstance();

java.util.TimeZone tz = java.util.TimeZone.getTimeZone("GMT");

String d;

try{

myTimestamp = r.getTimestamp("fldDate", cal);

System.out.println("Got timestamp");

System.out.println("timestamp is "+myTimestamp.toString() );

}

catch( SQLException e) {

System.out.println("Timestamp failed"+e);

}

try{

myDate = r.getDate("fldDate");

d=myDate.toString();

System.out.println("Date :"+d);

}

catch( SQLException e) {

System.out.println("Date failed"+e);

}

[1105 byte] By [msnowdon] at [2007-9-26 1:38:11]
# 1

You can fix your problem with SQLServer timestamps simply making a minor change.

SQLServer timestamps are, in fact Binary big integers. So, for example if you have a Country table and you select it as:

Select

[CountryID],

[Name],

user_name([InsertUser]) AS InsertUser,

[InsertDate],

user_name([ModifyUser]) AS ModifyUser,

[ModifyDate],

ts

FromCountry

Order By [CountryID]

Then you will get a problem because Java timestamp are Dates types.To fix the problem, just add a Convert function, like this:

Select

[CountryID],

[Name],

user_name([InsertUser]) AS InsertUser,

[InsertDate],

user_name([ModifyUser]) AS ModifyUser,

[ModifyDate],

Convert(bigint, [ts]) AS ts

FromCountry

Order By [CountryID]

And finally, from your Java code, read this column as a BigInt.

RDC

rodolfodc at 2007-6-29 2:25:51 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...