Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, March 20, 2012

Retrieving generated keys

I have the following scenario:
MyTable
id number entity(1, 1)
name nvarchar
sniplet of Java code:
String sql = "Insert Into MyTable (name) Values( 'MyName' )";
int updateCnt = stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();
The executeUpdate() method throws an java.lang.AbstractMethodError exception.
How do I get the value of "id" after running my insert statement?
The microsoft example goes through a lot of object instantiation with
prepared calls and all that I don't necessarily want to complicate the
previous code by doing all that. But if I do, could someone please help me
with the conversion? I always use the above approach.
Thanks.
The MS driver is a JDBC 2.0 implementation.
Statement.RETURN_GENERATED_KEYS is a JDBC 3.0 feature. Either use a
JDBC 3.0 driver or append SELECT SCOPE_IDENTITY() to all your INSERT
statements.
Alin,
The jTDS Project.
|||If youre using SQL Server why not do the insert through a stored
procedure?...this way you can use the Scope_Identity function to return
back the value of the newly inserted identity to Java..let me know if
you need some sample code...
|||Jimbo wrote:
> If youre using SQL Server why not do the insert through a stored
> procedure?...this way you can use the Scope_Identity function to
return
> back the value of the newly inserted identity to Java..let me know if
> you need some sample code...
You can use scope_identity() without a stored procedure. Just execute
something like "INSERT ... SELECT scope_identity()". The problem is
that the JDBC code won't work across databases.
Alin.

Tuesday, February 21, 2012

retrieve Datetime

I have a table with a column tradedate of type datetime in which date
is stored in the format
mm/dd/yyyy hh:mm:sssAM. How do I retrieve this using Java resultset
I tried data, time and timestamp but I do not seem to get the exact
format. Any ideas?
Thanks a lot!
No, your date is NOT stored in that format. If the column is defined as
datetime, it uses an internal format that you never see. The way it gets
displayed is determined by a number of factors, including your client
settings. If you want date to be displayed a certain way, use the CONVERT
function, and specify a format code, which is documented along with the
CONVERT function in the Books Online.
For full details on datetime storage, display and manipulation, please see:
http://www.karaszi.com/sqlserver/info_datetime.asp
HTH
Kalen Delaney, SQL Server MVP
http://sqlblog.com
"db-x" <rashmi.ndeshpande@.gmail.com> wrote in message
news:1165512612.451830.77090@.79g2000cws.googlegrou ps.com...
>I have a table with a column tradedate of type datetime in which date
> is stored in the format
> mm/dd/yyyy hh:mm:sssAM. How do I retrieve this using Java resultset
> I tried data, time and timestamp but I do not seem to get the exact
> format. Any ideas?
> Thanks a lot!
>
|||I had to use
convert(char(25),trade_date,131)
That was very helpful. Thank you!
Kalen Delaney wrote:[vbcol=seagreen]
> No, your date is NOT stored in that format. If the column is defined as
> datetime, it uses an internal format that you never see. The way it gets
> displayed is determined by a number of factors, including your client
> settings. If you want date to be displayed a certain way, use the CONVERT
> function, and specify a format code, which is documented along with the
> CONVERT function in the Books Online.
> For full details on datetime storage, display and manipulation, please see:
> http://www.karaszi.com/sqlserver/info_datetime.asp
>
> --
> HTH
> Kalen Delaney, SQL Server MVP
> http://sqlblog.com
>
> "db-x" <rashmi.ndeshpande@.gmail.com> wrote in message
> news:1165512612.451830.77090@.79g2000cws.googlegrou ps.com...

retrieve Datetime

I have a table with a column tradedate of type datetime in which date
is stored in the format
mm/dd/yyyy hh:mm:sssAM. How do I retrieve this using Java resultset
I tried data, time and timestamp but I do not seem to get the exact
format. Any ideas?
Thanks a lot!No, your date is NOT stored in that format. If the column is defined as
datetime, it uses an internal format that you never see. The way it gets
displayed is determined by a number of factors, including your client
settings. If you want date to be displayed a certain way, use the CONVERT
function, and specify a format code, which is documented along with the
CONVERT function in the Books Online.
For full details on datetime storage, display and manipulation, please see:
http://www.karaszi.com/sqlserver/info_datetime.asp
HTH
Kalen Delaney, SQL Server MVP
http://sqlblog.com
"db-x" <rashmi.ndeshpande@.gmail.com> wrote in message
news:1165512612.451830.77090@.79g2000cws.googlegroups.com...
>I have a table with a column tradedate of type datetime in which date
> is stored in the format
> mm/dd/yyyy hh:mm:sssAM. How do I retrieve this using Java resultset
> I tried data, time and timestamp but I do not seem to get the exact
> format. Any ideas?
> Thanks a lot!
>|||I had to use
convert(char(25),trade_date,131)
That was very helpful. Thank you!
Kalen Delaney wrote:[vbcol=seagreen]
> No, your date is NOT stored in that format. If the column is defined as
> datetime, it uses an internal format that you never see. The way it gets
> displayed is determined by a number of factors, including your client
> settings. If you want date to be displayed a certain way, use the CONVERT
> function, and specify a format code, which is documented along with the
> CONVERT function in the Books Online.
> For full details on datetime storage, display and manipulation, please see
:
> http://www.karaszi.com/sqlserver/info_datetime.asp
>
> --
> HTH
> Kalen Delaney, SQL Server MVP
> http://sqlblog.com
>
> "db-x" <rashmi.ndeshpande@.gmail.com> wrote in message
> news:1165512612.451830.77090@.79g2000cws.googlegroups.com...