我可以使用凭据(用户名:HR,密码:*****)从控制台连接到Oracle XE,DB。但我收到错误消息
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程序连接到DB?
答案 0 :(得分:0)
使用凭据进行连接时(用户名:系统,密码:####),帐户连接时没有任何java程序问题。 但是,使用凭据(用户名:HR,密码: * *)连接时收到错误消息ORA-28000:帐户被锁定。但是能够从oracle控制台使用这些HR凭据登录。 因此,要从java程序获取连接,请执行以下操作:
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();
}
人力资源帐户已解锁,现在可以执行其他查询而不会出现问题:)
答案 1 :(得分:-2)
只需将用户名更改为“系统”,而不是“ HR”。就是这样!您现在可以连接到数据库。