Page 1 of 1

[SOLVED] Java's JDBC MySQL connector failing?

Posted: Fri Oct 20, 2006 5:10 am
by Chris Corbyn

Code: Select all

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class MySQLApp
{
	public static void main(String[] args)
	{
		Statement stmt;
		
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (Exception e) {
			System.out.println("Class Loading Failed: " + e.getMessage());
		}
		
		String url = "jdbc:mysql://127.0.0.1:3306/my_db";
		
		System.out.println("Trying to connect with the following credentials: " + url);
		
		Connection c;
		
		try {
			c = DriverManager.getConnection(url, "my_user", "password");
		} catch (SQLException e) {
			System.out.println("Failed: " + e.getMessage());
		}
	}
}
I'm using MySQL 5.0 with MySQL/J driver version 3.1 (which says it supports MySQL 5).

I just keep getting connection refused even though I've opened up this particular database to anyone and any location. Everything except my java app works fine :(

Code: Select all

[d11wtq@pc-cac MySQLApp]$ java MySQLApp
Trying to connect with the following credentials: jdbc:mysql://127.0.0.1/my_db
Failed: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection refused
        at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)
        at com.mysql.jdbc.Connection.createNewIO(Connection.java:2569)
        at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at MySQLApp.main(MySQLApp.java:26)


** END NESTED EXCEPTION **



Last packet sent to the server was 20 ms ago.
[d11wtq@pc-cac MySQLApp]$
Anybody else had this problem before? I'm using Java 1.4.2 on Linux.

Posted: Fri Oct 20, 2006 5:56 am
by timvw
Does your MySQL configuration allow tcp/ip connections?

- If i'm not mistaken the driver will presume that you're not going to provide credentials if there is not a ?user=XXX part in the connection string... (In a couple of hours i'm back at home and then i can look into this)

Posted: Fri Oct 20, 2006 5:56 am
by Chris Corbyn
Sorry - My bad. I had "skip_networking" in my.cnf :oops:

EDIT | Thanks tim; you were right on basically. I had MySQL set to just use /tmp/mysql.sock but not TCP/IP. Changed the config and it works a charm now.