Home Tutorial Java Core Change root password of MYSQL using Java

 
 

Share on Google+Share on Google+
Change root password of MYSQL using Java
Posted on: September 20, 2012 at 12:00 AM
Advertisement
In this tutorial, you will learn how to change the root password of MYSQL database using java code.

Change root password of MYSQL using Java

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.

Advertisement

Related Tags for Change root password of MYSQL using Java:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 20, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com