Can't connect to the database.
Hey guys, I'm doing a school project, and I need to connect to the database with Java. But the problem is that I can't do that.
My connecting code is this:
Code Java:
String drive = "com.mysql.jdbc.Driver";
Class.forName(drive).newInstance();
String url = "jdbc:mysql://localhost/sportovikt_db";
String userName = "sportovikt_base";
String password = "xxxxxx";
con = DriverManager.getConnection(url, userName, password);
But I get this error message:
Code :
java.sql.SQLException: Access denied for user 'sportovikt_base'@'localhost' (using password: YES)
What's wrong here? Did I filled in everything right? I have to say that my database isn't in my pc, it is inside my host name, which I bought. My website address is sportoviktorina.lt so it is ok to put localhost inside my server url?
Please give me some advices, because I don't know what to do, I have two weeks left, and I didn't even start to program real code :(
Re: Can\'t connect to the database.
Quote:
Originally Posted by
djokovic
What's wrong here? Did I filled in everything right? I have to say that my database isn't in my pc, it is inside my host name, which I bought. My website address is sportoviktorina.lt so it is ok to put localhost inside my server url?
You can only use 'localhost' if the code is running in the same machine with the database. Otherwise, you have to use exact host name.
From the exception, it looks like that you are trying to connect to your local MySQL database, but the user/password is incorrect.
Re: Can't connect to the database.
Although this and what you have are the same but try this out:
Code java:
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","username","password");
}
catch(Exception e)
{
// its always important that you print the Exceptions in other to trace 'em
e.printStackTrace();
}
Re: Can't connect to the database.
Quote:
Originally Posted by
Motion
Although this and what you have are the same but try this out:
What is different that makes you think this works over the original code? Please explain
Quote:
Originally Posted by
djokovic
I have to say that my database isn't in my pc, it is inside my host name, which I bought
As mentioned in post #2, localhost is your own computer. You need a) the web host or IP address of the server which hosts your database b) permission to access said database using remote connections.
Re: Can't connect to the database.
Quote:
Originally Posted by
copeg
What is different that makes you think this works over the original code? Please explain
As mentioned in post #2, localhost is your own computer. You need a) the web host or IP address of the server which hosts your database b) permission to access said database using remote connections.
Please genius...we aint fighting. Better watch your mouth.
Re: Can't connect to the database.
Quote:
Originally Posted by
Motion
Please genius...we aint fighting. Better watch your mouth.
Given you gave no explanation as to what you changed and why, that was a fair question. Responding using the wording you have chosen, along with your most recent thread, is not doing you any favors is continuing to be a member of these forums.
Re: Can't connect to the database.
Quote:
Originally Posted by
copeg
Given you gave no explanation as to what you changed and why, that was a fair question. Responding using the wording you have chosen, along with your most recent thread, is not doing you any favors is continuing to be a member of these forums.
And so?