我有一个Oracle 11g数据库,并且已经编写了一个应用程序,它能够创建用户,并且还有许多其他功能。我希望使用该帐户的最终用户在创建后更改密码,因此我在创建用户时设置了pasword expire
子句。
所以现在我的问题是如何在Java中处理ORA-28001: the password has expired
异常,以便用户能够根据需要更改密码。 grace period
设置为2天。
那就是如果需要的话我得到的整个错误消息:
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.<init>(PhysicalConnection.java:546)
at oracle.jdbc.driver.T4CConnection.<init>(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)
提前致谢
编辑:
这是我的openCon()方法的代码:
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;
}
编辑:
好的,谢谢,我尝试了这个,但仍然得到ora 28001:
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;
}
之后,我得到与以前一样的错误输出
再次编辑:
好吧,我以为我有它,但没有,毕竟没有用,该字段+新密码或字段值+新密码都与此网址一起工作:
static String url = "jdbc:oracle:thin:@//192.168.97.10:1521/orcl"
:不是这个
prop.setProperty("user", user);
prop.setProperty("password", pw);
prop.setProperty("OracleConnection.CONNECTION_PROPERTY_SET_NEW_PASSWORD", npw);
也不是
prop.setProperty("OciNewPassword", npw);
最佳答案
如果您发布一些代码,将会有所帮助。堆栈提示有一个swing应用程序,当您按下按钮时,该应用程序将启动oracle连接。
所以您要做的就是在您的actionPerformed中执行:
try {
// login to the database
} catch (SqlException ex) {
if (ex.getErrorCode() == 28001) {
// do whatever makes sense here, pop up a dialog, ask the user for the new password
} else {
// do whatever makes sense if you get a different error code
throw(ex); // for example, just let our caller handle it
}
}
要了解在设置Oracle连接时如何指定新密码,请参见the documentation,尤其是CONNECTION_PROPERTY_SET_NEW_PASSWORD。
编辑:
免责声明:我现在不在电脑上可以进行测试。
DriverManager类具有各种不同的指定参数的方法。其中之一是您在上述代码中使用的
DriverManager.getConnection(String url, String user, String password)
方法。还有一个DriverManager.getConnection(String url, Properties info)
,它也可以传递其他可能是数据库特定的属性。使用此方法时,需要将用户名和密码放入“属性”集中:
java.util.Properties info = new java.util.Properties();
info.put ("user", "scott");
info.put ("password","tiger");
con = DriverManager.getConnection ("jdbc:oracle:oci8:@",info);
现在,您可以在创建连接之前添加任何其他记录的属性:
java.util.Properties info = new java.util.Properties();
info.put ("user", "scott");
info.put ("password","tiger");
info.put (OracleDriver.CONNECTION_PROPERTY_SET_NEW_PASSWORD, "gorilla");
con = DriverManager.getConnection ("jdbc:oracle:oci8:@",info);
当您尝试登录时,它应该更改密码(前提是旧密码正确但已过期)。
我发现hint on the internet瘦驱动程序可能不支持该驱动程序,而您需要本机驱动程序,但是该页面没有日期戳,并且瘦驱动程序对版本11产生了很大的喜爱,因此,很可能与瘦驱动一起使用司机。