Example usage for java.sql PreparedStatement close

List of usage examples for java.sql PreparedStatement close

Introduction

In this page you can find the example usage for java.sql PreparedStatement close.

Prototype

void close() throws SQLException;

Source Link

Document

Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

Usage

From source file:module.entities.NameFinder.DB.java

public static void insertDemocracitConsultationMinister(TreeMap<Integer, String> consultationCountersign,
        TreeMap<Integer, String> consultationCounterrole) throws SQLException {
    try {//from w w  w.j  av a 2 s  . c  om
        String sql = "INSERT INTO consultations_ner "
                + "(id,countersigned_name, countersigned_position) VALUES " + "(?,?,?)";
        PreparedStatement prepStatement = connection.prepareStatement(sql);
        for (int consId : consultationCountersign.keySet()) {
            prepStatement.setInt(1, consId);
            prepStatement.setString(2, consultationCountersign.get(consId));
            if (consultationCounterrole.get(consId) != null) {
                prepStatement.setString(3, consultationCounterrole.get(consId));
            } else {
                prepStatement.setString(3, "");
            }
            prepStatement.addBatch();
        }
        prepStatement.executeBatch();
        prepStatement.close();
    } catch (BatchUpdateException ex) {
        //            ex.printStackTrace();
        System.out.println(ex.getNextException());
    }
}

From source file:com.wso2.raspberrypi.Util.java

public static List<KVPair> getKeyValuePairs() {
    List<KVPair> results = new ArrayList<KVPair>();

    BasicDataSource ds = getBasicDataSource();

    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;/* w  w  w .j  a v a2 s .c o m*/
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM KV_PAIR ORDER BY k");
        rs = prepStmt.executeQuery();

        while (rs.next()) {
            results.add(new KVPair(rs.getString("k"), rs.getString("v")));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return results;
}

From source file:module.entities.NameFinder.DB.java

/**
 * Starts the activity log/*w w w . ja  v  a 2  s .  c  o m*/
 *
 * @param startTime - The start time of the crawling procedure
 * @return - The activity's log id
 * @throws java.sql.SQLException
 */
public static int LogRegexFinder(long startTime) throws SQLException {
    String insertLogSql = "INSERT INTO log.activities (module_id, start_date, end_date, status_id, message) VALUES (?,?,?,?,?)";
    PreparedStatement prepLogCrawlStatement = connection.prepareStatement(insertLogSql,
            Statement.RETURN_GENERATED_KEYS);
    prepLogCrawlStatement.setInt(1, 4);
    prepLogCrawlStatement.setTimestamp(2, new java.sql.Timestamp(startTime));
    prepLogCrawlStatement.setTimestamp(3, null);
    prepLogCrawlStatement.setInt(4, 1);
    prepLogCrawlStatement.setString(5, null);
    prepLogCrawlStatement.executeUpdate();
    ResultSet rsq = prepLogCrawlStatement.getGeneratedKeys();
    int crawlerId = 0;
    if (rsq.next()) {
        crawlerId = rsq.getInt(1);
    }
    prepLogCrawlStatement.close();
    return crawlerId;
}

From source file:module.entities.NameFinder.DB.java

public static void insertJsonResponse(int curConsId, TreeMap<Integer, String> input) throws SQLException {
    try {/*from   w  ww . j  a v  a2  s .com*/
        String insertSQL = "INSERT INTO enhancedentities " + "(consultation_id,article_id,json_text) VALUES"
                + "(?,?,?);";
        PreparedStatement prepStatement = connection.prepareStatement(insertSQL);
        //            connection.setAutoCommit(false);
        for (int curArticle : input.keySet()) {
            String json_text = input.get(curArticle);
            prepStatement.setInt(1, curConsId);
            prepStatement.setInt(2, curArticle);
            prepStatement.setString(3, json_text);
            //                prepStatement.executeUpdate();
            prepStatement.addBatch();
        }
        prepStatement.executeBatch();
        //            connection.commit();
        prepStatement.close();

        //            for (int i = 0; i<x.length; i++){
        //                System.out.println(x[i]);
        //            }
    } catch (BatchUpdateException ex) {
        ex.printStackTrace();
        //            System.out.println(ex.getNextException());
    }
}

From source file:com.wso2.raspberrypi.Util.java

public static RaspberryPi getRaspberryPi(String macAddress) {
    System.out.println("Listing Raspberry Pi with Mac Address: " + macAddress);
    RaspberryPi pi = null;/*w  w  w.  j a  va2 s.  com*/

    BasicDataSource ds = getBasicDataSource();

    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM RASP_PI WHERE mac='" + macAddress + "'");
        rs = prepStmt.executeQuery();

        while (rs.next()) {
            pi = toRaspberryPi(rs);
            break;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return pi;
}

From source file:com.wso2.raspberrypi.Util.java

public static List<RaspberryPi> getSelectedPis() {
    List<RaspberryPi> results = new ArrayList<RaspberryPi>();

    BasicDataSource ds = getBasicDataSource();

    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;//from  www .j  ava 2 s.  co m
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM RASP_PI WHERE selected=true");
        rs = prepStmt.executeQuery();

        while (rs.next()) {
            RaspberryPi pi = toRaspberryPi(rs);
            results.add(pi);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return results;
}

From source file:at.becast.youploader.database.SQLite.java

public static void savePlaylists(Playlists playlists, int account) throws SQLException, IOException {
    PreparedStatement prest = null;
    String sql = "INSERT INTO `playlists` (`name`, `playlistid`,`image`,`account`) " + "VALUES (?,?,?,?)";
    for (Playlists.Item i : playlists.items) {
        prest = c.prepareStatement(sql);
        prest.setString(1, i.snippet.title);
        prest.setString(2, i.id);/*  w  w w  .j  a  va  2s  . c o m*/
        URL url = new URL(i.snippet.thumbnails.default__.url);
        InputStream is = null;
        is = url.openStream();
        byte[] imageBytes = IOUtils.toByteArray(is);
        prest.setBytes(3, imageBytes);
        prest.setInt(4, account);
        prest.execute();
        prest.close();
    }
}

From source file:com.wso2.raspberrypi.Util.java

public static List<RaspberryPi> getRaspberryPis(String orderBy) {
    System.out.println("Listing registered Raspberry Pis...");

    if (orderBy == null) {
        orderBy = "ip";
    }/*ww  w. j av a  2  s. c  om*/
    List<RaspberryPi> results = new ArrayList<RaspberryPi>();

    BasicDataSource ds = getBasicDataSource();

    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM RASP_PI ORDER BY " + orderBy);
        rs = prepStmt.executeQuery();

        while (rs.next()) {
            RaspberryPi pi = toRaspberryPi(rs);
            results.add(pi);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return results;
}

From source file:org.openmrs.module.kenyaemr.chore.VoidDuplicateIdentifiersTest.java

/**
 * Helper method to execute SQL on the test database
 * @param sql the SQL statement/*w  w w . ja  va  2 s.c o  m*/
 */
private void executeSql(String sql) throws Exception {
    PreparedStatement ps = getConnection().prepareStatement(sql);
    ps.execute();
    ps.close();

    Context.clearSession();
}

From source file:com.streamsets.pipeline.lib.jdbc.MultiRowInsertMap.java

public void executeStatements() throws SQLException {
    for (PreparedStatement statement : cache.values()) {
        statement.executeBatch();/*  w  w  w. ja v a 2s  .  c  om*/
        statement.close();
    }
}