Bit of feedback on my code

Discussion in 'Plugin Development' started by olihough, Jan 27, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    olihough

    I'm playing around writing an economy plugin just for the fun of it really.
    The plugin only uses mysql for its data store and right now all it does is check if the player has a bank account and if not it creates one (all done silently on logon). I don't want the plugin to depend on anything (so no vault etc)

    My question is am I doing things efficiently so far or have I accidently taken a long way round achieving a simple task?

    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2.  
    3. private static Connection connection;
    4.  
    5. @Override
    6. public void onEnable() {
    7. getServer().getPluginManager().registerEvents(this, this);
    8. }
    9.  
    10. @Override
    11. public void onDisable() {
    12. try {
    13. if(connection != null && !connection.isClosed()){
    14. connection.close();
    15. }
    16. } catch (SQLException e) {
    17. e.printStackTrace();
    18. }
    19. }
    20.  
    21. public synchronized static void openConnection() {
    22. try{
    23. connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/Minecraft", "xxxxx","xxxxx");
    24. }catch(Exception e) {
    25. e.printStackTrace();
    26. }
    27. }
    28. public synchronized static void closeConnection() {
    29. try{
    30. connection.close();
    31. }catch(Exception e) {
    32. e.printStackTrace();
    33. }
    34. }
    35.  
    36. public synchronized static boolean doesPlayerHaveAccount(Player player){
    37. try{
    38. PreparedStatement sql = connection
    39. .prepareStatement("SELECT account_id FROM wb_accounts WHERE account_owner=?;");
    40. sql.setString(1, player.getName());
    41. ResultSet resultSet = sql.executeQuery();
    42. boolean hasAccount = resultSet.next();
    43.  
    44. sql.close();
    45. resultSet.close();
    46.  
    47. return hasAccount;
    48. }catch(Exception e) {
    49. e.printStackTrace();
    50. return false;
    51. }
    52. }
    53.  
    54. @EventHandler
    55. public void onPlayerLogin(PlayerLoginEvent event) {
    56. openConnection();
    57. try {
    58. if(!doesPlayerHaveAccount(event.getPlayer())){
    59. PreparedStatement newAccount = connection.prepareStatement("INSERT INTO wb_accounts (account_owner, account_balance) VALUES(?,500);");
    60. newAccount.setString(1, event.getPlayer().getName());
    61. newAccount.execute();
    62. newAccount.close();
    63. }
    64. }catch(Exception e) {
    65. e.printStackTrace();
    66. }finally {
    67. closeConnection();
    68. }
    69. }
    70.  
    71. }
     
  2. Offline

    Tirelessly

    olihough You shouldn't open and close the connection for every interaction. You should open it once and close it once.
     
  3. Offline

    igwb

    Tirelessly I'm not quite sure about that. It could get you some ugly results if the server crashes. I'm pretty sure that opening it when you need is is the safer way. I'm not realy a database expert though, so I'm interested in advise on this to.
    (But it could be worse, if this was sqlite.)
     
  4. Offline

    sebasju1234

    I am doing this and it works great. ;)
     
  5. Offline

    olihough

    Well mysql will force close a connection after a while anyway, best practice would state that you should close your connections after you're done simply because theres no need to leave it open. On the other hand we are not talking about 1000's of users hitting the db so its not going to make a blind bit of difference.
    I appreciate the comments, if theres anything else to comment on I'd love to hear it, thanks guys
     
Thread Status:
Not open for further replies.

Share This Page