File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes MySQL connection Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "MySQL connection Problem" Watch "MySQL connection Problem" New topic

MySQL connection Problem

Anshul Mathur

Joined: Nov 07, 2003
Posts: 22
I am trying to connect to Mysql Database using the folling code.But not able to do it Database name is StudentPerformance.Please Guide Me where and what is the problem in this code


import java.sql.*;
public class SQLFirst
public static void main(String agrs[])
Statement stmt1;
ResultSet rs1;
DatabaseMetaData dbms1;

catch (Exception e)
System.out.println("Error" + e);
Connection conn1 = DriverManager.getConnection("jdbc:mysql:StudentPerformance");
stmt1 = conn1.createStatement();
rs1 = stmt1.executeQuery("Select * from patients");
dbms1 = conn1.getMetaData();
System.out.println("DATABASE NAME" + dbms1.getDatabaseProductName());
}catch(Exception e)
System.out.println("Error" + e);

Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
You will need to provide the actual error you are getting to help diagnose
Things to look for
1) Be sure that the mysql driver is in your classpath.
2) Be sure that the URL you are using to connect is valid
3) Be sure that you can connect to the DB and run the SQL query without Java.
Without seeing the error you are getting it's hard to give any other advice
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Also, not to sound funny, please be sure the server is running.
One more thing, after you have successfully compiled your code, mySQL listens to port 3306 by default.
For example your connection string should be similar to:
I hope this helps.
I agree. Here's the link:
subject: MySQL connection Problem
It's not a secret anymore!