Skip to content

Java Function always returns 0

Featured Replies

code:

	public int getLastGotKill()
{
	Connection con = null;
	int last_pvp_time = 0;
	try
	{
		con = L2DatabaseFactory.getInstance().getConnection(false);
		PreparedStatement statement;
		statement = con.prepareStatement("select last_got_kill from characters where obj_Id=?");
		statement.setInt(1, getObjectId());

		ResultSet rset = statement.executeQuery();
		while(rset.next())
		{
			last_pvp_time = rset.getInt("last_got_kill");
			_log.info("last pvp time is : " + last_pvp_time);
		}
		rset.close();
		rset = null;
		statement.close();
		statement = null;
	}
	catch(Exception e)
	{
		if(Config.ENABLE_ALL_EXCEPTIONS)
			e.printStackTrace();
	}
	finally
	{
		CloseUtil.close(con);
		con = null;
	}
	return last_pvp_time;
}

 

why does this return always 0 ? i am sure that the column last_got_kill is not zero for the selected player

Bhe my bad,you used other name for initial variable.

 

Cleaned version is (java 7)

 

	public int getLastGotKill()
{
	int lastGotKill = 0;
	try (Connection con = L2DatabaseFactory.getInstance().getConnection(false))
	{
		PreparedStatement statement = con.prepareStatement("SELECT last_got_kill FROM characters WHERE obj_Id=?");
		statement.setInt(1, getObjectId());

		ResultSet rset = statement.executeQuery();
		while(rset.next())
		{
			lastGotKill = rset.getInt("last_got_kill");
			_log.info("lastGotKill is : " + lastGotKill);
		}
		rset.close();
		statement.close();
	}
	catch(Exception e)
	{
		if(Config.ENABLE_ALL_EXCEPTIONS)
			e.printStackTrace();
	}
	return lastGotKill;
}

 

About your problem, do you see the log.info while executing that method ? Are you SO SURE about the variable being different of 0 ? If you edited at hand you have to save the change or if you used a query to save it, the query has to be correct.

 

Are you sure you use getLastGotKill() on the good instance ? (killed != killer).

Are you sure last_got_kill (on your sql) is an int type ?

  • Author

so i fixed it by changing getInt() to getLong() and last_got_kill variable type from int to long.

Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.