简体   繁体   English

Java Spring JDBC last_insert_id()

[英]Java Spring JDBC last_insert_id()

I have a web service and a database(MySQL), I made a table called batch which must generate each time I add some value, an unique key. 我有一个Web服务和一个数据库(MySQL),我创建了一个名为batch的表,每次添加一些值(唯一键)时,该表必须生成。

CREATE TABLE batch (
    id INT AUTO_INCREMENT PRIMARY KEY,
    description VARCHAR(250) NOT NULL
);

This is the code for the database table and here you are what I wrote so far: 这是数据库表的代码,这就是我到目前为止编写的内容:

static final String addBatchIdSql = "INSERT INTO batch" + "(description)" + "VALUES (?)";
static final String getBatchIdSql = "SELECT LAST_INSERT_ID()";

    @Override
        public int getBatchId() {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String batchIDcreation = dateFormat.format(new Date());
            jdbcTemplate.update(addBatchIdSql, batchIDcreation);
            SqlRowSet rowSet = jdbcTemplate.queryForRowSet(getBatchIdSql);
            while (rowSet.next()) {
                System.out.println(rowSet.findColumn("id"));
            }
            return 0;
        }

The problem is that each time I try to get the id it throws exception which states "invalid column name", I have checked the naming properties in my application in general and could not find any issue, what could be wrong here :// 问题是,每次尝试获取ID时,它都会引发异常,指出“无效的列名”,我通常检查了应用程序中的命名属性,但未发现任何问题,这里可能出了问题://

You can try aliasing the column which is returned from your application's call to LAST_INSERT_ID() : 您可以尝试将应用程序调用LAST_INSERT_ID()返回的列作为别名:

static final String getBatchIdSql = "SELECT LAST_INSERT_ID() AS id";

Full code: 完整代码:

static final String addBatchIdSql = "INSERT INTO batch" + "(description)" + "VALUES (?)";
static final String getBatchIdSql = "SELECT LAST_INSERT_ID() AS id";

@Override
public int getBatchId() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String batchIDcreation = dateFormat.format(new Date());
    jdbcTemplate.update(addBatchIdSql, batchIDcreation);
    SqlRowSet rowSet = jdbcTemplate.queryForRowSet(getBatchIdSql);
    if (rowSet.next()) {
        System.out.println(rowSet.findColumn("id"));
    }
    return 0;
}

Try this because, your id as auto increment, so last insert id is your max id 尝试执行此操作,因为您的ID为自动递增,因此最后一个插入ID为您的最大ID

 static final String getBatchIdSql = "SELECT MAX(id) AS id FROM batch"; 

Total Code: 总代码:

static final String addBatchIdSql = "INSERT INTO batch" + "(description)" + "VALUES (?)";
 static final String getBatchIdSql = "SELECT MAX(id) AS id FROM batch";

                @Override
                public int getBatchId() {
                    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String batchIDcreation = dateFormat.format(new Date());
                    jdbcTemplate.update(addBatchIdSql, batchIDcreation);
                    SqlRowSet rowSet = jdbcTemplate.queryForRowSet(getBatchIdSql);
                    if (rowSet.next()) {
                        System.out.println(rowSet.findColumn("id"));
                    }
                    return 0;


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring jdbctemplate为last_insert_id()返回0 - spring jdbctemplate returns 0 for last_insert_id() 获取 mysql(innodb) AUTO_INCREMENT Column、JDBC/getGeneratedKeys()/last_insert_id (in OkPacket) 和 LAST_INSERT_ID() 的方法 - Ways to get mysql(innodb) AUTO_INCREMENT Column, JDBC/getGeneratedKeys()/last_insert_id (in OkPacket) and LAST_INSERT_ID() SELECT LAST_INSERT_ID() - SELECT LAST_INSERT_ID() 春季批处理MySql序列“无法获取last_insert_id()”以保护引擎类型 - Spring batch MySql Sequence “Could not obtain last_insert_id()” reguarding engine type 如何使用prepareStatement插入last_insert_id? - How do I insert last_insert_id using preparedStatement? mysql last_insert_id()是否适用于连接池? - Does mysql last_insert_id() work for connection pool? MySQL last_insert_id()在正确的事务中返回0 - MySQL last_insert_id( ) returns 0 within the proper transaction INSERT Batch之后我可以用LAST_INSERT_ID()进行SELECT选择吗? - Can I SELECT with LAST_INSERT_ID() after INSERT Batch without any cares? MYSQL LAST_INSERT_ID无法正常工作。 我该如何解决? - MYSQL LAST_INSERT_ID not working as intended. How do I fix? 在 Apache Derby Embedded 上调用 last_insert_id() 时出现休眠异常 - Hibernate exception when last_insert_id() called on Apache Derby Embedded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM