Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

MySQL connection Problem

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
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
thanks
ANSHUL

*****************CODE***************

import java.sql.*;
public class SQLFirst
{
public static void main(String agrs[])
{
Statement stmt1;
ResultSet rs1;
DatabaseMetaData dbms1;
try
{
Class.forName("com.mysql.jdbc.Driver");

}
catch (Exception e)
{
System.out.println("Error" + e);
}
try
{
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);
}
}

}
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
"jdbc:mysql://localhost:3306/StudentPerformance"
I hope this helps.
Craig.
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic