Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

New to JDBC, Connection Error

843859Jul 26 2005 — edited Aug 5 2005
When attempting to connect to a database on a secondary machine on the network I get the following error
---
Cannot connect to database server
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.net.NoRouteToHostException: No route to host: connect

STACKTRACE:

java.net.SocketException: java.net.NoRouteToHostException: No route to host: connect
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:283)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2541)
at com.mysql.jdbc.Connection.<init>(Connection.java:1474)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:264)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at db.main(db.java:15)


** END NESTED EXCEPTION **



Last packet sent to the server was 40 ms ago.

------

My code that I am using for this is
import java.sql.*;

public class db {

	public static void main(String[] args) 
	{
		Connection conn = null;

        try
        {
            String userName = "myusername";
            String password = "mypass";
            String url = "jdbc:mysql://foobar/fooooo";
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
            System.out.println(e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close ();
                    System.out.println ("Database connection terminated");
                }
                catch (Exception e) { /* ignore close errors */ }
            }
        }
		
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 2 2005
Added on Jul 26 2005
3 comments
2,453 views