简体   繁体   English

从Java运行Oracle查询

[英]Run Oracle query from Java

I want to run the query: "DESCRIBE table_name;" 我想运行查询:“ DESCRIBE table_name;”

statement = this.connection.createStatement();
ResultSet rset = statement.executeQuery("DESCRIBE table_name");

and I got this error: 我得到这个错误:

 " java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement"

what is the problem? 问题是什么?

DESC[RIBE] is a SQL*Plus command, not a SQL statement. DESC[RIBE]是SQL * Plus命令,而不是SQL语句。 The DESC command queries the Oracle data dictionary, something like: DESC命令查询Oracle数据字典,例如:

select COLUMN_NAME, DATA_TYPE
  from USER_TAB_COLUMNS
 where TABLE_NAME = 'YOUR_TABLE'
describe user2.flights;

Here user2 is database name and flights is table name. 这里的user2是数据库名称,flight是表名称。 Try this. 尝试这个。

Or use next query 或使用下一个查询

select *
  from user_tab_columns
 where table_name = 'MY_TABLE'
 order by column_id;  

Use this query. 使用此查询。

column_id is the "order" of the column in the table. column_id是表中列的“顺序”。

You should ensure that ' MY_TABLE ' is capitalised unless you've been adding tables with casing ( a bad idea ) in which case you need to use something like = "MyTable" 除非您一直使用大写字母添加表(这是一个坏主意),否则应确保大写“ MY_TABLE ”,在这种情况下,您需要使用类似= “ MyTable”

DESC is a SQL*Plus command. DESCSQL*Plus命令。 SO, you cannot use it via JDBC/ODBC. 因此,您不能通过JDBC / ODBC使用它。 An alternative can be like this below. 另一种选择可以像下面这样。

select RPAD(COLUMN_NAME,30)||' '||DATA_TYPE||'('||DATA_LENGTH||')' as descr
FROM all_tab_cols
  WHERE TABLE_NAME = UPPER('YOUR_TABLE') and owner=UPPER('SCHEMA_NAME');

all_tab_cols is a data dictionary table (view) which contains the table metadata all_tab_cols是一个数据字典表 (视图),其中包含表元数据

Oracle's Reference Oracle参考

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

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