MySQL connection ploblem

Discussion in 'Plugin Development' started by hej, Jan 19, 2011.

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

    hej

    Hi, I have been looking around to see if i can find a way to connect to my database and retrieve some information.
    I did get some good code examples but when I sum them together it doesen't seem to work.
    Can you figure out what I'm doing wrong?

    Code:
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con = DriverManager.getConnection("jdbc:mysql://" + "localhost", "root", "password"); //con is a nill value of type "Connection"
    PreparedStatement prepareStatement = con.prepareStatement("use " + "hello");
    prepareStatement.executeQuery();
    Note: I have added the mysql connector as a reference from their official site.
     
  2. Offline

    apexearth

    How does it not work? Does it error?
     
  3. Offline

    Affecting_Nelas

    Hi, my guess is you forgot to connect to a DB:
    String userName = "testuser";
    String password = "testpass";
    String url = "jdbc:mysql://localhost/test"; // connection on Localhost and DB test
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection (url, userName, password);
     
  4. Offline

    hej

    Darn! I knew it was something I forgot to include!
    Okay, now the Class.forName line gives me a "ClassNotFoundExeption", a "IllegalAccessExeption" and a "InstantiationExeption" and the other three lines gives me a "SQLExeption" probably because of the error in the first line.

    Well, isn't that code doing exactly the same as mine?
     
  5. Offline

    Affecting_Nelas

    no

    Code:
    con = DriverManager.getConnection("jdbc:mysql://" + "localhost", "root", "password")
    
    there is no "/DBname" (test) at his code

    or i don't see it there.

    Code:
    String userName = "testuser";
    String password = "testpass";
    String url = "jdbc:mysql://localhost/---->!!!!test<----"; // <<<---- there is something diffrent about that i guess
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection (url, userName, password);
    behind the localhost should be the name of the database you are trying to connect to.
    else the connection wouldn't make any sense at alle because a DBserver can hold many
    DBs for a variaty of users.
     
  6. Offline

    hej

    Ahh, okay, I didn't really notice that, sorry. [​IMG]
    Anyways, I decided to make a plugin that connects to a C# program through a TCP connection, that way I get to work more with a language and IDE I'm familiar with.
    It might not be to efficient, but it will be integrated into my server management application so it doesn't really matter.
    So thank you for your time and help, and cya later!
     
  7. Offline

    Affecting_Nelas

    can happen to anyone :)
    i hope you will be successful at making a great plugin :)
    cya
     
Thread Status:
Not open for further replies.

Share This Page