Java Code Examples for java.sql.PreparedStatement.getUpdateCount()

The following are Jave code examples for showing how to use getUpdateCount() of the java.sql.PreparedStatement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: sstore-soft   File: TestJDBCQueries.java   Source Code and License Vote up 7 votes
@Before
public void populateTables()
{
    // Populate tables.
    for (Data d : data) {
        String q = String.format("insert into %s values(?, ?)", d.tablename);
        for (String id : d.good) {
            try {
                PreparedStatement sel = conn.prepareStatement(q);
                sel.setString(1, id);
                sel.setString(2, String.format("VALUE:%s:%s", d.tablename, id));
                sel.execute();
                int count = sel.getUpdateCount();
                assertTrue(count==1);
            }
            catch(SQLException e) {
                System.err.printf("ERROR(INSERT): %s value='%s': %s\n",
                        d.typename, d.good[0], e.getMessage());
                fail();
            }
        }
    }
    populateContestants();
    populateVotes();
    
}
 
Example 2
Project: sstore-soft   File: TestJDBCQueries.java   Source Code and License Vote up 6 votes
private void populateContestants() {
    String q = "insert into contestants values(?, ?)";
    for (int i = 0; i < TABLE_SIZE; i++) {
        try {
            PreparedStatement sel = conn.prepareStatement(q);
            
            sel.setInt(1, i);
            sel.setString(2, "constants" + i);
            sel.execute();
            int count = sel.getUpdateCount();
            assertTrue(count==1);
        }
        catch(SQLException e) {
            System.err.printf("ERROR(INSERT): %s value='%s': %s\n",
                    "votes", i, e.getMessage());
            fail();
        }
    }
}
 
Example 3
Project: s-store   File: TestJDBCQueries.java   Source Code and License Vote up 6 votes
@Before
public void populateTables()
{
    // Populate tables.
    for (Data d : data) {
        String q = String.format("insert into %s values(?, ?)", d.tablename);
        for (String id : d.good) {
            try {
                PreparedStatement sel = conn.prepareStatement(q);
                sel.setString(1, id);
                sel.setString(2, String.format("VALUE:%s:%s", d.tablename, id));
                sel.execute();
                int count = sel.getUpdateCount();
                assertTrue(count==1);
            }
            catch(SQLException e) {
                System.err.printf("ERROR(INSERT): %s value='%s': %s\n",
                        d.typename, d.good[0], e.getMessage());
                fail();
            }
        }
    }
    populateContestants();
    populateVotes();
    
}
 
Example 4
Project: s-store   File: TestJDBCQueries.java   Source Code and License Vote up 6 votes
private void populateContestants() {
    String q = "insert into contestants values(?, ?)";
    for (int i = 0; i < TABLE_SIZE; i++) {
        try {
            PreparedStatement sel = conn.prepareStatement(q);
            
            sel.setInt(1, i);
            sel.setString(2, "constants" + i);
            sel.execute();
            int count = sel.getUpdateCount();
            assertTrue(count==1);
        }
        catch(SQLException e) {
            System.err.printf("ERROR(INSERT): %s value='%s': %s\n",
                    "votes", i, e.getMessage());
            fail();
        }
    }
}
 
Example 5
Project: lams   File: ResultSetReturnImpl.java   Source Code and License Vote up 6 votes
@Override
public ResultSet execute(PreparedStatement statement) {
	// sql logged by StatementPreparerImpl
	try {
		final ResultSet rs;
		try {
			jdbcCoordinator.getTransactionCoordinator().getTransactionContext().startStatementExecution();
			if ( !statement.execute() ) {
				while ( !statement.getMoreResults() && statement.getUpdateCount() != -1 ) {
					// do nothing until we hit the resultset
				}
			}
			rs = statement.getResultSet();
		}
		finally {
			jdbcCoordinator.getTransactionCoordinator().getTransactionContext().endStatementExecution();
		}
		postExtract( rs, statement );
		return rs;
	}
	catch (SQLException e) {
		throw sqlExceptionHelper.convert( e, "could not execute statement" );
	}
}
 
Example 6
Project: L2ACP-api   File: Helpers.java   Source Code and License Vote up 6 votes
public static void jailOfflinePlayer(String name, int delay)
{
	try (Connection con = L2DatabaseFactory.getInstance().getConnection())
	{
		PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=?, punish_level=?, punish_timer=? WHERE char_name=?");
		statement.setInt(1, -114356);
		statement.setInt(2, -249645);
		statement.setInt(3, -2984);
		statement.setInt(4, L2PcInstance.PunishLevel.JAIL.value());
		statement.setLong(5, (delay > 0 ? delay * 60000L : 0));
		statement.setString(6, name);
		
		statement.execute();
		int count = statement.getUpdateCount();
		statement.close();
		
	}
	catch (SQLException se)
	{
	}
}
 
