List of usage examples for java.sql PreparedStatement executeUpdate
int executeUpdate() throws SQLException;
PreparedStatement
object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT
, UPDATE
or DELETE
; or an SQL statement that returns nothing, such as a DDL statement. From source file:dsd.dao.DAOProvider.java
public static int UpdateRowsSecure(String table, String[] fields, Connection con, Object[][] valueArray) throws SQLException { try {/*from w w w .j a v a2s . c o m*/ String values = "("; if (valueArray[0].length > 0) { values += "?"; } for (int i = 1; i < valueArray[0].length; i++) { values += ",?"; } values += ")"; String rows = ""; for (int j = 0; j < valueArray.length; j++) { rows += " " + values; if (j != valueArray.length - 1) rows += " , "; } String set = ""; // index starts from 1 cause index 0 should be ID column and this // doesn't goes into "on duplicate key update" for (int i = 1; i < fields.length; i++) { set += fields[i] + " = values (" + fields[i] + ")"; if (i != fields.length - 1) set += " , "; } String onDuplicateKey = "on duplicate key update " + set; PreparedStatement command = con.prepareStatement(String.format("insert into %s (%s) values %s %s", table, StringUtils.join(fields, ','), rows, onDuplicateKey)); for (int i = 0; i < valueArray.length; i++) { for (int j = 0; j < valueArray[i].length; j++) { SetParameter(command, valueArray[i][j], i * valueArray[i].length + j + 1); } } command.executeUpdate(); } catch (Exception ex) { ex.printStackTrace(); } return 0; }
From source file:ca.qc.adinfo.rouge.achievement.db.AchievementDb.java
public static boolean updateAchievement(DBManager dbManager, String key, long userId, double progress) { Connection connection = null; PreparedStatement stmt = null; ResultSet rs = null;/* ww w . ja v a 2s . c om*/ String sql = null; sql = "INSERT INTO rouge_achievement_progress (`achievement_key`, `user_id`, `progress`) " + "VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE progress = GREATEST(progress, ?)"; try { connection = dbManager.getConnection(); stmt = connection.prepareStatement(sql); stmt.setString(1, key); stmt.setLong(2, userId); stmt.setDouble(3, progress); stmt.setDouble(4, progress); int ret = stmt.executeUpdate(); return (ret > 0); } catch (SQLException e) { log.error(stmt); log.error(e); return false; } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(stmt); DbUtils.closeQuietly(connection); } }
From source file:ca.qc.adinfo.rouge.mail.db.MailDb.java
public static boolean sendMail(DBManager dbManager, long fromId, long toId, RougeObject content) { Connection connection = null; PreparedStatement stmt = null; ResultSet rs = null;//from w ww . j ava2 s . c om String sql = null; sql = "INSERT INTO rouge_mail (`from`, `to`, `content`, `status`, `time_sent`) " + "VALUES (?, ?, ?, ?, ?)"; try { connection = dbManager.getConnection(); stmt = connection.prepareStatement(sql); stmt.setLong(1, fromId); stmt.setLong(2, toId); stmt.setString(3, content.toJSON().toString()); stmt.setString(4, "unr"); stmt.setTimestamp(5, new Timestamp(System.currentTimeMillis())); int ret = stmt.executeUpdate(); return (ret > 0); } catch (SQLException e) { log.error(stmt); log.error(e); return false; } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(stmt); DbUtils.closeQuietly(connection); } }
From source file:edu.lafayette.metadb.model.userman.UserManDAO.java
/** * Update the login time of a user./*from www. j av a 2s . c o m*/ * @param userName the user name to update. * @param value the value of the login time, as a long integer. * @return true if the user's login time was updated successfully, false otherwise. */ public static boolean updateLoginTime(String userName, long value) { if (!MetaDbHelper.userExists(userName)) return false; Connection conn = Conn.initialize(); // Establish connection if (conn != null) { try { PreparedStatement updateUser = conn.prepareStatement(UPDATE_LAST_LOGIN); updateUser.setLong(1, value); 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
/** * Update the activity log//ww w.j ava 2 s .co m * * @param endTime * @param status_id * @param regexerId * @param obj * @throws java.sql.SQLException */ public static void UpdateLogRegexFinder(long endTime, int regexerId, JSONObject obj) throws SQLException { String updateCrawlerStatusSql = "UPDATE log.activities SET " + "end_date = ?, status_id = ?, message = ?" + "WHERE id = ?"; PreparedStatement prepUpdStatusSt = connection.prepareStatement(updateCrawlerStatusSql); prepUpdStatusSt.setTimestamp(1, new java.sql.Timestamp(endTime)); prepUpdStatusSt.setInt(2, 2); prepUpdStatusSt.setString(3, obj.toJSONString()); prepUpdStatusSt.setInt(4, regexerId); prepUpdStatusSt.executeUpdate(); prepUpdStatusSt.close(); }
From source file:edu.lafayette.metadb.model.userman.UserManDAO.java
/** * Update the user type of a user./*from ww w . ja va 2 s . c om*/ * * @param userName The username of the user to update. * @param newUserType The new user type for the user to update. * @return true if the user's user type is updated successfully, false otherwise */ public static boolean updateUserType(String userName, String newUserType) { Connection conn = Conn.initialize(); // Establish connection if (conn != null) { try { PreparedStatement updateUser = conn.prepareStatement(UPDATE_USER_TYPE); updateUser.setString(1, newUserType); 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:edu.lafayette.metadb.model.userman.UserManDAO.java
/** * Update the authentication type of a user. * //w ww .jav a2 s . c om * @param userName The username of the user to update. * @param newAuthType The new authentication type for the user to update. * @return true if the user's authentication type is updated successfully, false otherwise */ public static boolean updateAuthType(String userName, String newAuthType) { Connection conn = Conn.initialize(); // Establish connection if (conn != null) { try { PreparedStatement updateUser = conn.prepareStatement(UPDATE_AUTH_TYPE); updateUser.setString(1, newAuthType); 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: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. *///from www. java 2s .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:dsd.dao.DAOProvider.java
/** * the update row method done in a secure way * /*from w w w. ja v a 2s .c o m*/ * @param table * @param updateColumns * @param where * @param con * @param valueArray * @param wherePartParameters * @return * @throws SQLException */ public static int UpdateRowSecure(String table, String[] updateColumns, String where, Connection con, Object[] valueArray, Object[] wherePartParameters) throws SQLException { try { if (updateColumns.length != valueArray.length || updateColumns.length == 0) throw new IllegalArgumentException( "The size of updateColumns and valueArray parameters should be the same!"); String set = ""; for (int i = 0; i < valueArray.length; i++) { set += updateColumns[i] + " = ?"; if (i != valueArray.length - 1) set += ", "; } PreparedStatement command = con.prepareStatement(String.format("update %s set %s %s", table, set, (where.trim().equals("") ? "" : " where " + where))); for (int i = 0; i < valueArray.length; i++) { SetParameter(command, valueArray[i], i + 1); } for (int i = 0; i < wherePartParameters.length; i++) { SetParameter(command, wherePartParameters[i], valueArray.length + i + 1); } return command.executeUpdate(); } catch (Exception ex) { ex.printStackTrace(); } return 0; }
From source file:database.HashTablesTools.java
private static void insertIntoFailureTable(Connection connexion, String tableFailureName, String hash, String fourLetterCode) {/* w w w . j av a 2 s . c o m*/ try { String insertTableSQL = "INSERT INTO " + tableFailureName + " " + "(pdbfilehash, fourLettercode) VALUES" + "(?,?)"; PreparedStatement preparedStatement = connexion.prepareStatement(insertTableSQL); preparedStatement.setString(1, hash); preparedStatement.setString(2, String.valueOf(fourLetterCode)); int ok = preparedStatement.executeUpdate(); preparedStatement.close(); System.out.println(ok + " raw created in failure table" + String.valueOf(fourLetterCode)); } catch (SQLException e1) { System.out.println("Failed to enter entry in " + tableFailureName + " table "); } }