Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Can't connect to the database.

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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:
    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


  2. #2

    Default Re: Can't connect to the database.

    Quote Originally Posted by djokovic View Post
    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.

  3. #3
    Junior Member Motion's Avatar
    Join Date
    Jul 2012
    Posts
    21
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't connect to the database.

    Although this and what you have are the same but try this out:

     
    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();
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can't connect to the database.

    Quote Originally Posted by Motion View Post
    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 View Post
    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.

  5. #5
    Junior Member Motion's Avatar
    Join Date
    Jul 2012
    Posts
    21
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't connect to the database.

    Quote Originally Posted by copeg View Post
    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.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can't connect to the database.

    Quote Originally Posted by Motion View Post
    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.

  7. #7
    Junior Member Motion's Avatar
    Join Date
    Jul 2012
    Posts
    21
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't connect to the database.

    Quote Originally Posted by copeg View Post
    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?

Similar Threads

  1. How to connect to a remote database?
    By ace84 in forum JDBC & Databases
    Replies: 4
    Last Post: February 24th, 2011, 02:09 AM
  2. Cant connect with database
    By ronn1e in forum JDBC & Databases
    Replies: 1
    Last Post: January 4th, 2011, 05:45 PM
  3. Cant connect with database
    By ronn1e in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 4th, 2011, 04:09 PM
  4. connect java to a database
    By ridg18 in forum JDBC & Databases
    Replies: 1
    Last Post: December 25th, 2010, 11:28 AM