A utility to change oracle schema password using Java

The following java standalone program can be used to change Oracle password. It uses the OCI calls to change the password.

/*
Copyright (C) 2010-2011 Amin Jaffer

This program is free software: you can redistribute it and/or modify.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.GroupLayout.*;

public class PasswordChanger extends JFrame {
private JTextField pfldDB; // db name
private JTextField pfldUser; // db username
private JPasswordField pfldCurrent; // current password
private JPasswordField pfldNewPsw; // new password
private JPasswordField pfldChkPsw; // new confirm password
private Cursor normalCursor = new Cursor(Cursor.DEFAULT_CURSOR); // normal cursor
private Cursor hourglassCursor = new Cursor(Cursor.WAIT_CURSOR); // wait cursor

public PasswordChanger() throws Exception {
super("Oracle Password Change utility");

// Locale.setDefault(new Locale("us","EN"));
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

setDefaultCloseOperation(EXIT_ON_CLOSE);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension scr = tk.getScreenSize();
Dimension size = new Dimension(480,300);
int x = (scr.width – size.width) / 2;
int y = (scr.height – size.height) / 2;
setLocation(x,y);
//setPreferredSize(size);

pfldDB = new JTextField(10);
pfldUser = new JTextField(30);

pfldCurrent = new JPasswordField(15);
pfldNewPsw = new JPasswordField(15);
pfldChkPsw = new JPasswordField(15);

JLabel
lablDB = new JLabel("Database:"),
lablUser = new JLabel("Username:"),
lablCurrent = new JLabel("Current Password:"),
lablNewPsw = new JLabel("New Password:"),
lablChkPsw = new JLabel("Confirm New Password:");

JPanel pane = new JPanel();
GroupLayout glay = new GroupLayout(pane);
pane.setLayout(glay);

SequentialGroup hgrp = glay.createSequentialGroup();
hgrp.addGroup(glay.createParallelGroup(Alignment.TRAILING)
.addComponent(lablDB)
.addComponent(lablUser)
.addComponent(lablCurrent)
.addComponent(lablNewPsw)
.addComponent(lablChkPsw))
.addGroup(glay.createParallelGroup(Alignment.LEADING)
.addComponent(pfldDB)
.addComponent(pfldUser)
.addComponent(pfldCurrent)
.addComponent(pfldNewPsw)
.addComponent(pfldChkPsw));

SequentialGroup vgrp = glay.createSequentialGroup();
vgrp.addGroup(glay.createParallelGroup(Alignment.BASELINE)
.addComponent(lablDB).addComponent(pfldDB))
.addGroup(glay.createParallelGroup(Alignment.BASELINE)
.addComponent(lablUser).addComponent(pfldUser))
.addGroup(glay.createParallelGroup(Alignment.BASELINE)
.addComponent(lablCurrent).addComponent(pfldCurrent))
.addGroup(glay.createParallelGroup(Alignment.BASELINE)
.addComponent(lablNewPsw).addComponent(pfldNewPsw))
.addGroup(glay.createParallelGroup(Alignment.BASELINE)
.addComponent(lablChkPsw).addComponent(pfldChkPsw));

glay.setHorizontalGroup(hgrp);
glay.setVerticalGroup(vgrp);
glay.setAutoCreateContainerGaps(true);
glay.setAutoCreateGaps(true);

add(pane, BorderLayout.CENTER);

JPanel paneCmd = new JPanel();
paneCmd.add(new JButton(new OKAction()));
paneCmd.add(new JButton(new CancelAction()));

add(paneCmd, BorderLayout.SOUTH);

pack();
}

private class OKAction extends AbstractAction {
public OKAction() {
putValue(NAME, "Change Password");
}

private String appendMessage(String src, String msg) {
if ( src.equals("") )
return msg;
else
return src + "\n" + msg;
}
private String checkField(String field, String sErrMessage, String msg) {
if ( field.equals("") ) {
sErrMessage = appendMessage(sErrMessage, msg);
}
return sErrMessage;
}

public void actionPerformed(ActionEvent e) {
String sErrMessage = "";
String sDB, sUser, sCurrent, sNewPassword, sNewConfirmPassword;

sDB = pfldDB.getText();
sErrMessage = checkField(sDB, sErrMessage, "Database cannot be blank");

sUser = pfldUser.getText();
sErrMessage = checkField(sUser, sErrMessage, "Username cannot be blank");

sCurrent = new String(pfldCurrent.getPassword());
sErrMessage = checkField(sCurrent, sErrMessage, "Current Password cannot be blank");

sNewPassword = new String(pfldNewPsw.getPassword());
sErrMessage = checkField(sNewPassword, sErrMessage, "New Password cannot be blank");

sNewConfirmPassword = new String(pfldChkPsw.getPassword());
sErrMessage = checkField(sNewConfirmPassword, sErrMessage, "Confirm New Password cannot be blank");

if ( ! sNewPassword.equals(sNewConfirmPassword) ) {
sErrMessage = appendMessage(sErrMessage, "New Password does not match Confirm New Password");
}

if ( ! sErrMessage.equals("") ) {
JOptionPane.showMessageDialog(new JFrame(), sErrMessage, "Error", JOptionPane.ERROR_MESSAGE);
}
else {
Properties props;
Connection newconn = null;
String url = "jdbc:oracle:oci:@";

setCursor(hourglassCursor);

props = new Properties();

try {
url = url + sDB;
props.put("user", sUser);
props.put("password", sCurrent);
props.put("OCINewPassword", sNewPassword);
newconn = DriverManager.getConnection(url, props);
JOptionPane.showMessageDialog(new JFrame(),
"Password changed successfully", "Oracle Password changer",
JOptionPane.INFORMATION_MESSAGE);
int response = JOptionPane.showConfirmDialog(new JFrame(),
"Would you like to change your password for another database?",
"",
JOptionPane.YES_NO_OPTION);
if ( response == JOptionPane.NO_OPTION )
System.exit(0);
else {
pfldDB.setText(""); // db
pfldCurrent.setText(""); // current password
pfldNewPsw.setText(""); // new password
pfldChkPsw.setText(""); // new confirm password
}
}
catch (Exception exp) {
setCursor(normalCursor);

JOptionPane.showMessageDialog(new JFrame(),
exp.getMessage(), exp.getClass().getSimpleName(),
JOptionPane.WARNING_MESSAGE);

}

setCursor(normalCursor);

try {
if ( newconn != null )
newconn.close();
}
catch (Exception exp) {
JOptionPane.showMessageDialog(new JFrame(),
exp.getMessage(), exp.getClass().getSimpleName(),
JOptionPane.WARNING_MESSAGE);
}

}

}
}

private class CancelAction extends AbstractAction {
public CancelAction() {
putValue(NAME, "Cancel");
}

public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}

/**
* @param args
*/
public static void main(String[] args) throws Exception {
(new PasswordChanger()).setVisible(true);

}

}

To compile:
C:> set CLASSPATH=c:\Oracle\product\10.2.0\client_1\jdbc\lib\classes12.zip
C:> javac.exe PasswordChanger.java

To execute: One would need the Oracle instant client to execute, in the following example it uses the 11g instant client
@set ROOT_DIR=c:\passwordchanger
@set TNS_ADMIN=\\sharefolder\tnsadmin
@set NLS_LANG=american_america.we8iso8859p1
@set CLASSPATH=%ROOT_DIR%\oraclient11g\ojdbc6.jar;%ROOT_DIR%
@set PATH=%ROOT_DIR%\oraclient11g;%PATH%
java PasswordChanger

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s