MySQL Forums :: Newbie :: Cannot connect to database server

Advanced Search

Cannot connect to database server
Posted by: umesh rawar ()
Date: August 23, 2008 11:32AM


I am new to java/mysql. I am not able to connet to mysql. Please help

While compiling I have classpath set to

I am running on redhat linux 5.

java version is
java version "1.5.0_16"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b02)
Java HotSpot(TM) Server VM (build 1.5.0_16-b02, mixed mode)

Mysql version is 5.0.22

Here is my file

Compiled as
#javac -classpath /var/lib/tomcat5/mysqlconnector/mysql-connector-java-5.1.6/mysql-connector-java-5.1.6-bin.jar

ls -altr Connect*
-rw-r--r-- 1 root root 1168 Aug 23 13:23 Connect.class
-rw-r--r-- 1 root root 1149 Aug 23 13:08

Here is my file.

import java.sql.*;
public class Connect
public static void main (String[] args)
Connection conn = null;
String userName = "testuser";
String password = "testpass";
String url = "jdbc:mysql://localhost/testdb";
System.out.println (url);
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
System.out.println ("After forname");
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
catch (Exception e)
System.err.println ("Cannot connect to database server");
if (conn != null)
conn.close ();
System.out.println ("Database connection terminated");
catch (Exception e) { /* ignore close errors */ }

When I run I get following error

java Connect
Cannot connect to database server

Pleaz help

Options: ReplyQuote

Subject Written By Posted
Cannot connect to database server umesh rawar 08/23/2008 11:32AM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.