Java SQL PreparedStatement getIdentity(PreparedStatement stat)

Here you can find the source of getIdentity(PreparedStatement stat)

Description

Returns the value for IDENTITY column returned by the last execution of the given statement.

License

Open Source License

Parameter

Parameter Description
stat The statement executed that performed an insert in a table with an IDENTITY column.

Exception

Parameter Description

Return

The value of IDENTITY.

Declaration

static final long getIdentity(PreparedStatement stat)
        throws SQLException 

Method Source Code

//package com.java2s;
/*/*from w w w .j a  va2  s.  co  m*/
    
 Copyright (C)    2007 Joao F. (joaof@sourceforge.net)
 http://paccman.sourceforge.net 

 This program is free software; you can redistribute it and/or modify      
 it under the terms of the GNU General Public License as published by      
 the Free Software Foundation; either version 2 of the License, or         
 (at your option) any later version.                                       

 This program is distributed in the hope that it will be useful,           
 but WITHOUT ANY WARRANTY; without even the implied warranty of            
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             
 GNU General Public License for more details.                              

 You should have received a copy of the GNU General Public License         
 along with this program; if not, write to the Free Software               
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    
 */

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
    /**
     * Returns the value for IDENTITY column returned by the last execution of 
     * the given statement.
     * @param stat The statement executed that performed an insert in a table
     * with an IDENTITY column.
     * @return The value of IDENTITY.
     * @throws java.sql.SQLException
     */
    static final long getIdentity(PreparedStatement stat)
            throws SQLException {
        ResultSet rs = stat.getGeneratedKeys();
        rs.next();
        return rs.getBigDecimal(1).longValue();
    }
}

Related

  1. fillStatement(PreparedStatement ps,Object...params)
  2. fillStatement(PreparedStatement stmt, Object[] params)
  3. getFileContent(final File file)
  4. getGeneratedKey(PreparedStatement ps)
  5. getGenerateKey(PreparedStatement stmt)
  6. getLastId(PreparedStatement s)
  7. getLimitedBatchSizePreparedStatement(PreparedStatement pstmt, int maxBatchSize)
  8. getNewPreparedStatement(String format)
  9. getPreparedStatement(Connection conn, String sql)