Java SQL PreparedStatement query(List paramList, Connection conn, PreparedStatement pstmt)

Here you can find the source of query(List paramList, Connection conn, PreparedStatement pstmt)

Description

Don't forget: After execute query, we need to close resultset, prepared statement and connection in persistence layer

License

Apache License

Parameter

Parameter Description
mapper query parameters
conn a parameter
pstmt a parameter

Exception

Parameter Description
SQLException an exception

Declaration

public static ResultSet query(List<String> paramList, Connection conn, PreparedStatement pstmt)
        throws SQLException 

Method Source Code


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

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

public class Main {
    /**//  ww w .  j av  a  2 s.c  o  m
     * Don't forget: After execute query, we need to close resultset, prepared statement and connection in
     * persistence layer
     * @param mapper query parameters
     * @param conn
     * @param pstmt
     * @return
     * @throws SQLException
     */
    public static ResultSet query(List<String> paramList, Connection conn, PreparedStatement pstmt)
            throws SQLException {
        ResultSet result = null;
        try {
            Iterator<String> paramIter = paramList.iterator();
            int i = 1;
            while (paramIter.hasNext()) {
                String eachParam = paramIter.next();
                pstmt.setString(i, eachParam);
                i++;
            }
            result = pstmt.executeQuery();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    /**
     * Don't forget: After execute query, we need to close resultset, prepared statement and connection in
     * persistence layer
     * @param mapper query parameters
     * @param conn
     * @param pstmt
     * @return
     * @throws SQLException
     */
    public static ResultSet query(Connection conn, PreparedStatement pstmt) throws SQLException {
        ResultSet result = null;
        try {
            result = pstmt.executeQuery();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
}

Related

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

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