Java SQL Table getLastCreatedEntry(Connection conn, String tableName)

Here you can find the source of getLastCreatedEntry(Connection conn, String tableName)

Description

get Last Created Entry

License

LGPL

Declaration

public static int getLastCreatedEntry(Connection conn, String tableName) throws SQLException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.sql.*;

public class Main {
    public static int getLastCreatedEntry(Connection conn, String tableName) throws SQLException {
        int id = -1;
        String sql = "SELECT id FROM " + tableName + " ORDER BY id DESC LIMIT 1";
        Statement st = conn.createStatement();
        try {/*from  ww  w .  ja v  a2  s  . c  om*/
            ResultSet resultSet = st.executeQuery(sql);
            if (resultSet.next()) {
                id = resultSet.getInt("id");
            } else
                throw new SQLException("No results");
        } finally {
            st.close();
        }
        if (id >= 0) {
            return id;
        } else {
            throw new SQLException();
        }
    }
}

Related

  1. getAllTables(Connection conn)
  2. getBatchResultMessage(String tableName, int rowIdx, int resultCode)
  3. getCount(Connection conn, String tableName)
  4. getIdNumber(String tableName)
  5. getImmutableDefaults()
  6. getMetaDataMap(Statement statement, String tableOrViewName)
  7. getNextId(Connection con, String table, String identityFieldName)
  8. getRowCount(Statement stmt, String tableName)
  9. getSensorsList( final Connection jdbcConnection, String tableName)