List of usage examples for java.sql PreparedStatement getUpdateCount
int getUpdateCount() throws SQLException;
ResultSet
object or there are no more results, -1 is returned. From source file:com.chiorichan.database.DatabaseEngine.java
public int queryUpdate(String query) throws SQLException { int cnt = 0;/*w ww. jav a2 s .c o m*/ if (con == null) throw new SQLException("The SQL connection is closed or was never opened."); try { PreparedStatement statement = con.prepareStatement(query); statement.execute(); cnt = statement.getUpdateCount(); } catch (MySQLNonTransientConnectionException e) { if (reconnect()) return queryUpdate(query); } catch (CommunicationsException e) { if (reconnect()) return queryUpdate(query); } catch (SQLException e) { throw e; } catch (Exception e) { e.printStackTrace(); } Loader.getLogger().fine("Update Query: \"" + query + "\" which affected " + cnt + " row(s)."); return cnt; }
From source file:org.wso2.carbon.appfactory.core.dao.JDBCResourceDAO.java
/** * update resources//w ww . jav a 2 s . c om * * @param applicationKey application key * @param resourceType resource type * @param resourceName resource Name * @param environment environment * @param description description * @return status of update operation * @throws AppFactoryException */ public boolean updateResource(String applicationKey, String resourceType, String resourceName, String environment, String description) throws AppFactoryException { Connection databaseConnection = null; PreparedStatement preparedStatement = null; try { databaseConnection = AppFactoryDBUtil.getConnection(); preparedStatement = databaseConnection.prepareStatement(SQLConstants.UPDATE_RESOURCE); preparedStatement.setString(1, description); preparedStatement.setString(2, applicationKey); preparedStatement.setInt(3, CarbonContext.getThreadLocalCarbonContext().getTenantId()); preparedStatement.setString(4, resourceType); preparedStatement.setString(5, resourceName); preparedStatement.setString(6, environment); preparedStatement.setInt(7, CarbonContext.getThreadLocalCarbonContext().getTenantId()); preparedStatement.execute(); if (preparedStatement.getUpdateCount() > 0) { databaseConnection.commit(); JDBCResourceCacheManager.clearCache(applicationKey, environment, resourceType); if (log.isDebugEnabled()) { log.debug("Cache cleared for resource type : " + resourceType + " of application : " + applicationKey + " in : " + environment); } return true; } } catch (SQLException e) { String msg = "Error while updating the database for the resource type : " + resourceType + " with the name : " + resourceName + " for application : " + applicationKey + " in environment : " + environment; log.error(msg, e); throw new AppFactoryException(msg, e); } finally { AppFactoryDBUtil.closePreparedStatement(preparedStatement); AppFactoryDBUtil.closeConnection(databaseConnection); } return false; }
From source file:com.tesora.dve.common.DBHelper.java
private boolean executePrepared(PreparedStatement ps, String query, List<Object> params) throws SQLException, PEException { if (ps == null) { throw new PEException("A prepared statement is not available to be executed - call prepare first"); }/*from w ww . ja v a 2s. c o m*/ if (logger.isDebugEnabled() && query != null) logger.debug("Command: " + query); for (int i = 0; i < params.size(); i++) { ps.setObject(i + 1, params.get(i)); } boolean ret = ps.execute(); if (!ret) { // when stmt.execute returns false it means no result set is // expected get the number of rows affected rowCount = ps.getUpdateCount(); printLine(rowCount + " rows affected"); } else { // a prepStmt returning a result set was run resultSet = ps.getResultSet(); if (useBufferedQuery) resultSet.setFetchSize(Integer.MAX_VALUE); } return ret; }
From source file:org.geowebcache.storage.jdbc.metastore.JDBCMBWrapper.java
public void renameLayer(final long layerId, final String newLayerName) throws SQLException { String statement = "UPDATE LAYERS SET VALUE = ? WHERE ID = ?"; final Connection conn = getConnection(); try {//w w w . ja v a 2 s.co m PreparedStatement prep = conn.prepareStatement(statement); try { prep.setString(1, newLayerName); prep.setLong(2, layerId); prep.execute(); int updateCount = prep.getUpdateCount(); if (1 == updateCount) { log.info("Layer " + layerId + " successfully renamed to '" + newLayerName + "'"); } else { log.warn("Attempt to rename layer with id " + layerId + " as '" + newLayerName + "' did not produce any update on the database!"); } } finally { close(prep); } } finally { close(conn); } }
From source file:com.alibaba.wasp.jdbc.TestPreparedStatement.java
public void testGetMoreResults() throws SQLException { Statement stat = conn.createStatement(); PreparedStatement prep; ResultSet rs;/* w w w .j av a 2s . c om*/ stat.execute("CREATE TABLE TEST(ID INT)"); stat.execute("INSERT INTO TEST VALUES(1)"); prep = conn.prepareStatement("SELECT * FROM TEST"); // just to check if it doesn't throw an exception - it may be null prep.getMetaData(); assertTrue(prep.execute()); rs = prep.getResultSet(); assertFalse(prep.getMoreResults()); assertEquals(-1, prep.getUpdateCount()); // supposed to be closed now assertThrows(SQLErrorCode.OBJECT_CLOSED, rs).next(); assertEquals(-1, prep.getUpdateCount()); prep = conn.prepareStatement("UPDATE TEST SET ID = 2"); assertFalse(prep.execute()); assertEquals(1, prep.getUpdateCount()); assertFalse(prep.getMoreResults(Statement.CLOSE_CURRENT_RESULT)); assertEquals(-1, prep.getUpdateCount()); // supposed to be closed now assertThrows(SQLErrorCode.OBJECT_CLOSED, rs).next(); assertEquals(-1, prep.getUpdateCount()); prep = conn.prepareStatement("DELETE FROM TEST"); prep.executeUpdate(); assertFalse(prep.getMoreResults()); assertEquals(-1, prep.getUpdateCount()); }
From source file:com.l2jfree.gameserver.handler.admincommands.AdminTeleport.java
private void changeCharacterPosition(L2Player activeChar, String name) { Connection con = null;/*from w w w . j a va2s. c o m*/ try { con = L2DatabaseFactory.getInstance().getConnection(con); PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=? WHERE char_name=?"); statement.setInt(1, activeChar.getX()); statement.setString(2, name); statement.execute(); statement = con.prepareStatement("UPDATE characters SET y=? WHERE char_name=?"); statement.setInt(1, activeChar.getY()); statement.setString(2, name); statement.execute(); statement = con.prepareStatement("UPDATE characters SET z=? WHERE char_name=?"); statement.setInt(1, activeChar.getZ()); statement.setString(2, name); statement.execute(); int count = statement.getUpdateCount(); statement.close(); if (count == 0) activeChar.sendMessage("Character not found or position unaltered."); else { activeChar.sendMessage("Character's position is now set to (" + activeChar.getX() + "," + activeChar.getY() + "," + activeChar.getZ() + ")"); } } catch (SQLException se) { activeChar.sendMessage("SQLException while changing offline character's position"); } finally { L2DatabaseFactory.close(con); } }
From source file:org.etudes.jforum.dao.generic.GenericPrivateMessageDAO.java
/** * @see org.etudes.jforum.dao.PrivateMessageDAO#delete(org.etudes.jforum.entities.PrivateMessage[]) */// w w w .j a v a 2 s . c om public void delete(PrivateMessage[] pm) throws Exception { PreparedStatement p = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("PrivateMessageModel.delete")); p.setInt(2, pm[0].getFromUser().getId()); p.setInt(3, pm[0].getFromUser().getId()); PreparedStatement delete = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("PrivateMessagesModel.deleteText")); PreparedStatement deleteAttachInfo = JForum.getConnection() .prepareStatement(SystemGlobals.getSql("PrivateMessagesModel.deleteAttachmentInfo")); for (int i = 0; i < pm.length; i++) { p.setInt(1, pm[i].getId()); p.executeUpdate(); if (p.getUpdateCount() > 0) { delete.setInt(1, pm[i].getId()); delete.executeUpdate(); deleteAttachInfo.setInt(1, pm[i].getId()); deleteAttachInfo.executeUpdate(); } } delete.close(); deleteAttachInfo.close(); p.close(); // Mallika's new code beg CoursePrivateMessageDAO cpmDao = DataAccessDriver.getInstance().newCoursePrivateMessageDAO(); for (int i = 0; i < pm.length; i++) { cpmDao.delete(pm[i].getId()); } // Mallika's new code end }
From source file:org.wso2.carbon.appfactory.core.dao.JDBCResourceDAO.java
/** * Add resource to an application//w ww. j a va 2 s .c om * * @param applicationKey application key * @param resourceName application name * @param resourceType resource type * @param environment environment * @param description description * @return true if resource added successfully * @throws AppFactoryException */ public boolean addResource(String applicationKey, String resourceName, String resourceType, String environment, String description) throws AppFactoryException { Connection databaseConnection = null; PreparedStatement preparedStatement = null; try { databaseConnection = AppFactoryDBUtil.getConnection(); preparedStatement = databaseConnection.prepareStatement(SQLConstants.ADD_RESOURCE_SQL); int applicationId = JDBCApplicationDAO.getInstance().getAutoIncrementAppID(applicationKey, databaseConnection); preparedStatement.setInt(1, applicationId); preparedStatement.setString(2, resourceName); preparedStatement.setString(3, resourceType); preparedStatement.setString(4, environment); preparedStatement.setString(5, description); preparedStatement.setInt(6, CarbonContext.getThreadLocalCarbonContext().getTenantId()); preparedStatement.execute(); int affectedRow = preparedStatement.getUpdateCount(); if (affectedRow > 0) { databaseConnection.commit(); // clear the cache JDBCResourceCacheManager.clearCache(applicationKey, environment, resourceType); if (log.isDebugEnabled()) { log.debug("Cache cleared for resource type : " + resourceType + " of application key : " + applicationKey + " in : " + environment); } return true; } } catch (SQLException e) { try { if (databaseConnection != null) { databaseConnection.rollback(); } } catch (SQLException e1) { String msg = "Error while rolling back resource addition for : " + resourceName + " of resource type : " + resourceType + " of application key : " + applicationKey + " in : " + environment; log.error(msg, e1); } String msg = "Error while adding resource : " + resourceName + " of resource type : " + resourceType + " of application key : " + applicationKey + " in : " + environment; log.error(msg, e); throw new AppFactoryException(msg, e); } finally { AppFactoryDBUtil.closePreparedStatement(preparedStatement); AppFactoryDBUtil.closeConnection(databaseConnection); } return false; }
From source file:edu.umd.cs.marmoset.modelClasses.Project.java
public boolean setHidden(boolean newValue, Connection conn) throws SQLException { String update = "UPDATE projects set hidden=? where project_pk=?"; PreparedStatement stmt = null; try {//from w ww . j a va 2s .co m stmt = Queries.setStatement(conn, update, newValue, projectPK); stmt.execute(); return stmt.getUpdateCount() > 0; } finally { Queries.closeStatement(stmt); } }
From source file:org.jumpmind.db.sql.JdbcSqlTemplate.java
public int update(final String sql, final Object[] args, final int[] types) { logSql(sql, args);//from ww w.j a va 2 s .com return execute(new IConnectionCallback<Integer>() { public Integer execute(Connection con) throws SQLException { if (args == null) { Statement stmt = null; try { stmt = con.createStatement(); stmt.setQueryTimeout(settings.getQueryTimeout()); stmt.execute(sql); return stmt.getUpdateCount(); } finally { close(stmt); } } else { PreparedStatement ps = null; try { ps = con.prepareStatement(sql); ps.setQueryTimeout(settings.getQueryTimeout()); if (types != null) { setValues(ps, args, types, getLobHandler().getDefaultHandler()); } else { setValues(ps, args); } ps.execute(); return ps.getUpdateCount(); } finally { close(ps); } } } }); }