SQL in Plugins

Discussion in 'Plugin Development' started by chasechocolate, Feb 2, 2013.

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

    chasechocolate

    Assuming you know basic SQL syntax, I wrote up a method to run SQL queries Bukkit plugins. If you don't know, I recommend watching thenewboston's tutorial series.
    Code:java
    1. public void executeQuery(String username, String password, String databaseHost, String databaseName, int port, String query){
    2. Connection conn;
    3. String url = "jdbc:mysql://" + databaseHost + ":" + port + "/" + databaseName;
    4.  
    5. //Attempt to connect
    6. try{
    7. //Connection succeeded
    8. conn = DriverManager.getConnection(url, username, password);
    9. PreparedStatement statement = conn.prepareStatement(query);
    10. statement.executeQuery();
    11. } catch(Exception e){
    12. //Couldn't connect to the database
    13. }
    14. }

    To use this, just put it in a class and execute the query! If you want to do it from different classes, just add the static modifier to the method to make it easier. Hope you use this!
     
  2. Offline

    chris4315

    How would I execute a query in another class?

    Thank you so much! You're awesome btw.
     
  3. Offline

    chasechocolate

    Either create a constructor or make that method static. If you do a constructor, it would look something like this:
    Code:java
    1. plugin.executeQuery(...);

    And making the method static would look like this:
    Code:java
    1. <mainclass>.executeQuery(...);
     
  4. Offline

    chris4315

    Right right. Thanks :D
    So the parameters for the execution will be like <mainclass>.executeQuery(etc, etc)?
     
  5. Offline

    chasechocolate

    Yes, if the method is static.
     
  6. Offline

    Giant

    You realize you are creating a new database connection on every call to the method, whilst never closing of the created connections?
     
  7. Offline

    chasechocolate

    Yes, I do. This is just as an example, if a class already had an established connection, then they can run the queries directly from that. This is just a bunch of stuff (that normally would go in different methods) put into one method as an example.
     
  8. I think I will learn mySQL when I am finished with Ruby.
     
  9. Offline

    Mother__

    hey brother not working i trying

    Code:java
    1.  
    2. public void executeQuery(String username, String password, String databaseHost, String databaseName, int port, String query){
    3. String url = "jdbc:mysql://" + databaseHost + ":" + port + "/" + databaseName;
    4.  
    5. //Attempt to connect
    6. try{
    7. //Connection succeeded
    8. conn = DriverManager.getConnection(url, username, password);
    9. PreparedStatement statement = conn.prepareStatement(query);
    10. statement.executeQuery();
    11. } catch(Exception e){
    12. System.out.println("Error update MySQL");
    13. }
    14. }
    15.  
    16. @EventHandler
    17. public void onPlayerJoin(PlayerJoinEvent event){
    18. Total ++;
    19. Player player = event.getPlayer();
    20. executeQuery("root", "1245", "localhost", "minecraft", 3306,"INSERT INTO onplayers ('name') VALUES ('" + player.getName() + "');");
    21.  
    22. }
    23.  

    but no have errors and no update in my database minecraft and not insert name in onplayers..

    sorry my bad english i from brazil

    help me please!!

    oh guys please help me =(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  10. Offline

    Mango

    chasechocolate
    Woah, woah, woah. Bad, bad bad.

    Make a class for SQL managing that connects once at startup, instead of connecting for every query.

    Seriously, connecting to a MySQL server (even on localhost) can take up to 100 ms, which is actually pretty significant. You also didn't close your connections (very hard on the server), which will just make this delay snowball over time.

    With a manager class, you can also hardcode the server information there instead of using them as arguments every time. Much better practice and much cleaner code.
     
  11. Offline

    chasechocolate

    Mango Yeah I know... I was trying to compact it into one method. if I get some time, I might split that up in to two or three methods, for establishing the connection, executing queries, etc.
     
  12. Offline

    Mother__

    please someone help me i try
    Code:java
    1.  
    2. public void executeQuery(String username, String password, String databaseHost, String databaseName, int port, String query){
    3. String url = "jdbc:mysql://" + databaseHost + ":" + port + "/" + databaseName;
    4.  
    5. //Attempt to connect
    6. try{
    7. //Connection succeeded
    8. conn = DriverManager.getConnection(url, username, password);
    9. PreparedStatement statement = conn.prepareStatement(query);
    10. statement.executeQuery();
    11. } catch(Exception e){
    12. System.out.println("Error update MySQL");
    13. }
    14. }
    15.  
    16. @EventHandler
    17. public void onPlayerJoin(PlayerJoinEvent event){
    18. Total ++;
    19. Player player = event.getPlayer();
    20. executeQuery("root", "1245", "localhost", "minecraft", 3306,"INSERT INTO onplayers ('name') VALUES ('" + player.getName() + "');");
    21.  
    22. }
    23.  


    but not insert
     
  13. Offline

    caldabeast

    I could never get the Java SQL drivers to work, so I got my friend with a website to make me a set of pages which I could send web requests to to save and read the data with. Quite effective of a work around, if you ask me!
     
  14. Offline

    chris4315

    I fucking hate life. Tried at least 15 things and I don't understand nothing works!!!!!!!!!!
     
Thread Status:
Not open for further replies.

Share This Page