我正在尝试学习连接到oracle数据库
当我以这种方式登录 sqlplus 时:
sqlplus sys/Oracle_1@orcl as sysdba;
我运行这段代码:
连接 hr/hr@orclpdb
从员工中选择count(*);
我得到这个输出:
COUNT(*)
----------
107
但是当我尝试在 java 代码中访问此表时,出现此错误:
ORA-00942: 表或 View 不存在
这是我的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);}
}
}
我应该怎么做才能解决这个问题?
最佳答案
您正在尝试在 Java 代码中使用 sys
用户连接它,但表 employee
存在于 hr
架构中。
您可以更改:
//Change this line from
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:hr/hr@localhost:1521/orclpdb","sys as sysdba","Oracle_1");
// to
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:hr/hr@localhost:1521/orclpdb","hr","hr");
或者
您可以更改从表 employee
中获取数据的方式(使用其架构查询表)
//Change this line from
ResultSet rs=stmt.executeQuery("select count(*) from employees");
//to
ResultSet rs=stmt.executeQuery("select count(*) from hr.employees");
干杯!!
关于java - java中出现错误 "ORA-00942: table or view does not exist ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57917956/