• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JDBC- mysql Connectivity Problem

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic