#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2001
    Location
    New Zealand
    Posts
    638
    Rep Power
    14

    JDBC : MySQL Connection


    Greetings All,

    I'm trying to use the mm.mysql.Driver to connect from JDBC to MySQL. I load the driver okay, but then get the following error message:

    PHP Code:
    SQLExceptionCannot connect to MySQL Server on localhost:3306  Is there a MySQL Server running on the machine/port you are trying to connect to
    What port should I be connecting on? Do I need to configure JDBC or MySQL in someway? Any suggestions are very welcome.

    Thanks,
    Z.
  2. #2
  3. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    How are you trying to connect (i.e. what commands are you using)?
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2001
    Location
    New Zealand
    Posts
    638
    Rep Power
    14
    Greetings All,

    I've included my basic class below, but I know it works because I tried it locally! So if anyone's want to know how to connect MySQL with JDBC here's some basic code, I'm using the MM driver, which you need to install and copy to the /jdk/jre/lib/ext directory. My problem is definately from the MySQL end, I cannot get a remote connection going and I really need to

    Any help is appreciated,
    Z.


    java class connect
    (I get the error just after connecting to MySQL hits the screen)

    PHP Code:

    import java
    .sql.*;

    public class 
    connect
    {
        public static 
    void main(String[] Args)
        {
              
    System.out.println("Loading Driver.");
               try {
                      
              Class.
    forName("org.gjt.mm.mysql.Driver").newInstance();
            }
              catch (
    Exception E) {
              
    System.err.println("Unable to load driver.");
              
    E.printStackTrace();
            }

            try {

        
    String url="jdbc:mysql://localhost/weblearn";
       
    System.out.println("Connecting to MySQL.");
       
    Connection Conn DriverManager.getConnection (url"root""dalek$");
               
    System.out.println("connected!");
             
    Statement Stmt Conn.createStatement();
    ResultSet RS Stmt.executeQuery("SELECT Name from test");
               while (
    RS.next()) {
                     
    System.out.println(RS.getString(1));
               }

            
    // Clean up after ourselves
               
    RS.close();
               
    Stmt.close();
               
    Conn.close();
               }
                catch (
    SQLException E) {
                  
    System.out.println("SQLException: " E.getMessage());
                  
    System.out.println("SQLState:     " E.getSQLState());
                  
    System.out.println("VendorError:  " E.getErrorCode());
                }
     }


  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Singapore
    Posts
    3
    Rep Power
    0
    Do you set up user on mysql table?

    If so you may need to use
    "jdbc:mysql://localhost/databasename?user=username"
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    NE Pa.
    Posts
    96
    Rep Power
    14

    NOt sure but this might help


    I think your problem may be with the way your presenting the connection to the db....I had quite a few problems when i first started but then settled on this config & havent had a problem since....of course this is single connection stuff which should be avoided in favor of connection pooling if you expect mulitple db connections. Anyways, here's how I do it:

    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    conn = DriverManager.getConnection("jdbc:mysql://localhost/[dbName]?user=[dbUserName]&password=[dbPassWord]");
    Statement stmt = conn.createStatement();

    Hope this helps

    Also your gonna have to catch 3 exceptions here or the compiler screams. They are InstantiationException, ClassNotFoundException & IllegalAccessException

IMN logo majestic logo threadwatch logo seochat tools logo