java中出现错误“ORA-00942:表或视图不存在”

我正在尝试学习连接到oracle数据库


当我以这种方式登录 sqlplus 时:


sqlplus sys/Oracle_1@orcl as sysdba;


我运行这段代码:


connect hr/hr@orclpdb


select count(*) from employees;


我得到这个输出:


 COUNT(*)

----------

       107

但是当我尝试在 java 代码中访问该表时,出现此错误:


ORA-00942: table or view does not exist


这是我的java代码:


class OracleCon{  

public static void main(String args[]){  

try{  

Class.forName("oracle.jdbc.driver.OracleDriver");  

Connection con=DriverManager.getConnection(  

        "jdbc:oracle:thin:hr/hr@localhost:1521/orclpdb","sys as sysdba","Oracle_1"); 

Statement stmt=con.createStatement();  

ResultSet rs=stmt.executeQuery("select count(*) from employees");  

while(rs.next())  

System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  

con.close();  

}catch(Exception e){ System.out.println(e);}  


}  

}  

我应该怎么做才能解决这个问题?


qq_笑_17
浏览 186回答 2
2回答

斯蒂芬大帝

您正在尝试使用 java 代码中的用户连接它sys,但表employee存在于hr架构中。您可以更改://Change this line fromConnection con=DriverManager.getConnection(          "jdbc:oracle:thin:hr/hr@localhost:1521/orclpdb","sys as sysdba","Oracle_1"); // toConnection con=DriverManager.getConnection(          "jdbc:oracle:thin:hr/hr@localhost:1521/orclpdb","hr","hr"); 或者您可以更改从表中获取数据的方式employee(使用其架构查询表)//Change this line fromResultSet rs=stmt.executeQuery("select count(*) from employees"); //toResultSet rs=stmt.executeQuery("select count(*) from hr.employees"); 干杯!!

幕布斯7119047

请尝试一下这段代码。它必须工作public static void conn() throws ClassNotFoundException, SQLException{      Connection con=DriverManager.getConnection(              "jdbc:oracle:thin:@localhost:1521:orclpdb", "HR", "HR");     Statement stmt=con.createStatement();      ResultSet rs=stmt.executeQuery("select count(*) from dual");      while(rs.next())          System.out.println(rs.getInt(1));      con.close();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java