Data saving to MySQL!

Discussion in 'Plugin Development' started by mateuszhp, Jan 14, 2013.

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

    mateuszhp

    Hi! I wrote a plugin that after killing another player saves it to a MySQL database. Plugin writes 3 different values​​, murder, death, and experience points. Unfortunately, with 30 players online server lag gets. I do not know how to optimize.
    Code:
    public static void dodajzabojstwo(String s) {
           
              try {
              Connection conn = DriverManager.getConnection(url + baza, login, password);
              conn.setAutoCommit(false);
              System.out.println("Połączenie ustanowione");
              Statement st = conn.createStatement();
              try {
                  String sql = "UPDATE gracze SET zabojstw=zabojstw+1 WHERE `nick`='"+s+"';";
                  String sql2 = "UPDATE gracze SET elo=elo+10 WHERE `nick`='"+s+"';";
                  st.executeUpdate(sql);
                  st.executeUpdate(sql2);
     
              } catch (Exception e) {
                  System.out.println(e);
              }
              } catch (SQLException e) {
              System.out.println("Uwaga! Mamy problemy z połączeniem!");
              }
            }
     
  2. Offline

    Lolmewn

    You could do it in a separate thread.
    Also, am I missing a .commit() statement somewhere?
     
  3. Offline

    fireblast709

    to elaborate on that: a async scheduled task. Also PreparedStatements should optimize it a bit
     
  4. Offline

    Lolmewn

    Yes. Async.
    Are PreparedStatements faster than normal Statements? :O
     
  5. If you're going to be executing a lot of the same statement then yes. And by a lot I am talking 100+ in under an hour then yes it would be sensible to use prepared statements as it would speed things up a little. Other than that I wouldn't worry about it too much.
     
  6. Offline

    Lolmewn

    Oh, okay. Thanks for the information. I guess I have some rewriting to do :)
    100 in under an hour? I get about 100 every minute :p
     
  7. Offline

    mateuszhp

    Not understand what I do, could you give me examples of fast record? As for examples.
     
  8. Lolmewn
    I think this should help clear things up :)

    Quoted from StackOverflow
     
    Lolmewn likes this.
  9. Offline

    mateuszhp

    This is my code after your advice but it is better, but now at 50 people a timeout, or lag overall.
    Code:
        public static void dodajzabojstwo(String s) {
           
     
              try {
              Connection conn = DriverManager.getConnection(url + baza, login, password);
              conn.setAutoCommit(false);
              System.out.println("Połączenie ustanowione");
              Statement st = conn.createStatement();
              PreparedStatement pstmt = conn.prepareStatement("UPDATE gracze SET zabojstw=zabojstw+1 WHERE `nick`='"+s+"';");
              pstmt.execute();
              PreparedStatement pstmt1 = conn.prepareStatement("UPDATE gracze SET elo=elo+10 WHERE `nick`='"+s+"';");
              pstmt1.execute();
              } catch (SQLException e) {
              System.out.println("Uwaga! Mamy problemy z połączeniem!");
              }
            }
        public static void dodajelo(String s) {
            try {
                  Connection conn = DriverManager.getConnection(url + baza, login, password);
                  conn.setAutoCommit(false);
                  System.out.println("Połączenie ustanowione");
                  Statement st = conn.createStatement();
             
                  PreparedStatement pstmt1 = conn.prepareStatement("UPDATE gracze SET elo=elo+10 WHERE `nick`='"+s+"';");
                  pstmt1.execute();
                  } catch (SQLException e) {
                  System.out.println("Uwaga! Mamy problemy z połączeniem!");
                  }
            }
        public static void minuselo(String s) {
            try {
                  Connection conn = DriverManager.getConnection(url + baza, login, password);
                  conn.setAutoCommit(false);
                  System.out.println("Połączenie ustanowione");
                  Statement st = conn.createStatement();
                  PreparedStatement pstmt = conn.prepareStatement("UPDATE gracze SET elo=elo-5 WHERE `nick`='"+s+"';");
                  pstmt.execute();
                  } catch (SQLException e) {
                  System.out.println("Uwaga! Mamy problemy z połączeniem!");
                  }
            }
     
Thread Status:
Not open for further replies.

Share This Page