Library MySQL Connection class

Discussion in 'Resources' started by HungerCraftNL, May 12, 2016.

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

    HungerCraftNL

    Hi guys,

    I've made a Connection class a few days ago, and I thought why shouldn't I share it with you guys. It's pritty easy to use if you have some experience with Java and SQL. I'll post a small explaination bellow.

    Link: https://gist.github.com/Shadow48402/897818db1d4372602f769fdb0c15a01e

    Making connection
    Code:
    private Connection con;
    
    public void onEnable()
    {
      String hostname = "localhost";
      String port = "3306";
      String username = "root";
      String password = "root";
      String database = "database";
    
      this.con = new Connection(hostname, port, username, password, database);
      this.con.connect();
    }
    
    public Connection getConnection()
    {
      return this.con;
    }
    
    Executing select example
    Code:
    Player p = (Player) sender;
    ArrayList<Object> objs = new ArrayList<Object>();
    objs.add(p.getUniqueId().toString());
    ResultSet rs = plugin.getConnection().preparedStatement("SELECT * FROM users WHERE player_uuid=?", objs);
    while(rs.next())
    {
      p.sendMessage("Current balance: " + rs.getDouble("balance"));
    }
    Executing insert example
    Code:
    Player p = (Player) sender;
    String uuid = p.getUniqueId().toString();
    String item = "blabla";
    plugin.getConnection().updateQuery("INSERT INTO sells (player_uuid, item) VALUES ('"+uuid+"', '"+item+"')");
    
    Have fun with it, if you have any questions I would love to answer them <3
     
  2. Offline

    EmeraldRailsMC

    Do you have to put the password in a plain text string?
     
  3. Offline

    FisheyLP

    You can change permissions to only let the user root read from the database and not write
     
Thread Status:
Not open for further replies.

Share This Page