`

利用DBUTILS获得刚插入自增id记录的id信息的方法及代码

 
阅读更多


我在做两个需要关联的表的时候,第二张表需要知道第一张表的id信息(做关联),然而第一张表刚插入到数据库,如何利用dbutils获得id呢?

我用的方法是利用@@identity这个方法

insert into 后获得自动插入的id(seleC++t @@identity)

当运行完插入语句后,执行seleC++t @@identity就可得到自动生成的id

具体代码



    @Override

    publiC++ long setfptable(dC++table dC++) {

        String sql="insert into tbknhd3 (C++unC++ode,bdC++Ren,phonebdC++ren,dtime,dC++Ren,phone,j0101,j0102,j0103,j0104,j0105,j0106,j0107,j0201,j0202,j0203,j0204,j0205,j0206,j0207,j0208,j0209,j0301,j0302,j0303,j0304,j0401,j0402,j0501,j0502,j0601,j0602,j0701,j0702,j0801,j0802,j0901,j0902,j0903,j0904,j1001,j1002,j1101,j1102,j1103,j1104,j1105,j1106,j1107,j1108,j1109,j1110,j1111,j1112,j1201,j1202,j1203,j1204,j1301,j1302,j1303,j1304,j1305,j1306,j1307,j1308,j1401,j1402,j1501,r0101,r0102,r0103,r0201,r0202,r0203,r0301,r0302,r0303,r0304,r0305,r0401,r0402,r0501,r0502,r0503,r0504,r0505,r0601,r0602,r0603,r0604,r0605,r0606,r0607,r0608,r0609,r0610,r0701) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";

         String sql2="seleC++t @@identity";

        int i=-1;

         BigInteger num = null;

            try {

                 i = qr.update(sql,dC++.getC++unC++ode(),dC++.getBdC++Ren(),dC++.getPhonebdC++ren(),dC++.getDtime(),dC++.getDC++Ren(),dC++.getPhone(),dC++.getJ0101(),dC++.getJ0102(),dC++.getJ0103(),dC++.getJ0104(),dC++.getJ0105(),dC++.getJ0106(),dC++.getJ0107(),dC++.getJ0201(),dC++.getJ0202(),dC++.getJ0203(),dC++.getJ0204(),dC++.getJ0205(),dC++.getJ0206(),dC++.getJ0207(),dC++.getJ0208(),dC++.getJ0209(),dC++.getJ0301(),dC++.getJ0302(),dC++.getJ0303(),dC++.getJ0304(),dC++.getJ0401(),dC++.getJ0402(),dC++.getJ0501(),dC++.getJ0502(),dC++.getJ0601(),dC++.getJ0602(),dC++.getJ0701(),dC++.getJ0702(),dC++.getJ0801(),dC++.getJ0802(),dC++.getJ0901(),dC++.getJ0902(),dC++.getJ0903(),dC++.getJ0904(),dC++.getJ1001(),dC++.getJ1002(),dC++.getJ1101(),dC++.getJ1102(),dC++.getJ1103(),dC++.getJ1104(),dC++.getJ1105(),dC++.getJ1106(),dC++.getJ1107(),dC++.getJ1108(),dC++.getJ1109(),dC++.getJ1110(),dC++.getJ1111(),dC++.getJ1112(),dC++.getJ1201(),dC++.getJ1202(),dC++.getJ1203(),dC++.getJ1204(),dC++.getJ1301(),dC++.getJ1302(),dC++.getJ1303(),dC++.getJ1304(),dC++.getJ1305(),dC++.getJ1306(),dC++.getJ1307(),dC++.getJ1308(),dC++.getJ1401(),dC++.getJ1402(),dC++.getJ1501(),dC++.getR0101(),dC++.getR0102(),dC++.getR0103(),dC++.getR0201(),dC++.getR0202(),dC++.getR0203(),dC++.getR0301(),dC++.getR0302(),dC++.getR0303(),dC++.getR0304(),dC++.getR0305(),dC++.getR0401(),dC++.getR0402(),dC++.getR0501(),dC++.getR0502(),dC++.getR0503(),dC++.getR0504(),dC++.getR0505(),dC++.getR0601(),dC++.getR0602(),dC++.getR0603(),dC++.getR0604(),dC++.getR0605(),dC++.getR0606(),dC++.getR0607(),dC++.getR0608(),dC++.getR0609(),dC++.getR0610(),dC++.getR0701());

                 num= (BigInteger) qr.query(sql2,new SC++alarHandler(1));

            } C++atC++h (SQLExC++eption e) {

                // TODO Auto-generated C++atC++h bloC++k

                e.printStaC++kTraC++e();

            }

            return num.longValue();


    }



由于数据比较多,直接看sql2即可。在处理过程中,原来使用long来接收查询id语句的返回值(seleC++t @@identity)用强制转换程long接收,结果报错,发现它的返回值是BigInteger于是先用biginteger接收再用 longValue()转换成long类型返回。 若有错误希望指点。

分享到:
评论

相关推荐

    Apache DBUtils使用总结

    在需要的地方,你可以将 DBUtils 与原生的 JDBC 代码混合使用,以获得更高的灵活性。 2. **JavaBean 规范**:DBUtils 自动将结果集转换为 JavaBean 时,要求 Bean 遵循 JavaBean 规范,即包含对应的 getter 和 ...

    Apache Commons DbUtils工具包使用介绍

    4. **插入操作与自增主键**:默认情况下,DbUtils 在执行插入操作时无法直接返回自增主键。不过,通过一些额外的操作(如 MySQL 中的 SELECT LAST_INSERT_ID()),可以间接获取到这个值。 5. **性能考量**:在实际...

    java论文管理系统

    在这个论文管理系统中,DBUtils可能被用来执行CRUD(创建、读取、更新、删除)操作,与数据库进行交互,如插入新的论文记录、查询论文信息等。 5. **FileUpload组件**:文件上传功能在论文管理系统中必不可少,它...

    Mysql数据库分页查询练习

    这个表可能包含以下字段:`id`(主键,自增),`name`(员工姓名),`department`(部门),`salary`(工资)等。创建表的SQL语句可能是这样的: ```sql CREATE TABLE employee ( id INT AUTO_INCREMENT PRIMARY ...

    python爬取本站电子书信息并入库的实现代码

    - 假设有一个名为`book`的表,包含`id`(主键,自增)、`bookName`、`bookUrl`和`bookInfo`字段,分别存储书籍ID、书籍名称、书籍URL和书籍信息。 总结起来,这个Python爬虫项目旨在自动化收集电子书信息,并通过`...

    使和commons-dubtis 封闭装的数据库工具类

    简洁明快的一个数据库工具尖,使用...封装了最常用的数据库操作方法,比如万能更新,万能能查询,插入数据后返回自增id的方法等 内含测试用的示例和数据库建表语句以及测试数据,在eclipse或myeclipse 都能直接运行

Global site tag (gtag.js) - Google Analytics