用java 写向两个表同时插入数据,第二个表的一个字段是取第一个表的ID值

刚开始我是这样写的
String sql = "INSERT INTO person ( name, pass ) VALUES ( 'balabala', '123456' )"; // 执行SQL语句 stmt = conn.createStatement(); // stmt=conn.prepareStatement(sql);stmt.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS); ResultSet rs = stmt.getGeneratedKeys(); if (rs.next()) { int id = rs.getInt(1); System.out.println ("生成记录的key为 :" + id); } System.out.println("插入数据成功");
这样就可以没问题,但是我改成下面就出现问题了
String sql = "insert into person (name, pass) values (?, ?)";conn = DB.getConnection();pstmt = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);ResultSet rs = pstmt.getGeneratedKeys(); pstmt.setString(1, person.getName());pstmt.setString(2, person.getPass());pstmt.executeUpdate();
编辑后出现问题:Exception in thread "main" java.lang.AbstractMethodError: com.microsoft.jdbc.sqlserver.SQLServerConnection.prepareStatement(Ljava/lang/String;I)Ljava/sql/PreparedStatement
换了sqljdbc_2.0、sqljdbc_3都没用
最新回答
小小

2024-05-04 07:00:23

String sql = "insert into person (name, pass) values (?, ?)";
conn = DB.getConnection();
pstmt = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);

pstmt.setString(1, person.getName());
pstmt.setString(2, person.getPass());
pstmt.executeUpdate();

ResultSet rs = pstmt.getGeneratedKeys();//放在后面
葫芦岛

2024-05-04 09:09:39

pc_id,screen_id哪个优先?以pc_id优先为例:
oracle用decode函数,sqlserver可以用case...when,给你个oracle的例子
create procedure add_table
is
begin
insert into wip_led_opration(operation_id,led_pc_id,led_screen_id)
select operation_id,led_pc_id, decode(led_pc_id,null,led_screen_id,null)
-- 逻辑:第三列led_screen_id,先判断led_pc_id是否为空,为空则用led_screen_id,非空即led_pc_id存在,则led_screen_id列位置留空
from fnd_led_pc ,fnd_led_screen ,fnd_operation
where fnd_operation.status=1
and fnd_led_screen.status=1
and fnd_led_pc.status=1
-- and fnd_operation.status=fnd_led_screen.status --这个条件是多余的,两个值都=1了
and fnd_led_screen.status=fnd_led_pc.status ;
end;
/* 另外提几点建议
1. 建议给三个表加上别名
2. 多余的连接条件我注释掉了,虽然对执行计划应该没有太大影响
3. SELECT后最好标识出源表,比如fnd_operation.operation_id,这样自己看着也清楚
*/ {[MIMIcall]]不错的软件