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: I get an error trying to connect to database:java.sql.SQLException: Access denied for

  1. #1
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I get an error trying to connect to database:java.sql.SQLException: Access denied for

    Greetings!
    I get the following error while compiling my java code:
    java.sql.SQLException: Access denied for user 'root '@'localhost' (using password: YES)
    I have made some research and noticed that this is a common mistake. Unfortunately i didn`t found a solution yet how simply to resolve this matter. I will appreciate any help!

    I use a .properties file with the information about the database.
    Here is my class:
    public static void CreateTable(Connection conn, String url, String driver, String dbName, String userName, String password) {
    			try {
    				Class.forName(driver).newInstance();
    				conn=DriverManager.getConnection(url+dbName, userName, password);
    				try {
    					Statement st=conn.createStatement();
    					String table = "CREATE TABLE Server(name varchar(255), IP varchar(20), OS varchar(255), RAM integer";
    			        st.executeUpdate(table);
    			        System.out.println("Table successfully created");
    				}
    				catch(SQLException e){
    					System.out.println("Table already exists!");
    				}
    				conn.close();
    			}
    			catch(Exception e){
    				e.printStackTrace();
    			}			
    		}
    Last edited by copeg; August 18th, 2010 at 08:49 AM.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: I get an error trying to connect to database:java.sql.SQLException: Access denied

    Since it appears you are using root, you should have permissions to modify the database so make sure your password is correct. In addition, your CREATE TABLE statement is missing an ending parenthesis. Lastly, knowing which line the SQLException is causing the exception (from the stack trace) will shed light on what exactly is causing the exception.

  3. #3
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I get an error trying to connect to database:java.sql.SQLException: Access denied

    I have made a change
    The method CreateTable drops out.
    I create the table manually.
    Then when i am executinmg this, the samo error appears:
    public static void AddInfoDB(Connection conn, String driver, String url, String dbName, String userName, String password, String entered_name, String entered_IP, String entered_OS, int entered_RAM){
    			try {
    				Class.forName(driver);
    				conn = DriverManager.getConnection(url+dbName,userName,password);
    				try {
    					Statement st = conn.createStatement();
    			        int val = st.executeUpdate("INSERT INTO "+dbName+" VALUES("+entered_name+","+entered_IP+","+entered_OS+","+entered_RAM+")");
    			        System.out.println("Information added");
    				}
    				catch(SQLException e){
    					System.out.println("SQL statement is not executed!");
    				}	
    				conn.close();
    			}
    			catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    And the error:

    java.sql.SQLException: Access denied for user 'root '@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1075)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3566)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3498)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:919)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java: 4004)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:12 84)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(Co nnectionImpl.java:2312)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(Connecti onImpl.java:2122)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImp l.java:774)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connect ion.java:49)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:40 9)
    at com.mysql.jdbc.ConnectionImpl.getInstance(Connecti onImpl.java:375)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:289)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at MainMethods.AddInfoDB(MainMethods.java:31)
    at MainProgram.InsertInformation(MainProgram.java:161 )
    at MainProgram.MainActions(MainProgram.java:35)
    at Server.main(Server.java:84)
    Last edited by copeg; August 18th, 2010 at 10:22 AM.

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: I get an error trying to connect to database:java.sql.SQLException: Access denied

    Please use the code tags or the highlight=java tags. This way your code is much more readable for others.

    Which line of the ones you posted corresponds to line 1075?

  5. #5
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I get an error trying to connect to database:java.sql.SQLException: Access denied

    i have no idea there isn`t line 1075
    I debugged and the exception was thrown after this line:
    conn = DriverManager.getConnection(url+dbName,userName,pa ssword);
    when I click on the error that`s what appears:
    the jat filemysql-connector-java-5.1.13-bin.jar has no source attachment
    I don`t know if this could be in any help of you

  6. #6
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: I get an error trying to connect to database:java.sql.SQLException: Access denied

    Doh, my bad. Still not awake after all that coffee...that should read line 31, see the stack trace
    at MainMethods.AddInfoDB(MainMethods.java:31)

    You didn't give the url. Given the stack trace it looks like you are using mysql, does your url conform to something like this?
    jdbc:mysql://localhost:3306/

  7. #7
    Member
    Join Date
    Aug 2010
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I get an error trying to connect to database:java.sql.SQLException: Access denied

    the url, driver, dbName, userName, and password are stored in a properties file, i load the values with no problem, while debugging i checked the values and they are just as they are supposed to be
    Last edited by noFear; August 19th, 2010 at 02:13 AM.

Similar Threads

  1. Java Swing Tables ( JTable Models ) to connect to Database
    By javaprogrammer in forum JDBC & Databases
    Replies: 0
    Last Post: January 26th, 2010, 03:13 PM
  2. Replies: 0
    Last Post: January 26th, 2010, 03:10 PM
  3. How can i store ArrayList objects in Access database
    By frankycool in forum JDBC & Databases
    Replies: 0
    Last Post: November 3rd, 2009, 11:44 PM
  4. access database connectivity from outside an application
    By suchirag in forum JDBC & Databases
    Replies: 0
    Last Post: October 29th, 2009, 02:03 AM
  5. Replies: 6
    Last Post: August 30th, 2009, 04:31 AM