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

    RE3ELL

    Hi,
    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:
    http://pastebin.com/HC8vn7Tj

    My plugin.yml:
    Code:
    name: OnServerList
    version: 1.0
    main: src.Cakestory.OnServerList.main.OnServerList
    author: Cakestory
    website: http://www.heuschober-server.de
    depend: [SQLibrary]
    description: An easy to use MySQL based OnlinePlayersList for server homepages.
    commands:
      refreshlist:
          description: Manually refresh the MySQL List
          usage: /refreshlist           
    permissions:
        osl.*:
            description: Access to everything
            children:
                osl.refresh: true
        osl.refresh:
            description: Refresh the playerlist
            default: op                
    My config:
    Code:
    # 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
    MySQL:
      Hostname: 'localhost'
      Password: 'mypassword'
      Port: 3306
      User: 'username'
      Database: 'databasename'
     
  2. Offline

    slayr288

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

    RE3ELL

    I have tried it like this:
    Code:java
    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

    kreashenz

    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

    RE3ELL

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

    CubieX

    "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 java.sql.xxx)
     
  7. Offline

    AmShaegar

    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.

    http://pastebin.com/wQ3Ju81r
     
  8. Offline

    RE3ELL

    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:
    http://pastebin.com/7U8rwGS5

    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

    slayr288

    RE3ELL
    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:
    Code:
    jdbc:mysql://localhost:3306/minecraft
    Here's an example with what you need to replace:
    Code:
    jdbc:mysql://<ip/domain>:<port>/<database>
    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