[Solved]Help with adding player name to database

Discussion in 'Plugin Development' started by herghost, Nov 15, 2011.

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

    herghost

    Hi all

    I am having some problems with getting a players name on login and entering into the database.

    This is what I have (snippets)

    PlayerListener:

    Code:
    public void onPlayerJoin(PlayerJoinEvent event)
        {
            try
            {
                sqlFunctions method = new sqlFunctions();
                method.add_user();
    
            }
    
        catch
            (SQLException e1)
                {
                    e1.printStackTrace();
                }
    
        }
    and my add_user method from sqlFunctions class

    Code:
    public void add_user() throws SQLException
        {
            Connection conn = DriverManager.getConnection(url, user, pass);
            PreparedStatement sampleQueryStatement = conn.prepareStatement("INSERT INTO users (p_name,p_ip) VALUES (" + event.getPlayer().getName() + ",1)");
            sampleQueryStatement.executeUpdate();
            sampleQueryStatement.close();
            conn.close();
        }
    My problem is I am not sure how to get my player name?
    Logic tells me it should be with this code 'event.getPlayer().getName()' in the listener, but I am not sure how to pass the string into my method?

    I think this is a gap in my java knowledge more than anything, I am attempting the learn by doing method for both more advanced java and bukkit!

    Much thanks :)

    Ok,

    well I have now got to

    Code:
    public void onPlayerJoin(PlayerJoinEvent event)
        {
            String thisplayer = event.getPlayer().getName();
    
            try
            {
    
                    Connection conn = DriverManager.getConnection(url, user, pass);
                    PreparedStatement sampleQueryStatement = conn.prepareStatement("INSERT INTO users (p_name,p_ip) VALUES (" + thisplayer + ",1)");
                    sampleQueryStatement.executeUpdate();
                    sampleQueryStatement.close();
                    conn.close();
     
            }
    
        catch
            (SQLException e1)
                {
                    e1.printStackTrace();
                }
      
        }
    Which correctly sets the player name, however I get a mysql error that the players name is not in the field list, surely this should be a value!

    D'oh moment :p

    Code:
    PreparedStatement sampleQueryStatement = conn.prepareStatement("INSERT INTO users (p_name,p_ip) VALUES ('" + thisplayer + "','1')");
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
Thread Status:
Not open for further replies.

Share This Page