Change Password functionality using JDBC

Change Password functionality using JDBC

we already created table Password_info in mysqlpassword_table

JDBC program to Implement change password functionality

package jdbc;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
public class Passwordupdate
{
 
	public static void main(String[] args)
	{
 
	 Connection con=null;
	 PreparedStatement pstmt=null;
 
	 String username="bhavesh";
	 String old_password="12345";
	 String new_password="4321";
 
  try{ 
 
	     /*
	      * 1.load the driver
	      */
		Class.forName("com.mysql.jdbc.Driver").newInstance();
 
		/*
		 * 2.get the db connection via driver
		 */
		String dburl="jdbc:mysql://localhost:3306/test?user=root&password=root";
	    con=DriverManager.getConnection(dburl);
 
	    /*
	     * 3.issue sql query via connection
	     */
		String query="update password_info "
	                 + " set current_password=? "
				     + " where user=? and current_password=?";
 
 
		       pstmt=con.prepareCall(query);
 
		       pstmt.setString(1,new_password);
		       pstmt.setString(2,username);
		       pstmt.setString(3,old_password);
 
 
		       int count=pstmt.executeUpdate();
 
		      if(count>0)
		      {
		    	  System.out.println("password successfully changed");
		      }
		      else
		      {
		       System.out.println("unable to change password, provide right credential");
		      }
         }
 
        catch(Exception e)
         {
 
	       e.printStackTrace();
         }
 
           finally
           {
        	   try {
				if(con!=null)
				   {
					   con.close();
				   }
				   if(pstmt!=null)
				   {
 
					  pstmt.close();  
				   }
			} 
 
        	   catch (SQLException e) 
        	   {
				// TODO Auto-generated catch block
				e.printStackTrace();
	           }
              }  
 
 
	}
  }
 
output:
password successfully changed
After changing passwordupdated-password

Leave a Reply