File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes MySql connection error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "MySql connection error" Watch "MySql connection error" New topic
Author

MySql connection error

Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
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

T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 108

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'.


Thanks,
Tushar (SCJP 1.5)
Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
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

Joined: Apr 04, 2006
Posts: 108

I hope you've already checked this section 2.10.4.5. Starting MySQL from the Windows Command Line
Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
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

Joined: Apr 04, 2006
Posts: 108

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

Joined: May 22, 2008
Posts: 122
Extremely Sorry I have just copy paste a wrong portion of code , which creates the misunderstanding here.

Thanks
 
 
subject: MySql connection error
 
Similar Threads
Query about Connection interface
How to Resolve Java / MySql error: Driver loaded, but cannot connect to db: jdbc:mysql://ecterra
JDBC connection
java jdbc
connection problem