Example 7
Project: L2ACP-api   File: Helpers.java   Source Code and License Vote up 6 votes
public static void unjailOfflinePlayer(String name)
{
	try (Connection con = L2DatabaseFactory.getInstance().getConnection())
	{
		PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=?, punish_level=?, punish_timer=? WHERE char_name=?");
		statement.setInt(1, 17836);
		statement.setInt(2, 170178);
		statement.setInt(3, -3507);
		statement.setInt(4, 0);
		statement.setLong(5, 0);
		statement.setString(6, name);
		statement.execute();
		int count = statement.getUpdateCount();
		statement.close();
	}
	catch (SQLException se)
	{
	}
}
 
Example 8
Project: uroborosql   File: DebugSqlFilter.java   Source Code and License Vote up 6 votes
/**
 * {@inheritDoc}
 *
 * @see jp.co.future.uroborosql.filter.AbstractSqlFilter#doBatch(jp.co.future.uroborosql.context.SqlContext, java.sql.PreparedStatement, int[])
 */
@Override
public int[] doBatch(final SqlContext sqlContext, final PreparedStatement preparedStatement, final int[] result) {
	if (LOG.isDebugEnabled()) {
		int[] counts = result;
		try {
			counts = new int[] { preparedStatement.getUpdateCount() };
		} catch (SQLException ex) {
			ex.printStackTrace();
		}

		StringBuilder builder = new StringBuilder();
		for (int val : counts) {
			builder.append(val).append(", ");
		}
		LOG.debug("SQL:{} executed. Result:{}", sqlContext.getSqlName(), builder.toString());
	}
	return result;
}
 
Example 9
Project: calcite-avatica   File: RemoteDriverTest.java   Source Code and License Vote up 6 votes
@Test public void testInsertDrop() throws Exception {
  final String t = AvaticaUtils.unique("TEST_TABLE2");
  final String create =
      String.format(Locale.ROOT, "create table if not exists %s ("
          + "id int not null, "
          + "msg varchar(3) not null)", t);
  final String insert = String.format(Locale.ROOT,
      "insert into %s values(1, 'foo')", t);
  Connection connection = ljs();
  Statement statement = connection.createStatement();
  statement.execute(create);

  Statement stmt = connection.createStatement();
  int count = stmt.executeUpdate(insert);
  assertThat(count, is(1));
  ResultSet resultSet = stmt.getResultSet();
  assertThat(resultSet, nullValue());

  PreparedStatement pstmt = connection.prepareStatement(insert);
  boolean status = pstmt.execute();
  assertThat(status, is(false));
  int updateCount = pstmt.getUpdateCount();
  assertThat(updateCount, is(1));
}
 
Example 10
Project: L2jBrasil   File: GameStatusThread.java   Source Code and License Vote up 5 votes
private void jailOfflinePlayer(String name, int delay)
{
	Connection con = null;
  	try
   	{
   		con = L2DatabaseFactory.getInstance().getConnection();

   		PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=?, punish_level=?, punish_timer=? WHERE char_name=?");
   		statement.setInt(1, -114356);
   		statement.setInt(2, -249645);
   		statement.setInt(3, -2984);
   		statement.setInt(4, L2PcInstance.PunishLevel.JAIL.value());
   		statement.setLong(5, delay * 60000L);
   		statement.setString(6, name);

		statement.execute();
		int count = statement.getUpdateCount();
		statement.close();

		if (count == 0)
			_print.println("Character not found!");
		else
			_print.println("Character "+name+" jailed for "+(delay>0 ? delay+" minutes." : "ever!"));
   	} catch (SQLException se)
   	{
   		_print.println("SQLException while jailing player");
        if (Config.DEBUG) se.printStackTrace();
   	} finally
   	{
   		try { con.close(); } catch (Exception e) {}
   	}
}
 
