oracle In java JDBC connection error ORA-28000: the account got locked, but db account is not locked

euoag5mw  于 2022-12-11  发布在  Oracle
关注(0)|答案(2)|浏览(223)

我可以使用凭据(用户名:HR,密码:*****)。但我收到错误消息
ORA-28000:帐户被锁定
当我尝试使用JDBC从Java程序建立连接时,会出现这种情况。
编码:

public static void main(String args[]) throws Exception{
          Class.forName("oracle.jdbc.driver.OracleDriver");
          String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
          Connection con = DriverManager.getConnection(url,"USER NAME","PASSWORD");
          Statement statement = con.createStatement();  
          ResultSet resultset = statement.executeQuery("select 'Connected' from dual");  

          while (resultset.next()) {
                 System.out.println(resultset.getString(1));
          }        
          statement.close();  
          con.close();  
   }

如何从这个java程序连接到数据库?

gudnpqoy

gudnpqoy1#

When connect using the credentials (Username : system, Password : ####) the account got connected without any issue from java program. However, when connected using the credentials (Username: HR, Password : ****) got the error message ORA-28000: the account got locked. But was able to login using these HR credentials from oracle console. So, to get the connection from java program, did the following:

public static void main(String args[]) throws SQLException, ClassNotFoundException{

          String query;

          // Connect to system Database first
          Class.forName("oracle.jdbc.driver.OracleDriver");
          String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
          Connection con = DriverManager.getConnection(url,"system","####");
          Statement statement = con.createStatement();  
          query = "alter user HR identified by HR account unlock";

          //Unlock the account
          ResultSet resultset = statement.executeQuery(query);

          //Connect to HR database
          con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","HR","****");
          statement = con.createStatement();  

          //Fetch all the tables in HR database
          query = "select 'Connected' from dual";
          resultset = statement.executeQuery(query);

          //Prints records fetched
          while (resultset.next()) {
                 System.out.println(resultset.getString(1));
          }        

          statement.close();  
          con.close();  

   }

The HR account got unlocked and now able to execute other queries without issues :)

bwleehnv

bwleehnv2#

只需将用户名更改为“System”而不是“HR”即可!您现在就可以连接到数据库了。

相关问题