I installed mysql, and am having a problem creating a Connection object. Below is the code and the runtime error I received. The driver load works. The getConnection does not. I would appreciate some guidance. Thanks, Stan
import java.sql.*;
class PetDB
{
public static void main (String[] args)
{
String database = "jdbc:mysql://localhost/test";
Object o = null;
Connection con = null;
try
{
o = Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception e)
{
System.err.println("Unable to load driver.");
e.printStackTrace();
}
System.out.println("Object o:" + o);
try
{
con = DriverManager.getConnection(database);
}
catch (SQLException sqle)
{
System.out.println("Caught SQLException when invoking DriverManager.getConnection()");
System.out.println("SQLException: " + sqle.getMessage());
System.out.println("SQLState: " + sqle.getSQLState());
System.out.println("VendorError: " + sqle.getErrorCode());
sqle.printStackTrace();
}
System.out.println("Connection con:" + con);
}
________________________________________________________________
Caught SQLException when invoking DriverManager.getConnection()
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.net.Conn
ectException)
SQLState: 08S01
VendorError: 0
java.sql.SQLException: Cannot connect to MySQL server on localhost:3306. Is ther
e a MySQL server running on the machine/port you are trying to connect to? (java
.net.ConnectException)
at org.gjt.mm.mysql.Connection.<init>(Connection.java:239)
at org.gjt.mm.mysql.Driver.connect(Driver.java:126)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at PetDB.main(PetDB.java:22)
Connection con:null
import java.sql.*;
class PetDB
{
public static void main (String[] args)
{
String database = "jdbc:mysql://localhost/test";
Object o = null;
Connection con = null;
try
{
o = Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception e)
{
System.err.println("Unable to load driver.");
e.printStackTrace();
}
System.out.println("Object o:" + o);
try
{
con = DriverManager.getConnection(database);
}
catch (SQLException sqle)
{
System.out.println("Caught SQLException when invoking DriverManager.getConnection()");
System.out.println("SQLException: " + sqle.getMessage());
System.out.println("SQLState: " + sqle.getSQLState());
System.out.println("VendorError: " + sqle.getErrorCode());
sqle.printStackTrace();
}
System.out.println("Connection con:" + con);
}
________________________________________________________________
Caught SQLException when invoking DriverManager.getConnection()
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.net.Conn
ectException)
SQLState: 08S01
VendorError: 0
java.sql.SQLException: Cannot connect to MySQL server on localhost:3306. Is ther
e a MySQL server running on the machine/port you are trying to connect to? (java
.net.ConnectException)
at org.gjt.mm.mysql.Connection.<init>(Connection.java:239)
at org.gjt.mm.mysql.Driver.connect(Driver.java:126)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at PetDB.main(PetDB.java:22)
Connection con:null