Skip navigation links

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

Hi,

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

While compiling I have classpath set to
/var/lib/tomcat5/mysqlconnector/mysql-connector-java-5.1.6/mysql-connector-java-5.1.6-bin.jar

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 Connect.java file

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

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 Connect.java

Here is my Connect.java file.

import java.sql.*;
public class Connect
{
public static void main (String[] args)
{
Connection conn = null;
try
{
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");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}

When I run I get following error


java Connect
jdbc:mysql://localhost/testdb
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.