Java SQL PreparedStatement runInsertLong(PreparedStatement s)

Here you can find the source of runInsertLong(PreparedStatement s)

Description

runInsertLong is a database insert helper function, it runs an insert and returns a Long value based on the result of the query - Statements must return a value to be used with this function

License

Open Source License

Parameter

Parameter Description
s a prepared statement that provides a return value

Return

long return of the insert query

Declaration

protected static long runInsertLong(PreparedStatement s) 

Method Source Code

//package com.java2s;
/*/*from   www.  java2  s . co m*/
 collabREate Utils
 Copyright (C) 2008 Chris Eagle <cseagle at gmail d0t com>
 Copyright (C) 2008 Tim Vidas <tvidas at gmail d0t com>

 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.*;

public class Main {
    /**
     * runInsertLong is a database insert helper function, it runs an insert and returns
     * a Long value based on the result of the query - Statements must return a value to
     * be used with this function
     * @param s a prepared statement that provides a return value
     * @return long return of the insert query
     */
    protected static long runInsertLong(PreparedStatement s) {
        long rval = -1;
        try {
            ResultSet rs = s.executeQuery();
            if (rs.next()) {
                rval = rs.getLong(1);
                System.out.println("SQL Insert rval:  " + rval);
            }
            rs.close();
        } catch (SQLException e) {
            System.err.println("SQL Exception encountered");
            System.err.println(e);
        } catch (Exception exc) {
            System.err
                    .println("Database Insert error: " + exc.getMessage());
            exc.printStackTrace();
        }
        return rval;
    }
}

Related

  1. prepareStatement(String parameterizedSQL, List values, Connection conn)
  2. prepareStatement(String query)
  3. prepareStatementForwardReadOnly(Connection conn, String name, String sql)
  4. putArgsToStatement(PreparedStatement stmt, List args)
  5. query(List paramList, Connection conn, PreparedStatement pstmt)
  6. saveResourceInfoRecord(long resourceId, long propertyId, String propertyValue, String userId, PreparedStatement ps)
  7. setByte(PreparedStatement statement, int index, Byte value)
  8. setBytes(PreparedStatement pstmt, int index, byte[] bytes)
  9. setValue(PreparedStatement ps, int paramIndex, Object inValue)

  10. HOME | Copyright © www.java2s.com 2016