java如何往子表插入数据

数据库是sqlserver2000,有两个表,table1,table2是子表。table1主键为ID(为标识递增的),table外键是ID,用java插入数据,怎么插入... 数据库是sql server 2000,有两个表,table1,table2是子表。table1主键为ID(为标识递增的),table外键是ID,用java插入数据,怎么插入。table1插入ID略过,自动会赋值,但是table2如何插入数据,ID怎么来?具体代码怎么写?发现没有财富值了,哪位热心帮帮忙吧。
再清楚说下用java写string sql=insert table2 (ID,x,x) values (?,"",""),这个问号的值怎么来。
展开
 我来答
bd9006
2012-04-22 · TA获得超过2.5万个赞
知道大有可为答主
回答量:4.8万
采纳率:63%
帮助的人:1.7亿
展开全部
不考虑兼容问题,SQL Server 2000有语句

IDENT_CURRENT
Returns the last identity value generated for a specified table in any session and any scope.

Syntax
IDENT_CURRENT('table_name')

Arguments
table_name

Is the name of the table whose identity value will be returned. table_name is varchar, with no default.

Return Types
sql_variant

Remarks
IDENT_CURRENT is similar to the Microsoft® SQL Server™ 2000 identity functions SCOPE_IDENTITY and @@IDENTITY. All three functions return last-generated identity values. However, the scope and session on which 'last' is defined in each of these functions differ.

IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.

@@IDENTITY returns the last identity value generated for any table in the current session, across all scopes.

SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.
Examples
This example illustrates the different identity values returned by IDENT_CURRENT, @@IDENTITY, and SCOPE_IDENTITY.

USE pubs
DROP TABLE t6
DROP TABLE t7
GO
CREATE TABLE t6(id int IDENTITY)
CREATE TABLE t7(id int IDENTITY(100,1))
GO
CREATE TRIGGER t6ins ON t6 FOR INSERT
AS
BEGIN
INSERT t7 DEFAULT VALUES
END
GO
--end of trigger definition

SELECT * FROM t6
--id is empty.

SELECT * FROM t7
--id is empty.

--Do the following in Session 1
INSERT t6 DEFAULT VALUES
SELECT @@IDENTITY
/*Returns the value 100, which was inserted by the trigger.*/

SELECT SCOPE_IDENTITY()
/* Returns the value 1, which was inserted by the
INSERT stmt 2 statements before this query.*/

SELECT IDENT_CURRENT('t7')
/* Returns value inserted into t7, i.e. in the trigger.*/

SELECT IDENT_CURRENT('t6')
/* Returns value inserted into t6, which was the INSERT statement 4 stmts before this query.*/

-- Do the following in Session 2
SELECT @@IDENTITY
/* Returns NULL since there has been no INSERT action
so far in this session.*/

SELECT SCOPE_IDENTITY()
/* Returns NULL since there has been no INSERT action
so far in this scope in this session.*/

SELECT IDENT_CURRENT('t7')
/* Returns the last value inserted into t7.*/

See Also

@@IDENTITY

SCOPE_IDENTITY

如果是Hibernate,则另说。
yuyouchun241
2012-04-25 · TA获得超过221个赞
知道小有建树答主
回答量:185
采纳率:0%
帮助的人:115万
展开全部
string sql=insert table2 (ID,x,x) values (?,"",""),

Connection conn = DbUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1,"ddd");//添加字符串的话 他会自动加 ‘ddd’的
stmt.setInt(1,22222);//也可以添加整数。
还可以添加其他的。。。。

ResultSet rs = stmt.executeQuery();

如果对你有帮助的话希望采纳。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式