Plugin won´t connect to MySQL DB

Discussion in 'Plugin Development' started by RE3ELL, Jun 22, 2013.

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


    i try to create a plugin which writes a userstatus (online or offline) to a MySQL database, to read it out with php on my homepage.
    I´m using PatPeter´s SQLibrary.
    With SQLite, my plugin worked fine, but now when i try to use MySQL, it won´t connect to the database, also my data (Hostname,port,pw,...) are correct.
    When i try to use variables in the MySQL(Logger, prefix, host, ...) methode which get their values from my config.yml it just says in the console, that the fields i replaced with variables "cannot be null or empty!"
    When i tried it with hardcoding the values needed for the MySQL connection (replacing the variables with the exact values), it connects to the database, but it doesn´t seem to be able to write a new table to the database. (Writing is allowed on the MySQL server)
    I tried 2 differnt MySQL servers (from my Webspace and my gameserver) but both didn´t work.

    I hope you can help me.

    Here´s my code:

    My plugin.yml:
    name: OnServerList
    version: 1.0
    main: src.Cakestory.OnServerList.main.OnServerList
    author: Cakestory
    depend: [SQLibrary]
    description: An easy to use MySQL based OnlinePlayersList for server homepages.
          description: Manually refresh the MySQL List
          usage: /refreshlist           
            description: Access to everything
                osl.refresh: true
            description: Refresh the playerlist
            default: op                
    My config:
    # If something doesn´t work set this to true to get an error log in console.
    # This plugin only prints stacktrace in debugmode!
    Debug: false
    # Change the following to fit your needs
    # Default Hostname = "localhost" and default Port = 3306
      Hostname: 'localhost'
      Password: 'mypassword'
      Port: 3306
      User: 'username'
      Database: 'databasename'
  2. Offline


    Try using this method to write a new table:
                    try {
                        final Connection conn = DriverManager.getConnection(url, user, pass);
                        final PreparedStatement state = conn.prepareStatement("query");
                    } catch (final SQLException e) {
  3. Offline


    I have tried it like this:
    1. try {
    2. final Connection conn = DriverManager.getConnection("ip of my webspace", "username", "password");
    3. final PreparedStatement state = conn.prepareStatement("CREATE TABLE IF NOT EXISTS '" + db_table + "' (id INTEGER PRIMARY KEY AUTOINCREMENT, 'player' VARCHAR(16), 'online' BIT)");
    4. state.executeUpdate();
    5. state.close();
    6. conn.close();
    7. } catch (final SQLException e) {
    8. e.printStackTrace();
    9. }

    When i try to run the plugin, the console just says:
    "SQLException: No suitable driver found for ..."

    If this is the default way of communicating with a MySQL server in Java (Import java.sql.*), do i need something different to initialize the connection? Or is the code you gave me everything you need to query a MySQL database?
    As i said i´m using PatPeter´s SQLibrary.
  4. Offline


    Make sure you have a Database created called "databasename" otherwise you will get errors. I had this happen to me, and it worked fine after I changed the name of the database.
  5. Offline


    The database exists, with the given name. This doesn´t seem to be the problem.
  6. Offline


    "SQLException: No suitable driver found for ..."
    Can you please show us, which imports you are using?
    Perharps you have accidently used the wrong ones. (others than
  7. Offline


    I created a SQL helper class which uses DatabaseConfig. This may help you eventually. I just do: SQL.createDatabase(); to get a reference to a Database object.
  8. Offline


    Thanks AmShaegar. I´ll look into it later this week, as i don´t have time for programming atm.

    Here is my class with slayr288´s solution:

    The imports should be included now.

    I bet i used it wrong way but, i have nearly no experince in java with mysql. Maybe you could give me an example how to use it if i did it wrong
  9. Offline


    You haven't used a MySQL driver url, where I stated 'url'.
    You don't put the ip/domain there, you put the driver url, example:
    Here's an example with what you need to replace:
    I suggest creating a variable that is the url, so you don't need to keep on making it in every new connection.
Thread Status:
Not open for further replies.

Share This Page