Example 11
Project: L2jBrasil   File: GameStatusThread.java   Source Code and License Vote up 5 votes
private void unjailOfflinePlayer(String name)
{
	Connection con = null;
  	try
   	{
   		con = L2DatabaseFactory.getInstance().getConnection();

   		PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=?, punish_level=?, punish_timer=? WHERE char_name=?");
   		statement.setInt(1, 17836);
   		statement.setInt(2, 170178);
   		statement.setInt(3, -3507);
   		statement.setInt(4, 0);
   		statement.setLong(5, 0);
   		statement.setString(6, name);

		statement.execute();
		int count = statement.getUpdateCount();
		statement.close();

		if (count == 0)
			_print.println("Character not found!");
		else
			_print.println("Character "+name+" set free.");
   	} catch (SQLException se)
   	{
   		_print.println("SQLException while jailing player");
        if (Config.DEBUG) se.printStackTrace();
   	} finally
   	{
   		try { con.close(); } catch (Exception e) {}
   	}
}
 
Example 12
Project: L2jBrasil   File: AdminBan.java   Source Code and License Vote up 5 votes
private void jailOfflinePlayer(L2PcInstance activeChar, String name, int delay)
{
	Connection con = null;
	try
	{
		con = L2DatabaseFactory.getInstance().getConnection();

		PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=?, in_jail=?, jail_timer=? WHERE char_name=?");
		statement.setInt(1, -114356);
		statement.setInt(2, -249645);
		statement.setInt(3, -2984);
		statement.setInt(4, 1);
		statement.setLong(5, (delay > 0 ? delay * 60000L : 0));
		statement.setString(6, name);

		statement.execute();
		int count = statement.getUpdateCount();
		statement.close();

		if (count == 0)
			activeChar.sendMessage("Character not found!");
		else
			activeChar.sendMessage("Character "+name+" jailed for "+(delay>0 ? delay+" minutes." : "ever!"));
	} catch (SQLException se)
	{
		activeChar.sendMessage("SQLException while jailing player");
		if (Config.DEBUG) se.printStackTrace();
	} finally
	{
		try { con.close(); } catch (Exception e)
		{
			if (Config.DEBUG)
				e.printStackTrace();
		}
	}
}
 
Example 13
Project: L2jBrasil   File: AdminBan.java   Source Code and License Vote up 5 votes
private void unjailOfflinePlayer(L2PcInstance activeChar, String name)
{
	Connection con = null;
	try
	{
		con = L2DatabaseFactory.getInstance().getConnection();
		PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=?, in_jail=?, jail_timer=? WHERE char_name=?");
		statement.setInt(1, 17836);
		statement.setInt(2, 170178);
		statement.setInt(3, -3507);
		statement.setInt(4, 0);
		statement.setLong(5, 0);
		statement.setString(6, name);
		statement.execute();
		int count = statement.getUpdateCount();
		statement.close();
		if (count == 0)
			activeChar.sendMessage("Character not found!");
		else
			activeChar.sendMessage("Character "+name+" removed from jail");
	} catch (SQLException se)
	{
		activeChar.sendMessage("SQLException while jailing player");
		if (Config.DEBUG) se.printStackTrace();
	} finally
	{
		try
		{
			con.close();
		}
		catch (Exception e)
		{
			if (Config.DEBUG)
				e.printStackTrace();
		}
	}
}
 
Example 14
Project: incubator-netbeans   File: SQLExecutionHelper.java   Source Code and License Vote up 5 votes
private int executePreparedStatement(PreparedStatement stmt) throws SQLException {
    long startTime = System.currentTimeMillis();
    
    stmt.execute();

    long executionTime = System.currentTimeMillis() - startTime;
    String execTimeStr = SQLExecutionHelper.millisecondsToSeconds(executionTime);
    dataView.setInfoStatusText(NbBundle.getMessage(SQLExecutionHelper.class, "MSG_execution_success", execTimeStr));
    
    return stmt.getUpdateCount();
}
 
Example 15
Project: L2ACP-api   File: Helpers.java   Source Code and License Vote up 5 votes
public static void banChatOfflinePlayer(String name, int delay, boolean ban)
{
	int level = 0;
	long value = 0;
	
	if (ban)
	{
		level = L2PcInstance.PunishLevel.CHAT.value();
		value = (delay > 0 ? delay * 60000L : 60000);
	}
	else
	{
		level = L2PcInstance.PunishLevel.NONE.value();
		value = 0;
	}
	
	try (Connection con = L2DatabaseFactory.getInstance().getConnection())
	{
		PreparedStatement statement = con.prepareStatement("UPDATE characters SET punish_level=?, punish_timer=? WHERE char_name=?");
		statement.setInt(1, level);
		statement.setLong(2, value);
		statement.setString(3, name);
		
		statement.execute();
		int count = statement.getUpdateCount();
		statement.close();
	}
	catch (SQLException se)
	{
	}
}
 
