Returning generated keys to MySql using JDBC PreparedStatement

I program using regular JDBC a DAO because I only have 61.38 MB of Java Memory in my Tomcat (hosting service). I have a table with a column AUTO_INCREMENTin MySQL. The current implementation has no problems. But I would like to know the value that was created in the column AUTO_INCREMENT.

In a method that inserts a new line, the current code is as follows.

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 {
        ...
    }
}

I saw that this is possible in , but since I have little memory, this is not a feasible option. Hibernate

I appreciate the help.

+3
source share

All Articles