3 Replies - 2841 Views - Last Post: 01 May 2011 - 03:03 PM Rate Topic: -----

#1 v0rtex   User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Connection to a mySQL database using mySQL JDBC and Connector/J

Posted 01 May 2011 - 01:48 PM

Hey all, I am trying to connect to a database I made called grid_db, as can be seen here:
mysql> SELECT * FROM user;
+---------+-----------+------+------+
| user_id | user_name | xpos | ypos |
+---------+-----------+------+------+
|       1 | Default   |    1 |    1 |
+---------+-----------+------+------+


These are the default values I have entered into my table called user in the grid_db database.
I then proceeded to grant privileges to a user as I did not wish for the person connecting to use my root pass.
mysql> GRANT SELECT, INSERT, UPDATE
    -> ON grid_db.user
    -> TO 'user'@'%'
    -> IDENTIFIED BY 'userpass';
Query OK, 0 rows affected (0.40 sec)


Now, I think the above code is correct so I decided to test my DB using JDBC and the mySQL connector/J with the following code:
import java.sql.*;
public class Main {
    public static void main(String[] args) {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String connectionUrl = "jdbc:mysql://localhost/grid_db?" + "user=user & password = userpass";
        Connection con = DriverManager.getConnection(connectionUrl);
        } catch (SQLException e) {
            System.out.println ("SQL Exception: " + e.toString());
        } catch (ClassNotFoundException cE) {
            System.out.println("Class Not Found Exception: " + cE.toString());
    }
    }

}



However when I run the code, it seem's to connect to my database as it gives me an access denied error:
run:
SQL Exception: java.sql.SQLException: Access denied for user 'user '@'localhost' (using password: NO)
BUILD SUCCESSFUL (total time: 1 second)



I am wondering if anyone has any ideas as to why this is not working? I am rather stumped, I was following this tutorial: http://www.linglom.c...e-a-connection/

Any ideas?
Thanks,
v0rtex

Is This A Good Question/Topic? 0
  • +

Replies To: Connection to a mySQL database using mySQL JDBC and Connector/J

#2 sas1ni69   User is offline

  • D.I.C Regular
  • member icon

Reputation: 86
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Re: Connection to a mySQL database using mySQL JDBC and Connector/J

Posted 01 May 2011 - 02:56 PM

Hi there,

Look at this error here means that it cannot access your database because the username for your database is incorrect. You should check the privileges. Usually it's root. Why don't you check that out.

Access denied for user 'user '@'localhost' (using password: NO)


The error is right here btw;

"jdbc:mysql://localhost/grid_db?" + "user=user & password = userpass";

Was This Post Helpful? 1
  • +
  • -

#3 v0rtex   User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Connection to a mySQL database using mySQL JDBC and Connector/J

Posted 01 May 2011 - 03:02 PM

Thanks, I got it working for some reason, it seems to interpret spaces as part of the password. I granted privileges for user before, this worked though:
String connectionUrl = "jdbc:mysql://localhost/grid_db?" + "user=user&password=userpass";


Thanks
Was This Post Helpful? 0
  • +
  • -

#4 sas1ni69   User is offline

  • D.I.C Regular
  • member icon

Reputation: 86
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Re: Connection to a mySQL database using mySQL JDBC and Connector/J

Posted 01 May 2011 - 03:03 PM

You're most welcome. Glad I could help out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1