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.concursive.connect.web.modules.common.social.rating.dao.Rating.java
public static int queryObjectRatingCount(Connection db, int objectId, String table, String uniqueField) throws SQLException { int count = -1; PreparedStatement pst = db .prepareStatement("SELECT rating_count FROM " + table + " WHERE " + uniqueField + " = ? "); pst.setInt(1, objectId);/*www .j a v a 2 s . c om*/ ResultSet rs = pst.executeQuery(); if (rs.next()) { count = rs.getInt("rating_count"); } rs.close(); pst.close(); return count; }
From source file:edu.lafayette.metadb.model.userman.UserManDAO.java
/** * Update the "last accessed" information of a user. * @param userName the user name to update. * @param accessData semicolon-delimited string of values. * @return true if the last access information for the user was updated successfully, false otherwise. */// www . j av a2 s. co m public static boolean updateLastProject(String userName, String accessData) { if (!MetaDbHelper.userExists(userName)) return false; Connection conn = Conn.initialize(); // Establish connection if (conn != null) { try { PreparedStatement updateUser = conn.prepareStatement(UPDATE_LAST_PROJECT); updateUser.setString(1, accessData); updateUser.setString(2, userName); updateUser.executeUpdate(); updateUser.close(); conn.close(); // Close statement and connection return true; } catch (Exception e) { MetaDbHelper.logEvent(e); } } return false; }
From source file:module.entities.NameFinder.DB.java
public static void InsertNewLemma2DB(String lemma, String category) throws SQLException { String selectSQL = "SELECT * FROM WHERE = ? "; PreparedStatement preparedStatement = connection.prepareStatement(selectSQL); preparedStatement.setString(1, lemma); preparedStatement.setString(2, category); ResultSet rs = preparedStatement.executeQuery(); String insertSQL = "INSERT INTO " + "(lemma_text,lemma_category) VALUES" + "(?,?)"; PreparedStatement prepStatement = connection.prepareStatement(insertSQL); int id = -1;//from w ww. ja v a 2s .c o m if (rs.next()) { id = rs.getInt(1); } else { prepStatement.setString(1, lemma); prepStatement.setString(2, category); prepStatement.addBatch(); } prepStatement.executeBatch(); prepStatement.close(); }
From source file:at.becast.youploader.database.SQLite.java
public static Boolean updateTemplate(int id, Template template) throws SQLException, IOException { PreparedStatement prest = null; ObjectMapper mapper = new ObjectMapper(); String sql = "UPDATE `templates` SET `data`=? WHERE `id`=?"; prest = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); prest.setString(1, mapper.writeValueAsString(template)); prest.setInt(2, id);//ww w . j ava2s. c o m boolean res = prest.execute(); prest.close(); return res; }
From source file:com.concursive.connect.web.modules.common.social.rating.dao.Rating.java
public static int queryObjectRatingValue(Connection db, int objectId, String table, String uniqueField) throws SQLException { int count = -1; PreparedStatement pst = db .prepareStatement("SELECT rating_value FROM " + table + " " + "WHERE " + uniqueField + " = ? "); pst.setInt(1, objectId);//from w w w. jav a 2s . co m ResultSet rs = pst.executeQuery(); if (rs.next()) { count = rs.getInt("rating_value"); } rs.close(); pst.close(); return count; }
From source file:com.concursive.connect.web.modules.setup.utils.SetupUtils.java
/** * Determines if there is an administrative user configured in the database * * @param db/* ww w . j ava 2 s. c o m*/ * @return */ public static boolean isAdminInstalled(Connection db) { int count = -1; try { PreparedStatement pst = db.prepareStatement( "SELECT count(*) AS recordcount " + "FROM users " + "WHERE access_admin = ? "); pst.setBoolean(1, true); ResultSet rs = pst.executeQuery(); rs.next(); count = rs.getInt("recordcount"); rs.close(); pst.close(); } catch (Exception e) { } return count > 0; }
From source file:at.becast.youploader.database.SQLite.java
public static Boolean setUploadFinished(int upload_id, Status Status) { PreparedStatement prest = null; String sql = "UPDATE `uploads` SET `status`=?,`url`=?,`uploaded`=`lenght` WHERE `id`=?"; try {//from w w w. j a va 2 s . co m prest = c.prepareStatement(sql); prest.setString(1, Status.toString()); prest.setString(2, ""); prest.setInt(3, upload_id); boolean res = prest.execute(); prest.close(); return res; } catch (SQLException e) { LOG.error("Error marking upload as finished", e); return false; } }
From source file:com.concursive.connect.web.modules.common.social.rating.dao.Rating.java
/** * Deletes just a specific user's rating, and updates the parent table with a proper calculation * * @param db/*from ww w . j a v a2 s. c om*/ * @param userId * @param objectId * @param table * @param uniqueField * @throws SQLException */ public static synchronized void delete(Connection db, int userId, int objectId, String table, String uniqueField) throws SQLException { boolean commit = false; try { commit = db.getAutoCommit(); if (commit) { db.setAutoCommit(false); } // Get the project's rating int ratingCount = queryObjectRatingCount(db, objectId, table, uniqueField); // Get the user's rating int thisRating = queryUserRating(db, userId, objectId, table, uniqueField); // Delete the user's rating PreparedStatement pst = db.prepareStatement( "DELETE FROM " + table + "_rating " + "WHERE " + uniqueField + " = ? " + "AND enteredby = ? "); pst.setInt(1, objectId); pst.setInt(2, userId); int deleteCount = pst.executeUpdate(); pst.close(); if (deleteCount > 0 && thisRating != INAPPROPRIATE_COMMENT) { // Update the parent table's rating information // NOTE: make sure not to divide by 0 pst = db.prepareStatement("UPDATE " + table + " " + "SET rating_count = rating_count - ?, rating_value = rating_value - ?, " + (ratingCount == 0 ? "rating_avg = 0 " : "rating_avg = ((rating_value - ?) / (rating_count - ?)) ") + "WHERE " + uniqueField + " = ? "); int i = 0; pst.setInt(++i, 1); pst.setInt(++i, thisRating); if (ratingCount > 1) { pst.setInt(++i, thisRating); pst.setInt(++i, 1); } pst.execute(); pst.close(); } } catch (Exception e) { if (commit) { db.rollback(); } throw new SQLException(e.getMessage()); } finally { if (commit) { db.setAutoCommit(true); } } }
From source file:com.concursive.connect.web.modules.setup.utils.SetupUtils.java
/** * Determines if a default project has been installed * * @param db/*from w ww . j a v a2 s. c om*/ * @return */ public static boolean isDefaultProjectInstalled(Connection db) { int count = -1; try { PreparedStatement pst = db.prepareStatement( "SELECT count(*) AS recordcount " + "FROM projects " + "WHERE system_default = ? "); pst.setBoolean(1, true); ResultSet rs = pst.executeQuery(); rs.next(); count = rs.getInt("recordcount"); rs.close(); pst.close(); } catch (Exception e) { } return count > 0; }
From source file:at.becast.youploader.database.SQLite.java
public static Boolean startUpload(int id, long progress) { PreparedStatement prest = null; String sql = "UPDATE `uploads` SET `status`=?,`uploaded`=? WHERE `id`=?"; try {/* w w w . j ava 2 s .co m*/ prest = c.prepareStatement(sql); prest.setString(1, UploadManager.Status.UPLOADING.toString()); prest.setLong(2, progress); prest.setInt(3, id); boolean res = prest.execute(); prest.close(); return res; } catch (SQLException e) { LOG.error("Error starting upload", e); return false; } }