I'm working on a spigot plugin, that deals with messages to from players on a server to the admin's. It's the same plugin this question relates to: SQL structure to save coordinates for spigot plugin . I've got a command, which allowes access to the inbox, and does all kinds of operations on the data. One of these operations is deleting an entry by it's ID.
I've got a simple SQL query to delete it, and want to display a message, depending on if the message could be deleted or not. Therefore I wrote this method in my Command-Class:
private void deleteMessage(CommandSender sender, int messageId) {
int deleted = DatabaseMethods.deleteMessage(messageId);
if (deleted == 0) {
sender.sendMessage(MessageManager.couldNotBeFound(messageId));
} else {
sender.sendMessage(MessageManager.deletedMessage(messageId));
}
}
and this is the method that accesses the database:
public static int deleteMessage(int messageId) {
try (final PreparedStatement ps = BasePluginAPI.getDatabaseConnection()
.prepareStatement("delete from MELDUNG where ID = ?")) {
ps.setInt(1, messageId);
return ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
I don't know if this is the best way to approach it. I have two main questions:
- Is an early return better for the first method? Or is it just a question of personal taste?
- Can the SQL-handling be improved? Should maybe the method return a
boolean
instead of anint
?
Besides these specific questions, of course general feedback is very welcome as well! :)
else
clause, just insert areturn;
after sending thecouldNotBeFound
-Message, and putting the second message after theif
\$\endgroup\$ID
, ormessageId
is the primary key of my database table \$\endgroup\$