title: Oracle password expiry and grace period handling using Java, Oracle JDBC
body: <p>I have an Oracle 11g Database and i´ve coded an application which is along with many other things able to create users. i want the end user who uses the account to change the password after creating so i set the <code>pasword expire</code> clause when creating a user.</p>
  
  <p>so now my question is how to handle an <code>ORA-28001: the password has expired</code> exception in java, so the user is able to change the password as required. the <code>grace period</code>is set to 2 days. </p>
  
  <p>thats the whole error message i get if you need it:</p>
  
  <pre><code>    openCon():  URL = jdbc:oracle:thin:@//xxx.xxx.xxx.xxxx:xxxx/dbName username = asd password = mypasswordwhichisexpired
      java.sql.SQLException: ORA-28001: the password has expired
  
  at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:445)
  at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:389)
  at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:382)
  at oracle.jdbc.driver.T4CTTIfun.processError(T4CTTIfun.java:600)
  at oracle.jdbc.driver.T4CTTIoauthenticate.processError(T4CTTIoauthenticate.java:445)
  at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:450)
  at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:192)
  at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:380)
  at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:760)
  at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:401)
  at oracle.jdbc.driver.PhysicalConnection.&lt;init&gt;(PhysicalConnection.java:546)
  at oracle.jdbc.driver.T4CConnection.&lt;init&gt;(T4CConnection.java:236)
  at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
  at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
  at java.sql.DriverManager.getConnection(Unknown Source)
  at java.sql.DriverManager.getConnection(Unknown Source)
  at at.nje.DataBaseOp.ConnectOrcl.openCon(ConnectOrcl.java:73)
  at at.nje.Kone.GUI.LoginFrame.login(LoginFrame.java:126)
  at at.nje.Kone.GUI.LoginFrame$3.actionPerformed(LoginFrame.java:67)
  at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
  at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
  at java.awt.Component.processMouseEvent(Unknown Source)
  at javax.swing.JComponent.processMouseEvent(Unknown Source)
  at java.awt.Component.processEvent(Unknown Source)
  at java.awt.Container.processEvent(Unknown Source)
  at java.awt.Component.dispatchEventImpl(Unknown Source)
  at java.awt.Container.dispatchEventImpl(Unknown Source)
  at java.awt.Component.dispatchEvent(Unknown Source)
  at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
  at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
  at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
  at java.awt.Container.dispatchEventImpl(Unknown Source)
  at java.awt.Window.dispatchEventImpl(Unknown Source)
  at java.awt.Component.dispatchEvent(Unknown Source)
  at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
  at java.awt.EventQueue.access$200(Unknown Source)
  at java.awt.EventQueue$3.run(Unknown Source)
  at java.awt.EventQueue$3.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
  at java.awt.EventQueue$4.run(Unknown Source)
  at java.awt.EventQueue$4.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
  at java.awt.EventQueue.dispatchEvent(Unknown Source)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  at java.awt.EventDispatchThread.run(Unknown Source)
  </code></pre>
  
  <p>thanks in advance</p>
  
  <p>EDIT:</p>
  
  <p>here is the code of my openCon() method:</p>
  
  <pre><code>    public final Connection openCon(){  
          Properties prop = new Properties();
          try{
              System.out.println("openCon():  URL = "+url+" username = "+user+" password = "+ pw);
              prop.setProperty("user", user);
              prop.setProperty("password", pw);
              con = (OracleConnection) DriverManager.getConnection(url,prop); 
              con.setAutoCommit(false);
              System.out.println("oracle con ok");            
              return con;
          }catch(SQLException e){
              e.printStackTrace();
              String errMessage = e.getMessage();
              System.out.println("oracle con not opened because: "+errMessage);
              JOptionPane.showMessageDialog (null, "Login Error", "You shall not pass", JOptionPane.INFORMATION_MESSAGE);         
          }
          return null;
  }
  </code></pre>
  
  <p>EDIT:
  ok thanks i tried this but still getting ora 28001:</p>
  
  <pre><code>            public final Connection openCon(){  
      Properties prop = new Properties();
          try{
              System.out.println("openCon():  URL = "+url+" username = "+user+" password = "+ pw);
              prop.setProperty("user", user);
              prop.setProperty("password", pw);
              con = (OracleConnection) DriverManager.getConnection(url,prop); 
              con.setAutoCommit(false);
              System.out.println("oracle con ok");            
              return con;
          }catch(SQLException e){
              e.printStackTrace();
              String errMessage = e.getMessage();
              System.out.println("oracle con not opened because: "+errMessage);
              EnterNewPwPanel panel = new EnterNewPwPanel();
              if (e.getErrorCode() == 28001) {                    
                  int result = JOptionPane.showConfirmDialog(null, panel, 
                          "Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION);
                  if(result == JOptionPane.OK_OPTION){
                      String npw = panel.getNewPw();
                      System.out.println("npw: "+npw);
  
                      prop.setProperty("user", user); // username is correct
                      prop.setProperty("password", pw); // pw is expired but correct
                      prop.setProperty(OracleConnection.CONNECTION_PROPERTY_SET_NEW_PASSWORD, npw);
                          try {
                              con = (OracleConnection) DriverManager.getConnection (url,prop);
                          } catch (SQLException e1) {
                              e1.printStackTrace();
                              System.out.println("setting new pw didnt work after all");
                          }
                  }else
                      System.out.println("your pw will expire in x days maddafakka");
              } else {
                  JOptionPane.showMessageDialog (null, "Login Error", "You shall not pass", JOptionPane.INFORMATION_MESSAGE);   
                  //throw(ex);        // for example, just let our caller handle it
              }
  
          }
          return null;
  }
  </code></pre>
  
  <p>after that i get the same error output as i got before</p>
  
  <p>EDIT AGAIN:</p>
  
  <p>well, i thought i had itbut no, didnt work after all, nether the field + new password nor the fields value + new pw worked with this url: 
  <code>static String url = "jdbc:oracle:thin:@//192.168.97.10:1521/orcl"</code>:
  not this</p>
  
  <pre><code>    prop.setProperty("user", user);
                      prop.setProperty("password", pw);                       
                      prop.setProperty("OracleConnection.CONNECTION_PROPERTY_SET_NEW_PASSWORD", npw);
  </code></pre>
  
  <p>nor this</p>
  
  <pre><code>    prop.setProperty("OciNewPassword", npw);
  </code></pre>