In this tutorial, you will learn how to change the root password of MYSQL database using java code.
In this tutorial, you will learn how to change the root password of MYSQL database using java code.In this tutorial, you will learn how to change the root password of MYSQL database using java code.
For every database, you have to reset the root password for its security. For mysql, the system administrator user is called root. You can also set the new password using the mysqladmin utility from the command line.
Example
import java.sql.*; class ChangeRootPassword { public static void main(String[] args) { try{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); String sql = "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('rose')"; Statement stmt = conn.createStatement(); stmt.executeQuery(sql); stmt.close(); conn.close(); System.out.println("Password is changed successfully!"); } catch(Exception ex){ ex.printStackTrace(); } } }
Description of Code: Changing the root password of a MySQL database is trivial if you know the current password if you don?t it is a little trickier. Here we are going to change the root password with the known password. For this, we have created a database connection and executed the query SET PASSWORD FOR 'root'@'localhost' = PASSWORD('rose') to change the password from "root" to "rose". Now our current password becomes rose.