Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

MySql connection error

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to connect mySql with JDBC , but i am getting error.
the code is--

public static void main(String[] s){
System.out.println("JDBC TEST CONNECTION");
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("Required jdbc class have not found");
}
System.out.println("MYSQL JDBC Driver registered");
Connection connection = null;
try{
connection = DriverManager.getConnection("jdbc:mysql:\\localhost:3306\test","root","abhra");//test is the db name
}catch(SQLException sqe){
System.out.println("Connection failed! Check output consol");
}
if(connection==null){
System.out.println("You made this database, take charge of this");
}else{
System.out.println("Failed to connect the database");
}
}



and the output I am getting --

JDBC TEST CONNECTION
MYSQL JDBC Driver registered
Connection failed! Check output consol
You made this database, take charge of this



In the eclipse project i have put mysql-connector-java-5.1.24-bin.jar in the libs folder

If I change the code from "jdbc:mysql:\\localhost:3306\test" to jdbc:mysql://localhost:3306/test" then I am getting output ---

JDBC TEST CONNECTION
MYSQL JDBC Driver registered
Failed to connect the database


I have started the "test db with command use test; from mysql command prompt.and the username and password is correct.
Please help me to solve this .
Thanks

 
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is correct - jdbc:mysql://localhost:3306/test. Can you check if your mysql database is up and running. You may use MySql Workbench to connect to the database - 'test'.
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using MySql Command Line Client to run the database.Please tell me how to test the database is running or not and do I have to start it from any other place(may be windows command prompt).

Thanks in advance
 
T Mishra
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope you've already checked this section 2.10.4.5. Starting MySQL from the Windows Command Line
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link, I went to this and then follow these steps ---

1.>In the windows command prompt ------

C:\Program Files\MySQL\MySQL Server 5.0\bin>mysql -u root -p


then provide password "abhra".

2> Then select the the database "test"

mysql> use test;


3>provide permission to the "test" db both before and after selecting the db --

mysql> grant all on test.* to root@localhost;


4> Run the program in eclipse but the result is same .

Not able to understand where is the problem , please help.

Thanks
 
T Mishra
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if(connection==null){
System.out.println("You made this database, take charge of this");
}else{
System.out.println("Failed to connect the database");
}


what is this condition for? Even if the connection object is created you'll get a message - Failed to connect the database
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Extremely Sorry I have just copy paste a wrong portion of code , which creates the misunderstanding here.

Thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic