1
votes

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! :)

1
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
To start with, you should set up a separate user other than root.Adam B
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
Sorry, I'm newbie here. I don't know how to enter (newline), whenever I press enter,it submit my comment.nhix
Hi @AdamB, do you mean I have to create a new user? Is it really necessary? Please explain. thank younhix

1 Answers

0
votes

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