JDBC ORACLE: 取得SQL总列数; 插入数据后 取得指定自增字段(主键)的值
(1)取得 查询SQL 总列数:
Connection conn;
PreparedStatement ps;
......
ps = conn.prepareStatement( sql );
ResultSet rs = ps.executeQuery();
int cols = rs.getMetaData().getColumnCount(); //总列数
(2)插入数据后,取得指定自增字段(主键)的值: ( 在ibtats中用到过此功能 )
String sql = " insert into table1 ( a, b ) values ('1','2' )";
PreparedStatement ps = null;
Connection conn = SpringContextUtils.getConnection();
String returnCol[] = {"res_id"}; // res_id为表中的主键,自增
ps = conn.prepareStatement( sql, returnCol );
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
if( rs.next() ) {
id = rs.getInt( 1 );
}
注意:有些JDBC驱动好像不支持 getGeneratedKeys。