简体   繁体   English

如何通过 jdbc 运行命令“describe table_name”来查询 Oracle 数据库

[英]How to run command "describe table_name" via jdbc to query Oracle database

I know how to run normal select query.我知道如何运行正常的选择查询。

        Class.forName("oracle.jdbc.driver.OracleDriver");
        String url = "jdbc:oracle:thin:@xxx";
        String uname = "";
        String passwd = "";
        Connection conn = DriverManager.getConnection(url, uname, passwd);
        Statement stmt = conn.createStatement();
        String sql = "select * from table_name";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println();
        }

But how to run query like sql = "describe table_name" ?但是如何运行像sql = "describe table_name"这样的查询呢?

describe is a SQL*Plus command; describe是一个 SQL*Plus 命令; although it works elsewhere (such as in SQL Developer or TOAD), it is not a "standard" command so I don't think you can use it the way you wanted.尽管它可以在其他地方使用(例如在 SQL Developer 或 TOAD 中),但它不是“标准”命令,所以我认为您不能按照自己的方式使用它。

Therefore, as you already know how to run a "normal" query, do it again, but this time by fetching data from user_tab_columns which contains data you need.因此,既然您已经知道如何运行“正常”查询,请再次执行此操作,但这次是从包含您需要的数据的user_tab_columns中获取数据。 For example:例如:

SQL> SELECT column_name, data_type, data_precision, data_length, nullable
  2  from user_tab_columns
  3  where table_name = 'TEMP';

COLUMN_NAME     DATA_TYPE       DATA_PRECISION DATA_LENGTH N
--------------- --------------- -------------- ----------- -
ID              NUMBER                                  22 Y
ENAME           VARCHAR2                                10 Y
JOB             VARCHAR2                                15 Y
DEPT            NUMBER                                  22 Y
HIREDATE        DATE                                     7 Y
LOC             VARCHAR2                                10 Y

6 rows selected.

which can be compared to可以类比

SQL> describe temp
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 NUMBER
 ENAME                                              VARCHAR2(10)
 JOB                                                VARCHAR2(15)
 DEPT                                               NUMBER
 HIREDATE                                           DATE
 LOC                                                VARCHAR2(10)

SQL>

As of comments : there's that nice view named dictionary you can query and find some useful information, ie a system view which then lets you find another information.作为评论:有一个名为dictionary漂亮视图,您可以查询并找到一些有用的信息,即系统视图,然后可以让您找到其他信息。 Here's how:就是这样:

SQL> select * from dictionary
  2  where lower(table_name) like '%user%' and lower(comments) like '%comment%';

TABLE_NAME                COMMENTS
------------------------- --------------------------------------------------
USER_COL_COMMENTS         Comments on columns of user's tables and views
USER_INDEXTYPE_COMMENTS   Comments for user-defined indextypes
USER_MVIEW_COMMENTS       Comments on materialized views owned by the user
USER_OPERATOR_COMMENTS    Comments for user-defined operators
USER_TAB_COMMENTS         Comments on the tables and views owned by the user

OK;好的; it is user_tab_comments and user_col_comments I need.我需要的是user_tab_commentsuser_col_comments So let's add some comments to the temp table:所以让我们在temp表中添加一些注释:

SQL> comment on table temp is 'Sample table for Stack Overflow';

Comment created.

SQL> comment on column temp.ename is 'Employee''s name';

Comment created.

Result:结果:

SQL> select * from user_tab_comments where table_name = 'TEMP';

TABLE_NAME                TABLE_TYPE  COMMENTS
------------------------- ----------- --------------------------------------------------
TEMP                      TABLE       Sample table for Stack Overflow

SQL> select * from user_col_comments where table_name = 'TEMP';

TABLE_NAME                COLUMN_NAME     COMMENTS
------------------------- --------------- --------------------------------------------------
TEMP                      ID
TEMP                      ENAME           Employee's name
TEMP                      JOB
TEMP                      DEPT
TEMP                      HIREDATE
TEMP                      LOC

6 rows selected.

SQL>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM