9 Replies - 1190 Views - Last Post: 19 April 2011 - 01:42 AM Rate Topic: -----

#1 Saphiro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-September 07

Java & JDBC - Connection problem

Posted 17 April 2011 - 09:19 AM

Hey, so I'm trying create a Bukkit(Server software for a game called Minecraft :)) Plugin that uses a mysql database.

My code throws an exception every time I execute it though, and it's a pretty odd one as well:
Message:Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.



I'm 100% sure localhost is reachable (My music player Amarok uses a database on localhost), the account I'm trying to login with exists (can login using mysql -u) and the password is correct as well.

I also think that it's not the driver, because calling a DriverManager.GetDriver(*the exact same string*) returns a driver.

I've been looking for hours now and can't find the solution to this problem anywhere :(

Below is part of my code and the terminal output.

	private boolean openDB() {
		try{Class.forName("com.mysql.jdbc.Driver");}
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.err.println("[SanguineBounty]ClassNotFoundException");
			e.printStackTrace();
		}
		
		try
		{
			Driver d =  DriverManager.getDriver(mysqlurl+mysqldb+"?user="+mysqluser+"&password="+mysqlpass);
			System.out.println("Driver: "+d);
			conn = DriverManager.getConnection(mysqlurl+mysqldb+"?user="+mysqluser+"&password="+mysqlpass);
			System.out.println ("[SanguineBounty]Connected to database.");
			//checkDB();
			return true;
		}
        catch (SQLException e)
        {
        	System.out.println("Connection(conn): " + conn);
        	System.out.println(mysqlurl + mysqldb +" user: "+ mysqluser+ " pass: " + mysqlpass);
            System.err.println("[SanguineBounty]Cannot connect to database server");
            System.out.println("Message:"+ e.getMessage());
            System.out.println("Error code: "+ e.getErrorCode());
        }
		return false;
	}


Terminal output:
18:11:16 [INFO] Driver: com.mysql.jdbc.Driver@3c1fc1a6
18:11:17 [INFO] Connection(conn): null
18:11:17 [INFO] jdbc:mysql://localhost:3306/sanguineDB  user: sanguinebounty pass: <censored>
18:11:17 [SEVERE] [SanguineBounty]Cannot connect to database server
18:11:17 [INFO] Message:Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
18:11:17 [INFO] Error code: 0
18:11:17 [INFO] [SanguineBounty]Cannot connect to database. Disabling SanguineBounty...



If someone knows the answer to this dillema Please share it with me.. It's driving me nuts :(

Is This A Good Question/Topic? 0
  • +

Replies To: Java & JDBC - Connection problem

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Java & JDBC - Connection problem

Posted 17 April 2011 - 09:57 AM

It's certainly worth trying a different driver. Try the connector-j one for MySql
Was This Post Helpful? 1
  • +
  • -

#3 Saphiro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-September 07

Re: Java & JDBC - Connection problem

Posted 17 April 2011 - 11:57 AM

View Postg00se, on 17 April 2011 - 09:57 AM, said:

It's certainly worth trying a different driver. Try the connector-j one for MySql

I have tried changing the driver to org.gjt.mm.mysql.Driver . The program still loads the other one then, and when I deregister the other one it can't find the org.gjt.mm.mysql.Driver :S
Was This Post Helpful? 0
  • +
  • -

#4 Saphiro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-September 07

Re: Java & JDBC - Connection problem

Posted 18 April 2011 - 07:45 AM

I am still struggling with this, I've been hesitant to use SQL with java before for a few other projects, resulting in me programming all kinds of silly functions to store the data in random text files.
Once I decided I wanted to use SQL in this project I also made a promise to myself I'd get it to work properly.
Clearly I'm either doing something very wrong or java just hates me and wants me to store my data in a different way.
Regarding the last:
Any suggestions on how I should store the data in an effective way then? (the program will be querying for data quite often)
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Java & JDBC - Connection problem

Posted 18 April 2011 - 07:58 AM

Getting superstitious is not going to help you very much ;) Did you try the other driver as i suggested?
Was This Post Helpful? 0
  • +
  • -

#6 Saphiro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-September 07

Re: Java & JDBC - Connection problem

Posted 18 April 2011 - 08:15 AM

View Postg00se, on 18 April 2011 - 08:58 AM, said:

Getting superstitious is not going to help you very much ;) Did you try the other driver as i suggested?

When I swap Class.forName("com.mysql.jdbc.Driver") with Class.forName("org.gjt.mm.mysql.Driver") It keeps using the com.mysql Driver. I've tried deregistering the com.mysql Driver , but I'm just getting an exception "No driver found." then.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Java & JDBC - Connection problem

Posted 18 April 2011 - 08:58 AM

You need to put the connector-j jar in the classpath
Was This Post Helpful? 0
  • +
  • -

#8 Saphiro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-September 07

Re: Java & JDBC - Connection problem

Posted 18 April 2011 - 01:07 PM

View Postg00se, on 18 April 2011 - 09:58 AM, said:

You need to put the connector-j jar in the classpath

Hmm, You mean as in "add external jar file" in eclipse?
I have done that, (at least for a jar called mysql-connector-java-5.1.15-bin.jar containing an org.gjt.mm.mysql.Driver class) I don't know if that's what I'm supposed to do though.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java & JDBC - Connection problem

Posted 18 April 2011 - 06:54 PM

The DriverManager.getConnection() signature with username and password is

getConnection(URL url, String user, String password);

http://download.orac...Connection.html

You pass a single long String using the method:

getConnection(URL url);
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Java & JDBC - Connection problem

Posted 19 April 2011 - 01:42 AM

Quote

Hmm, You mean as in "add external jar file" in eclipse?
I have done that, (at least for a jar called mysql-connector-java-5.1.15-bin.jar containing an org.gjt.mm.mysql.Driver class) I don't know if that's what I'm supposed to do though.


Yes, that's exactly what you should be doing, and if you have, it's strange that you got that error. Do a complete clean of the project and try again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1