This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JDBC and the fly likes JDBC- mysql Connectivity Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC- mysql Connectivity Problem" Watch "JDBC- mysql Connectivity Problem" New topic
Author

JDBC- mysql Connectivity Problem

Stan Levine
Greenhorn

Joined: Jan 21, 2001
Posts: 22
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
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Ok, the first and most obvious question ( don't be insulted, I'm just going to basics first ) is, after you installed mySQL did you start the DB Engine and create a database? In other words, is the error message wrong and there IS a server running on that port?
Stan Levine
Greenhorn

Joined: Jan 21, 2001
Posts: 22
Thanks for responding and of course I'm not insulted. I appreciate all the help I can get. Your question raises another problem I had. First of all, when you say "start the DB Engine", I guess you mean issue cd c:\mysql\bin and issue the command "mysql -u userid =h localhost -p" and then give the password when prompted. I did this on day 1 and: 1) gave myself an ID and permissions and 2) built a database. The next day and also today the same command failed when I entered my password. So when the java program was running, no mysql session was logged on. So does that mean that no server was running? mysql must be started when a program tries to connect? By the way, I installed mysql on my PC (which is not a server), and I'm running on my PC. Is that a problem? Next question, I'm running on localhost, but what port am I running on, and does matter? In case I just have the wrong password, do you know how I can fix it? Thanks, Stan
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Stan, I feel a brotherhood with you. I also installed MySQL on my home PC, which is also not a server. I too am connecting to it with JDBC. I too had some struggles to get this working...
MySQL would normally be a client-server application. That would mean that the server portion of the database program would always be running (usually on a database server computer somewhere) and it would listen for database clients trying to connect on any port that it is listening to. If you haven't told it to listen to some other port, it is using the default, which I think is 3306 for MySQL. Assume for the moment that this is not your problem. The error message that you are getting doesn't make me think that you are finding the server but are not authorized to get in. It seems that JDBC can't find the server. There is a MySQL server application that you must be running in order for a client to connect. I'm not at home now, so I don't have the name of the executable. Sorry. Anyway, you will start the server and then clients can connect at will. On Windows Me ( where I run this thing ) a little traffic light comes up in my task bar with a green light indicating that the server is running normally and is ready for connections. If you are not seeing/doing this, then that is why you are getting the error you are getting. Check out this link for more information on how to start the server. If you aren't able to get this done by tonight, post back and I'll reply from home with specifics.
Stan Levine
Greenhorn

Joined: Jan 21, 2001
Posts: 22
Bodie Minster, Thanks very much. You hit it exactly! I went into Windows Explorer,did a cd c:\mysql\bin and ran every .exe until one worked(meaning until I could then logon to mysql).(It was mysqld.exe, by the way). Then I ran the java program and it worked too. Great satisfaction when you are able to break through! In retrospect I now remember trying to run these .exes the other day, when I was able to logon. I was just trying to bring up mysql from Windows Explorer, and when a window opened and closed immediately, I didn't know that I had stumbled into starting the server mysql program. Thanks a lot. Stan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC- mysql Connectivity Problem