List of usage examples for java.sql PreparedStatement close
void close() throws SQLException;
Statement
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. From source file:com.useekm.indexing.postgis.IndexedStatement.java
/** * Search for a statements with a given text predicate. For testing purposes * only./*from w ww. ja v a2s. c o m*/ * * @param vectorConfig * Name of the configuration to use for building the query. * @param query * The search query, e.g. "apples & oranges". * @return An closeable iteration to the resulting {@link IndexedStatement} * s. */ static CloseableIterator<IndexedStatement> search(Connection conn, String table, String vectorConfig, String query) throws SQLException, IndexException { Validate.notEmpty(query); validateConfig(vectorConfig); StringBuffer sql = new StringBuffer(MAX_SELECT_LEN).append(SELECT + table).append(WHERE); sql.append(OBJECT_TS_VECTOR); sql.append("@@to_tsquery('").append(vectorConfig).append("',?)"); PreparedStatement stat = conn.prepareStatement(sql.toString()); StatementIterator result = null; try { stat.setString(1, query); result = new StatementIterator(stat); } finally { if (result == null) // else StatementIterator should close the stat stat.close(); } return result; }
From source file:com.microsoft.sqlserver.jdbc.connection.PoolingTest.java
/** * setup connection, get connection from pool, and test threads * /* www. j a v a2 s .co m*/ * @param ds * @throws SQLException */ private static void connect(DataSource ds) throws SQLException { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; try { con = ds.getConnection(); pst = con.prepareStatement("SELECT SUSER_SNAME()"); pst.setQueryTimeout(5); rs = pst.executeQuery(); // TODO : we are commenting this out due to AppVeyor failures. Will investigate later. // assertTrue(countTimeoutThreads() >= 1, "Timeout timer is missing."); while (rs.next()) { rs.getString(1); } } finally { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } }
From source file:com.concursive.connect.web.modules.wiki.utils.WikiUtils.java
public static HashMap<String, ImageInfo> buildImageInfo(Connection db, int projectId) throws SQLException { HashMap<String, ImageInfo> images = new HashMap<String, ImageInfo>(); // Full size image PreparedStatement pst = db .prepareStatement("SELECT client_filename, filename, image_width, image_height, version " + "FROM project_files " + "WHERE link_module_id = ? " + "AND link_item_id = ? "); pst.setInt(1, Constants.PROJECT_WIKI_FILES); pst.setInt(2, projectId);//from w ww .j a v a2s. co m ResultSet rs = pst.executeQuery(); while (rs.next()) { ImageInfo image = new ImageInfo(rs); images.put(image.getFilename(), image); } rs.close(); pst.close(); return images; }
From source file:com.hangum.tadpole.rdb.core.editors.main.utils.plan.OracleExecutePlanUtils.java
/** * oracle query plan? . /*from ww w. j av a 2s . c o m*/ * * @param userDB * @param reqQuery * @param planTableName * @throws Exception */ public static void plan(UserDBDAO userDB, RequestQuery reqQuery, String planTableName, java.sql.Connection javaConn, String statement_id) throws Exception { PreparedStatement pstmt = null; try { String query = PartQueryUtil.makeExplainQuery(userDB, reqQuery.getSql()); query = StringUtils.replaceOnce(query, PublicTadpoleDefine.STATEMENT_ID, statement_id); query = StringUtils.replaceOnce(query, PublicTadpoleDefine.DELIMITER, planTableName); pstmt = javaConn.prepareStatement(query); if (reqQuery.getSqlStatementType() == SQL_STATEMENT_TYPE.PREPARED_STATEMENT) { final Object[] statementParameter = reqQuery.getStatementParameter(); for (int i = 1; i <= statementParameter.length; i++) { pstmt.setObject(i, statementParameter[i - 1]); } } pstmt.execute(); } finally { try { if (pstmt != null) pstmt.close(); } catch (Exception e) { } } }
From source file:at.becast.youploader.database.SQLite.java
public static int saveTemplate(Template template) throws SQLException, IOException { PreparedStatement prest = null; ObjectMapper mapper = new ObjectMapper(); String sql = "INSERT INTO `templates` (`name`, `data`) " + "VALUES (?,?)"; prest = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); prest.setString(1, template.getName()); prest.setString(2, mapper.writeValueAsString(template)); prest.execute();/*w w w . j av a 2 s . c o m*/ ResultSet rs = prest.getGeneratedKeys(); prest.close(); if (rs.next()) { int id = rs.getInt(1); rs.close(); return id; } else { return -1; } }
From source file:com.wso2.raspberrypi.Util.java
public static void clearAllRaspberryPis() throws RaspberryPiException { List<RaspberryPi> raspberryPis = getRaspberryPis(null); for (RaspberryPi raspberryPi : raspberryPis) { if (raspberryPi.getReservedFor() != null && !raspberryPi.getReservedFor().isEmpty()) { throw new RaspberryPiException("Cannot clear Raspberry Pis because some are reserved"); }/*from w ww . j a va 2 s.c o m*/ } System.out.println("Removing all Raspberry Pis..."); BasicDataSource ds = getBasicDataSource(); Connection dbConnection = null; PreparedStatement prepStmt = null; try { dbConnection = ds.getConnection(); prepStmt = dbConnection.prepareStatement("DELETE FROM RASP_PI"); prepStmt.execute(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (dbConnection != null) { dbConnection.close(); } if (prepStmt != null) { prepStmt.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
From source file:com.concursive.connect.web.modules.profile.utils.ProjectUtils.java
public static synchronized String updateUniqueId(Connection db, int projectId, String title) throws SQLException { if (projectId == -1) { throw new SQLException("ID was not specified"); }// www . jav a2 s . c o m if (title == null) { throw new SQLException("Title was not specified"); } // reserve a unique text id for the project String uniqueId = ProjectUtils.generateUniqueId(title, projectId, db); PreparedStatement pst = db .prepareStatement("UPDATE projects " + "SET projecttextid = ? " + "WHERE project_id = ?"); pst.setString(1, uniqueId); pst.setInt(2, projectId); pst.execute(); pst.close(); CacheUtils.invalidateValue(Constants.SYSTEM_PROJECT_CACHE, projectId); CacheUtils.invalidateValue(Constants.SYSTEM_PROJECT_UNIQUE_ID_CACHE, uniqueId); return uniqueId; }
From source file:com.wso2.raspberrypi.Util.java
public static void deleteRaspberryPi(String mac) { System.out.println("Removing Raspberry Pi: " + mac); BasicDataSource ds = getBasicDataSource(); Connection dbConnection = null; PreparedStatement prepStmt = null; try {/*from ww w. jav a2 s.co m*/ dbConnection = ds.getConnection(); prepStmt = dbConnection.prepareStatement("DELETE FROM RASP_PI WHERE mac='" + mac + "'"); prepStmt.execute(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (dbConnection != null) { dbConnection.close(); } if (prepStmt != null) { prepStmt.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
From source file:com.wso2.raspberrypi.Util.java
public static void updateKeyValuePair(String key, String value) { BasicDataSource ds = getBasicDataSource(); Connection dbConnection = null; PreparedStatement prepStmt = null; try {//from w w w . jav a 2s . c o m dbConnection = ds.getConnection(); prepStmt = dbConnection.prepareStatement("UPDATE KV_PAIR SET v='" + value + "' where k='" + key + "'"); prepStmt.execute(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (dbConnection != null) { dbConnection.close(); } if (prepStmt != null) { prepStmt.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
From source file:com.wso2.raspberrypi.Util.java
public static void releasePi(String macAddress) { System.out.println("Releasing RPi " + macAddress); BasicDataSource ds = getBasicDataSource(); Connection dbConnection = null; PreparedStatement prepStmt = null; try {// w w w . j a v a 2 s . co m dbConnection = ds.getConnection(); prepStmt = dbConnection.prepareStatement("UPDATE RASP_PI SET owner='' where mac='" + macAddress + "'"); prepStmt.execute(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (dbConnection != null) { dbConnection.close(); } if (prepStmt != null) { prepStmt.close(); } } catch (SQLException e) { e.printStackTrace(); } } }