Again MySQL question..

Discussion in 'Plugin Development' started by Adriani6, Feb 7, 2014.

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

    Adriani6

    I have two classes from which I would like to pass parameters.
    I know how to do so but my problem is quite difficult for me to resolve it ;p

    my main.java
    Code:java
    1. ackage adriani6.co.uk.ai6ms;
    2.  
    3. import org.apache.commons.lang.time.FastDateFormat;
    4. import org.bukkit.Bukkit;
    5.  
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.PlayerDeathEvent;
    11. import org.bukkit.event.player.PlayerJoinEvent;
    12. import org.bukkit.event.player.PlayerQuitEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.scheduler.BukkitScheduler;
    15.  
    16.  
    17. import java.io.File;
    18. import java.io.InputStream;
    19. import java.sql.Connection;
    20. import java.sql.DatabaseMetaData;
    21. import java.sql.ResultSet;
    22. import java.sql.SQLException;
    23. import java.sql.Statement;
    24. import java.util.Date;
    25. import java.util.HashMap;
    26.  
    27.  
    28. public class main extends JavaPlugin implements Listener{
    29.  
    30.  
    31. private main plugin;
    32.  
    33. public void Define(main plugin){
    34. this.plugin = plugin;
    35.  
    36. }
    37.  
    38. //Connect to MySQL
    39. MySQL MySQL = new MySQL(plugin, "localhost", "3306", "ai6ms", "root", "password");
    40. Connection c = null;
    41.  
    42. //onEnable
    43. public void onEnable(){
    44.  
    45. getLogger().info("Plugin Enabled!");
    46.  
    47. c = MySQL.openConnection();
    48.  
    49.  


    As you can see, I have my MySQL connect info outside any method and it's just lying in the "main" class since I have another 5 methods within thic class file, all those methods use the MySQL connect info.

    Now I have another class file called Config.class which handles my Configuration file.

    Code:java
    1. package adriani6.co.uk.ai6ms;
    2.  
    3. import java.io.File;
    4. import java.io.InputStream;
    5.  
    6. import org.bukkit.configuration.file.FileConfiguration;
    7.  
    8.  
    9. public class Config extends main{
    10.  
    11.  
    12.  
    13. public void Configurator(){
    14.  
    15. FileConfiguration config = null;
    16. try{
    17. config = getConfig();
    18. File AI6MS = new File (getDataFolder() + "config.yml");
    19. if(!config.contains("mysql.connect.host")){
    20. config.set("mysql.connect.host", "localhost");
    21. }
    22. if(!config.contains("mysql.connect.port")){
    23. config.set("mysql.connect.port", 3306);
    24. }
    25. if(!config.contains("mysql.connect.db")){
    26. config.set("mysql.connect.db", "nameOfYourDatabase");
    27. }
    28. if(!config.contains("mysql.connect.user")){
    29. config.set("mysql.connect.user", "yourUsername");
    30. }
    31. if(!config.contains("mysql.connect.password")){
    32. config.set("mysql.connect.password", "yourPassword");
    33. }
    34.  
    35. saveConfig();
    36.  
    37. }catch(Exception e1){
    38. e1.printStackTrace();
    39. }
    40. }
    41. }
    42.  

    Works perfectly, however, If I wanted to get values for MySQL connect info from config, how would I do so ?

    I know about
    Code:java
    1. config.getString("mysql.connect.host");

    But this only work within the Config.java....

    Is it even possible to do it the way I'm doing it ?

    That didn't work .0

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page