So, I wrote the following program to connect to my local mysql server and create a table in the existing "test" database.


The program compiled without any errors, but I got the following run-time error:

java.sql.SQLException: Cannot connect to MySQL server on localhost:3306. Is there a MySQL server running on the machine/port you are trying to connect to? (java.lang.NumberFormatException)
at org.gjt.mm.mysql.Connection.connectionInit(Unknown Source)
at org.gjt.mm.mysql.jdbc2.Connection.connectionInit(Unknown Source)
at org.gjt.mm.mysql.Driver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:200)
at DB.main(DB.java:10)

Here's the code.

import java.sql.*;

public class DB
{
	public static void main(String args[])
	{
		try
		{
		Class.forName("org.gjt.mm.mysql.Driver");
		Connection con =      DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
		Statement st = con.createStatement();
		st.executeUpdate("create table s1(name varchar(20), roll int(10))");
		st.close();		
		con.close();
		con.commit();
		}
		
		catch(Exception e)
			{
				e.printStackTrace();
			}
	
	}
		
}

Is the url correct: jdbc:mysql://localhost/test ? Maybe you are missing the port:
jdbc:mysql://localhost:<port>/test

As the exception says:
Cannot connect to MySQL server on localhost:3306. Is there a MySQL server running on the machine/port you are trying to connect to?

Try to read first the exceptions that you get. They provide the solution.

Also you have another error in your logic.
You don't need to call the commit method because the connection is set to auto commit by default.
You don't need to call commit in general when you call 'create' statements. Only for insert - update
You close the connection and then you call the commit. The close needs to be the last command. How can you commit anything if you have already closed the connection.

As the error message asks:

Is there a MySQL server running on the machine/port you are trying to connect to?

Install the mysql command line client and test if it can connect to your mysql server on your local machine. Or, if you're running windows, have a look at the services: is mysql active? Or, in linux, look which processes are running. Is mysqld running?

The usual driver for MySQL is com.mysql.jdbc.Driver . Driver you use is usable but little out of date

The org.gjt.mm.mysql.Driver class name is also usable to remain backward-compatible with MM.MySQL

Secondly, did you checked that DB is running? (In command line type mysql, or use one of the many GUIs to check if you can connect with DB)

Fixed the problem finally. The drivers were corrupt. Replacing them did the trick. Thanks for your valuable help. I appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.