Example 16
Project: L2ACP-api   File: Helpers.java   Source Code and License Vote up 5 votes
public static boolean changeCharAccessLevel(L2PcInstance targetPlayer, String player, int lvl)
{
	if (targetPlayer != null)
	{
		targetPlayer.setAccessLevel(lvl);
		targetPlayer.logout();
	}
	else
	{
		try (Connection con = L2DatabaseFactory.getInstance().getConnection())
		{
			PreparedStatement statement = con.prepareStatement("UPDATE characters SET accesslevel=? WHERE char_name=?");
			statement.setInt(1, lvl);
			statement.setString(2, player);
			statement.execute();
			int count = statement.getUpdateCount();
			statement.close();
			
			if (count == 0)
			{
				return false;
			}
		}
		catch (SQLException se)
		{				
			return false;
		}
	}
	return true;
}
 
Example 17
Project: L2J-Global   File: AdminTeleport.java   Source Code and License Vote up 5 votes
private void changeCharacterPosition(L2PcInstance activeChar, String name)
{
	final int x = activeChar.getX();
	final int y = activeChar.getY();
	final int z = activeChar.getZ();
	try (Connection con = DatabaseFactory.getInstance().getConnection())
	{
		final PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=? WHERE char_name=?");
		statement.setInt(1, x);
		statement.setInt(2, y);
		statement.setInt(3, z);
		statement.setString(4, name);
		statement.execute();
		final int count = statement.getUpdateCount();
		statement.close();
		if (count == 0)
		{
			activeChar.sendMessage("Character not found or position unaltered.");
		}
		else
		{
			activeChar.sendMessage("Player's [" + name + "] position is now set to (" + x + "," + y + "," + z + ").");
		}
	}
	catch (SQLException se)
	{
		activeChar.sendMessage("SQLException while changing offline character's position");
	}
}
 
Example 18
Project: uroborosql   File: AuditLogSqlFilter.java   Source Code and License Vote up 5 votes
@Override
public int[] doBatch(final SqlContext sqlContext, final PreparedStatement preparedStatement, final int[] result) {
	String userName = getParam(sqlContext, USER_NAME_KEY);
	if (userName == null) {
		// ユーザ名が設定されていない時
		userName = DEFAULT_USER_NAME;
	}

	String funcId = getParam(sqlContext, FUNC_ID_KEY);
	if (funcId == null) {
		// 機能IDが設定されていない時
		funcId = DEFAULT_FUNC_ID;
	}
	int rowCount = -1;
	if (LOG.isDebugEnabled()) {
		try {
			rowCount = preparedStatement.getUpdateCount();
		} catch (SQLException ex) {
			// ここでの例外は実処理に影響を及ぼさないよう握りつぶす
		}
	}
	LOG.debug(ToStringBuilder.reflectionToString(
			new AuditData(userName, funcId, sqlContext.getSqlId(), sqlContext.getSqlName(), sqlContext
					.getExecutableSql(), rowCount), ToStringStyle.JSON_STYLE));

	return result;
}
 
Example 19
Project: L2jBrasil   File: AdminBanChat.java   Source Code and License Vote up 4 votes
private void banChatOfflinePlayer(L2PcInstance activeChar, String name, int delay, boolean ban)
{
	Connection con = null;
	int level = 0;
	long value = 0;
	if(ban)
	{
		level = L2PcInstance.PunishLevel.CHAT.value();
		value = (delay > 0 ? delay * 60000L : 60000);
	}
	else
	{
		level = L2PcInstance.PunishLevel.NONE.value();
		value = 0;
	}
	
	try
	{
		con = L2DatabaseFactory.getInstance().getConnection();
		
		PreparedStatement statement = con.prepareStatement("UPDATE characters SET punish_level=?, punish_timer=? WHERE char_name=?");
		statement.setInt(1, level);
		statement.setLong(2, value);
		statement.setString(3, name);
		
		statement.execute();
		int count = statement.getUpdateCount();
		statement.close();
		
		if (count == 0)
			activeChar.sendMessage("Character not found!");
		else
			if(ban)
				activeChar.sendMessage("Character " + name + " chat-banned for " + (delay > 0 ? delay + " minutes." : "ever!"));
			else
				activeChar.sendMessage("Character " + name + "'s chat-banned lifted");
	}
	catch (SQLException se)
	{
		activeChar.sendMessage("SQLException while chat-banning player");
		if (Config.DEBUG)
			se.printStackTrace();
	}
	finally
	{
		L2DatabaseFactory.close(con);
	}
}