【问题标题】:java.sql.SQLException: ORA-00942: table or view does not exist?java.sql.SQLException: ORA-00942: 表或视图不存在?
【发布时间】:2020-10-16 02:40:28
【问题描述】:

我正在使用 甲骨文

SQL*Plus: Release 10.2.0.1.0 - Production on Thu Jun 25 20:17:54 2020

Java

java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)

jar 文件

ojdbc14.jar

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

我在我的 oracle dbms 中创建了一个表

SQL> create Table employees ( name varchar(20));
Table created.

我插入了几条记录

SQL> insert into employees (name) values ('Navjot');

1 row created.

SQL> insert into employees (name) values ('jagjot');


SQL> commit;

Commit complete.

我试图从我的 java 类中获取这些记录但它说不存在这样的表。

BaseDAO.java

import java.sql.*;

    class BaseDAO
    {
        public Connection getConnection()
        {
            String driver="oracle.jdbc.driver.OracleDriver";
            String url="jdbc:oracle:thin:@localhost:1521/xe";
            String user="system";
            String password="system";
            Connection conn=null;
            try
            {
                Class.forName(driver);
                conn=DriverManager.getConnection(url,user,password);
                System.out.println("Connected to oracle database successfully");
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
            return conn;
        }//End of getConnection method
    }//End of BaseDAO class

RetriveAllEmployees.java

import java.sql.*;
public class RetriveAllEmployees extends BaseDAO
{
    public void retrive()
    {
        try
        {
            Connection conn=getConnection();
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery("select * from EMPLOYEES");
            while(rs.next())
            {
                String employeeName=rs.getString("name");
                System.out.println(employeeName);
            }
            
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
    public static void main(String args[])
    {
        RetriveAllEmployees retriveAllEmployees=new RetriveAllEmployees();
        retriveAllEmployees.retrive();
    }
}

当我尝试执行此代码时,它说表不存在

   E:\aman\java\jdbc\jdbc 1 doc examples\003 connection oracle sql>java -classpath .;C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar; RetriveAllEmployees
Connected to oracle database successfully
java.sql.SQLException: ORA-00942: table or view does not exist

当我通过命令行登录oracle时

Enter user-name: system
Enter password:system

Connected to:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production

SQL> conn sys as sysdba;
Enter password:none
Connected.

【问题讨论】:

  • 无法建立与数据库的连接。检查您的连接属性(首先是 JDBC url)
  • 我不明白你的评论。 '“不可能建立与数据库的连接。”@AngeloImmediate。正如你所看到的,当我编译我的 cod 时,它说已成功连接到数据库。 BaseDAO
  • 在以前版本的问题中(在编辑之前),错误消息是:网络适配器无法建立连接这就是我写该评论的原因

标签: java oracle


【解决方案1】:

你需要检查谁是表的所有者:表的所有者是运行CREATE TABLE的用户。

仅当您以表所有者身份连接(或已创建同义词)时,您才能访问没有所有者前缀的表:如果已使用 SYSTEM 用户创建表,则需要以 SYSTEM 用户身份连接 -您可以与其他用户一起使用,但要访问您需要使用SYSTEM.EMPLOYEES 并在SYSTEM.EMPLOYEES 上具有SELECT 权限的表(除非您使用DBA 角色或SYS AS SYSDBA 连接,否则默认情况下您无法拥有该权限)。

ORA-00942 表示表不存在或您没有任何权限访问该表。

【讨论】:

  • 我做到了。但现在错误消息显示java.sql.SQLException: Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor The Connection descriptor used by the client was: localhost:1521:xe
猜你喜欢
  • 2015-01-30
  • 2019-01-22
  • 1970-01-01
  • 2015-11-09
  • 2011-12-10
  • 2016-08-23
  • 2018-02-05
  • 1970-01-01
相关资源
最近更新 更多