Java SQL Table getNextId(Connection con, String table, String identityFieldName)

Here you can find the source of getNextId(Connection con, String table, String identityFieldName)

Description

get Next Id

License

Apache License

Declaration

public static int getNextId(Connection con, String table, String identityFieldName) throws SQLException 

Method Source Code


//package com.java2s;
/*//from  ww w. j a va 2  s.  c  o  m
 * codjo.net
 *
 * Common Apache License 2.0
 */

import java.sql.Connection;
import java.sql.ResultSet;

import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    public static int getNextId(Connection con, String table, String identityFieldName) throws SQLException {
        Statement stmt = con.createStatement();
        try {
            ResultSet rs = stmt.executeQuery(
                    "select max(" + identityFieldName + ") as " + identityFieldName + " from " + table);
            try {
                if (rs.next()) {
                    return rs.getInt(1) + 1;
                } else {
                    return 1;
                }
            } finally {
                rs.close();
            }
        } finally {
            stmt.close();
        }
    }
}

Related

  1. getCount(Connection conn, String tableName)
  2. getIdNumber(String tableName)
  3. getImmutableDefaults()
  4. getLastCreatedEntry(Connection conn, String tableName)
  5. getMetaDataMap(Statement statement, String tableOrViewName)
  6. getRowCount(Statement stmt, String tableName)
  7. getSensorsList( final Connection jdbcConnection, String tableName)
  8. getShapeText(Connection conn, String tableName)
  9. getTableFields(Connection con, String tableName)