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! :)
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
CREATE USER 'mydbuser'@'localhost' IDENTIFIED BY 'asdf';
GRANT ALL PRIVILEGES ON . TO 'mydbuser'@'localhost' ;
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);
}