Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2003
    Posts
    2

    Unanswered: Problems with Connector/J,

    Hello people

    Im trying to run this simple program on Linux Red Hat 8.0
    #############################################
    import java.sql.*;
    // a simple java program to test how to access the db
    // by using JDBC

    public class MyJDBC {
    public static void main(String[] args) {
    try {
    // Load the JDBC driver for MySQL
    Class.forName("com.mysql.jdbc.Driver").newInstance ();

    // Connect to the DB using the above driver
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","");
    System.out.println("Connected");
    }catch( Exception ex)
    {
    ex.printStackTrace();
    System.out.println("Connection failed");
    }
    }
    }
    ################################################## ##

    I get this error message:

    java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.UnknownHostException: localhost
    at com.mysql.jdbc.Connection.createNewIO(Connection.j ava:1592)
    at com.mysql.jdbc.Connection.<init>(Connection.java:4 86)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:326)
    at java.sql.DriverManager.getConnection(DriverManager .java:512)
    at java.sql.DriverManager.getConnection(DriverManager .java:172)
    at MyJDBC.main(MyJDBC.java:13)
    Connection failed


    Any suggestions? I have also tried other urls, for example
    "jdbc:mysql://localhost/mysql?user=username&password=mypassword"
    but it gives me the same message
    Thanks for your help
    /Thomas

  2. #2
    Join Date
    Apr 2003
    Posts
    3

    Re: Problems with Connector/J,

    I'm just a beginner at this, but I've entered almost the exact same code as you posted and was able to compile and run via javac/java.

    Based on the error you included in your message:

    "java.net.UnknownHostException: localhost"

    I would have to ask the extremely stupid question of whether or not you have 'localhost' in your /etc/hosts file.

    Hope this helps...

    -Hani

    FYI: This is the code I have:

    =====================
    import java.sql.*;


    class DBTest
    {
    public static void main (String args[])
    {
    try {
    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    String url = "jdbc:mysql://localhost/<db>";
    Connection con = DriverManager.getConnection(url, "<user>", "<pass>");

    Statement stmt = con.createStatement();
    String query = "SELECT * FROM VERTICALS";
    ResultSet rs = stmt.executeQuery(query);
    while (rs.next()) {
    String s = rs.getString("vertical");
    int n = rs.getInt("id");
    System.out.println(s + " " + n);
    }
    } catch (Exception e)
    {
    }
    }
    }

  3. #3
    Join Date
    Apr 2003
    Posts
    2

    Thanks

    Thanks, the problem was something similar, i havent looked at
    etc/hosts but when i changed my computer name to the right
    name (ok, the bash looks like computername/directory but im
    not claiming to have a brain) and now it works. I guess
    your way would have worked to.

    Thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •