Click to See Complete Forum and Search --> : Connecting to MySQL with Java in Debian
Zaren
02-25-2003, 01:32 AM
I'm running Debian woody, running MySQL version 3.23.49 using mysql-connector-java-2.0.14-bin.jar to connect. My code is as following:
****CODE****
import java.sql.*;
public class testDB
{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost/timekeeper";
String userName = "chris";
String password = "";
Connection conn = DriverManager.getConnection(url, userName, password);
****END CODE****
And I am getting this 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.net.ConnectException)
I can connect to the database manually - it has been started - can anyone tell me what I am doing wrong?
Thanks.
~Z
Zaren
02-25-2003, 12:27 PM
~BUMP~
I'm away from my machine, but any suggestions would be greatly appreciated.
~Z
Zaren
02-26-2003, 01:50 AM
~bump~
Scouring the internet and finding nothing. I took my firewall down to see if I was blocking myself, doesn't look like it. I've found an error similar to the one I'm getting in the documentation, but it's for applets and says I should deploy the class files to a webserver, which is not my intent at all.
Any help would be greatly appreciated, if you don't know and you could point me to some place to research, that would be wonderful too.
~Z
GaryJones32
02-28-2003, 01:19 AM
here is generic connection code (might help)
i think you have a malformed url
also make sure driver is in classpath or spell out whole thing
also if driver has a native lib make sure it is in LD_LIBRARY_PATH
public Connection connect() throws Exception
{
con = DBConstants.DBCON;
if((con == null) || con.isClosed()) {
try {
DriverManager.registerDriver (new org.gjt.mm.mysql.Driver());
} catch(SQLException ex) {
System.out.println("DBConnection: ClassNotFoundException: " + ex.getMessage());
} catch(Exception e) {
System.out.println("DBConnection: ClassNotFoundException: " + e.getMessage());
}
try {
String tmpDBUrl = "jdbc:mysql://" + Constants.DBSERVERIP + ":3306/" + Constants.DBNAME;
System.out.println("Open DB-Connection: " + Constants.DBNAME +" at " + Constants.DBSERVERIP);
con= DriverManager.getConnection (tmpDBUrl, "username", "password");
con.setAutoCommit(false);
} catch(SQLException ex) {
System.err.println("DBConnection: SQLException: " + ex.getMessage());
}
DBConstants.DBCON = con;
}
return(con);
}
Zaren
02-28-2003, 01:51 AM
I really appreciate the help, unfortunately I tested the code out and I still get the same error. I've tried shutting down my firewall, I've tried multiple drivers, I've checked the mysql.sock permissions, I can't figure out if the problem is due to the drivers, or due to a configuration error in MySQL.
Thanks for the whirl, I've got this question posted on two boards and you're the first one to take a crack at it.
Zaren
02-28-2003, 08:51 PM
I've found the answer - I'm gonna go ahead and post it here because I've seen the question around in other forums with no answer.
I found my answer here (http://www.opencms.com/majordomo/opencms-dev/0111/msg00095.html).
Finally... a solution.
*Zaren cracks a beer open and kicks his feet up*
~Z