Solved SQL bug.

Discussion in 'Plugin Development' started by es359, Oct 25, 2014.

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

    es359

    So during my testing of one of my Plugins using SQL, I encountered an annoying bug. I'm sure it's a simple fix, however I am a little new to SQL.

    The function of this section of the plugin is to log the player's chat messages. This is successful except for my little problem, and I think it might be due to how modified something.

    Here is my error:
    [​IMG]







    You can see at the very top of the stacktrace at 10:59:24 where the chat was logged successfully.
    However at the bottom of the stacktrace you can see the message that started causing the problem.
    It starts when I use " ' " in any chat message.

    Here is my chat table. You can see that the chat message at 10:59:50 was not logged.
    [​IMG]

    The stacktrace is indicating an error at line 57 of my Functions.java class, (I wrote a custom method for logging the chat) Here is that section of code:

    Code:java
    1. public void logPlayerChat(Player p,String msg, String sql){
    2.  
    3. String name = p.getName();
    4. String ip = "" +p.getAddress();
    5. UUID uuid = p.getUniqueId();
    6.  
    7. sql = sql.replaceAll("%name%", p.getName());
    8. sql = sql.replaceAll("%ip%",""+ p.getAddress());
    9. sql = sql.replaceAll("%uuid%", "" +p.getUniqueId());
    10. sql = sql.replaceAll("%msg%", msg);
    11. sql = sql.replaceAll("%date%", "" +ts.stamp); //
    12. try {
    13. main.sql.c.prepareStatement(sql).executeUpdate(); //Line 57 is here.
    14. Bukkit.getServer().getConsoleSender().sendMessage("Logged the chat for " +p.getName());
    15. //LogToConsole(sql_prefix +" &7Logged the chat for &bplayer, &8"+name);
    16. }catch (SQLException e) {
    17. e.printStackTrace();
    18. }
    19. }


    Now on my Main class I have my chat Listener. Here is where I call this method:

    Code:java
    1. @EventHandler
    2. public void onPlayerChat(AsyncPlayerChatEvent event) {
    3. Player p =event.getPlayer();
    4.  
    5.  
    6. f.logPlayerChat(p,event.getMessage(), "INSERT INTO chat (name, UUID, chat, stamp) VALUES ('%name%', '%uuid%', '%msg%', '%date%' );");
    7.  
    8. }


    If I missed anything let me know.

    Thanks for any help that can be provided.
     
  2. Offline

    hexaan

    If you would use the Named Parameters in your prepared statements instead of using String.replaceAll() it would solve the problem.
     
    es359 likes this.
  3. Offline

    Lolmewn

    No, no, no!
    Please, do use a PreparedStatement object. Like this:
    Code:
    PreparedStatement st = yourConnection.prepareStatement("INSERT INTO chat(name, UUID, chat, stamp) VALUES (?, ?, ?, ?)");
    st.setString(1, name);
    // Your other thingies
    st.execute();
    st.close();
     
    ferrybig and es359 like this.
  4. Offline

    es359


    So...

    Code:java
    1. PreparedStatement myStatement = conection.prepareStatement("INSERT INTO chat(name, UUID, chat, stamp) VALUES (?, ?, ?, ?)");
    2.  
    3. myStatement.setString(1, p.getName());
    4. myStatement.setString(2,p.getUUID());
    5. myStatement.setString(3, p.getMessage());
    6. myStatement.setString(4, timestamp);
    7.  
    8. myStatement.execute();
    9. st.close();


    That's what I originally thought. I will test this out.

    Thanks to you both, this has been resolved! Lolmewn hexaan

    Lolmewn Do you have an recourses for learning SQL that you can suggest to me? Books, Docs?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  5. Offline

    Lolmewn

    I did most of it following the MySQL references, actually.
    Here's a nice reason why to use PreparedStatements (it's faster!).
    If you don't know something, Google is really your friend :) That's how I did it, really.
     
    es359 likes this.
  6. Offline

    es359

    Lolmewn Yeah google is amazing, I just didn't realize there was a more efficient way. :) Thanks!
     
  7. Offline

    hexaan

    Lolmewn
    Most of the books I have about SQL and Databases are in other languages. :p I would suggest you google MySQL tutrial and look for a good link! There is so much more that can be done with databases, but also so much that can go wrong. If you ever have a question about mysql just pm me and I'll see if I can help! :)
     
    es359 likes this.
  8. Offline

    Lolmewn

    hexaan Are you specifically addressing me or the OP here? :p
     
    es359 likes this.
  9. Offline

    hexaan

    Lolmewn Hahaha Sorry yeah I was trying to Tahg himXD
     
Thread Status:
Not open for further replies.

Share This Page