【发布时间】:2012-05-16 08:50:01
【问题描述】:
我正在使用纯 JDBC 和 DAO 层进行编程,因为我的 Tomcat 中的 Java 内存 上只有 61.38 MB (服务托管)。我在 MySQL 中有一个带有 AUTO_INCREMENT 列的表。当前的实现没有问题。但我想知道AUTO_INCREMENT 列中生成的值。
当前代码在插入新行的方法中如下所示。
public Integer store(MyBean bean) throws DAOException {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement("INSERT ...");
if (bean.getSomeProperty() != null) {
ps.setShort(1, bean.getSomeProperty());
} else {
ps.setNull(1, Types.SMALLINT);
}
/* Other fields */
int rows = ps.executeUpdate();
if (rows == 1) {
// RETURN the generated value
}
return null;
} catch (SQLException e) {
throw new DAOException(e);
} finally {
...
}
}
我在Hibernate中看到这是可能的,但是因为我的记忆力很小,所以不是一个可行的选择。
感谢您的帮助。
【问题讨论】: