1

I've created an application for our company. At first, I've tried to create without password on my database in MySQL-xampp and it works fine. Now my problem is I put a password on my database and my application is now throwing me this error whenever I log in:

SQL java.sql.SQLException: Access denied for user 'root'@'CITSP-MOB7'(using password: YES)

I also get an error connecting my database:

Unable to connect. Cannot establish a connection to jdbc:mysql//localhost:3306/mydb using com.mysql.jdbc.Driver (Access denied for user 'root'@'CITSP-MOB7' (using password: YES)).

Here is my code for database connection:

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://192.168.1.112:3306/citsp";
static final String USER = "root";
static final String PASS = "mypassword";

try{
   Class.forName("com.mysql.jdbc.Driver");
  }catch(ClassNotFoundException ex){
            }
  //STEP 3: Open a connection
  System.out.println("Connecting to database...");
  conn = DriverManager.getConnection(DB_URL,USER,PASS);

  //STEP 4: Execute a query
  System.out.println("Creating statement...");
  stmt = conn.createStatement();

Can somebody help me to solve my connection problem? Your answer will be appreciated. Thanks in advance! :)

nhix
  • 81
  • 3
  • 13
  • You set a password in your database and let your application know that it should know the password prior to access it. Post your java code please [only the connection to mysql portion] – 1000111 Apr 22 '15 at 04:07
  • To start with, you should set up a separate user other than root. – Adam B Apr 22 '15 at 04:08
  • Hi @Subrata Dey, this is my code: static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/citsp"; static final String USER = "root"; static final String PASS = "mypassword"; – nhix Apr 22 '15 at 05:17
  • Sorry, I'm newbie here. I don't know how to enter (newline), whenever I press enter,it submit my comment. – nhix Apr 22 '15 at 05:22
  • Hi @AdamB, do you mean I have to create a new user? Is it really necessary? Please explain. thank you – nhix Apr 22 '15 at 05:24
  • @SubrataDey I already pasted in my question. thank you. – nhix Apr 22 '15 at 05:29
  • are you able to access the database through command line with the password? – WannaBeGeek Apr 22 '15 at 05:37
  • @user3467204 I can access database through command line without entering password – nhix Apr 22 '15 at 05:41
  • try with password also and check if it is working. – WannaBeGeek Apr 22 '15 at 05:42
  • @user3467204 I cannot access. it says "Access denied for user 'root'@'CITSP-MOB7'(using password: YES)" – nhix Apr 22 '15 at 05:46
  • try with this command : GRANT ALL PRIVILEGES ON *.* TO 'root'@'CITSP-MOB7' IDENTIFIED BY 'root' WITH GRANT OPTION; and for more options refer here : : http://stackoverflow.com/questions/11922323/java-sql-sqlexception-access-denied-for-user-rootlocalhost-using-password – WannaBeGeek Apr 22 '15 at 05:50
  • @user3467204 I still get the same error. – nhix Apr 22 '15 at 05:57
  • whats is "CITSP-MOB7" ? – WannaBeGeek Apr 22 '15 at 06:00
  • @user3467204 that's my laptop's hostname. – nhix Apr 22 '15 at 06:01
  • @nhix, how have you set your password? I mean what's the syntax? – 1000111 Apr 22 '15 at 06:03
  • you need to resolve the database connection error with password. Refer : http://stackoverflow.com/questions/8484722/access-denied-for-user-rootlocalhost-while-attempting-to-grant-privileges and http://dba.stackexchange.com/questions/10852/mysql-error-access-denied-for-user-alocalhost-using-password-yes/10897#10897 – WannaBeGeek Apr 22 '15 at 06:08
  • @SubrataDey do you mean this -- > static final String PASS = "mypassword"; conn = DriverManager.getConnection(DB_URL,USER,PASS);? or in MySQL? – nhix Apr 22 '15 at 06:08
  • No didn't mean that. You said that you set a password for your mysql user. What was the syntax u used to set password in mysql? – 1000111 Apr 22 '15 at 06:09
  • @SubrataDey $cfg['Servers'][$i]['auth_type'] = 'http'; /*config*/ $cfg['Servers'][$i]['user'] = 'root'; $cfg['Servers'][$i]['password'] = 'mypassword'; $cfg['Servers'][$i]['extension'] = 'mysqli'; $cfg['Servers'][$i]['AllowNoPassword'] = false; $cfg['Lang'] = ''; – nhix Apr 22 '15 at 06:14
  • @user3467204 let me check the link. thanks. – nhix Apr 22 '15 at 06:14
  • @nhix, Are you accessing a remote database or local database? – 1000111 Apr 22 '15 at 06:15
  • @SubrataDey I'm accessing a local database. – nhix Apr 22 '15 at 06:17

1 Answers1

0

Please test creating a new user in mysql if this works for you.

First create a new user in mysql: Login from mysql command line as root. Then run the following commands one by one.

MySQL

  1. CREATE USER 'mydbuser'@'localhost' IDENTIFIED BY 'asdf';

  2. GRANT ALL PRIVILEGES ON . TO 'mydbuser'@'localhost' ;

  3. FLUSH PRIVILEGES;

Change your above java code to the following: I assumed your database name is 'citsp'

JAVA

        String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        String DB_URL = "jdbc:mysql://localhost/citsp";
        String USER = "mydbuser";
        String PASS = "asdf";

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            //JOptionPane.showMessageDialog(null, "There's an error connecting with the database. Please contact your administrator.");
        }
        //STEP 3: Open a connection
        System.out.println("Connecting to database...");
        java.sql.Connection conn = null;
        try {
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
        } catch (SQLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }

        //STEP 4: Execute a query
        System.out.println("Creating statement...");
        try {
            Statement stmt = conn.createStatement();
            String query = "";
            ResultSet rs = stmt.executeQuery(query);
            while(rs.next()){
                // Iterate through the result
            }
        } catch (SQLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
1000111
  • 13,169
  • 2
  • 28
  • 37
  • It still doesn't solved my issue. But really thank you for your help. – nhix Apr 22 '15 at 06:47
  • @nhix, Have you been able to create a new user in mysql following the given commands? What error message are you getting right now? – 1000111 Apr 22 '15 at 06:51
  • yes I have been able to create a new user with the commands you've given. Same error, the root just changed to mydbuser. :( – nhix Apr 22 '15 at 06:58
  • @nhix, I've made it work in my side. Have you granted and flushed privileges too? And Have you made all the necessary changes in java portion? – 1000111 Apr 22 '15 at 07:01
  • yes, I did flushed privileges. I think I've done all the necessary changes in java. Or am I forgetting something? Can you list up the changes that you're doing in java? thank you – nhix Apr 22 '15 at 07:11
  • It's exactly same I've stated in my answer. But for your convenience it will be found here too : http://paste.ubuntu.com/10864981/ @nhix – 1000111 Apr 22 '15 at 07:44
  • I think my issue is now solved. I've just started over again and then followed your instruction and it's working fine. Thank you very much. 'til next time. :) – nhix Apr 22 '15 at 07:58
  • Now I can heave a sigh of relief :) Glad to hear that it's working now :) :) – 1000111 Apr 22 '15 at 08:07