【问题标题】:Java PreparedStatement Comment on TableJava PreparedStatement 对表的注释
【发布时间】:2016-12-27 02:29:10
【问题描述】:

我熟悉使用 java 准备好的语句在表上插入/更新。在 oracle 中,您可以在表格上添加注释,我将如何使用准备好的语句来做到这一点?

这是我最初的尝试,没有运气;

PreparedStatement stmt = con.prepareStatement("comment on table my_table is q'[?]'");
stmt.setString(1, description);
stmt.executeUpdate();

【问题讨论】:

  • comment 是一个 DDL 语句,因此您不能使用绑定变量,就像您不能在 create table 语句中使用绑定变量一样。您需要组装完整的 SQL 语句并将其发送给 Oracle。另外,DDL 语句实际上是在准备阶段执行的,所以使用 PreparedStatement 是没有意义的。

标签: java sql oracle prepared-statement


【解决方案1】:

您可以使用系统 Oracle 表并在其中使用PreparedStatement 设置注释,如下所示:

PreparedStatement stmt = con.prepareStatement(
    "UPDATE user_tab_comments SET comments = ? WHERE table_name = 'my_table'");

或者尝试使用简单的语句:

String commentOnTableSQL = String.format("COMMENT ON TABLE my_table is '%s'", comment);
Statement statement = dbConnection.createStatement();
statement.execute(commentOnTableSQL);

【讨论】:

  • 我希望在user_tab_comments 上的update 会失败。直接更新数据字典表是一个糟糕的主意。它不受支持,如果它有效,那几乎总是会引入某种微妙的损坏。 Oracle 通常会阻止您这样做,但如果您有足够的权限并且“幸运”,您可能会发现您在技术上能够修改的视图。
猜你喜欢
  • 1970-01-01
  • 2014-12-27
  • 2012-01-22
  • 2016-12-10
  • 2010-09-06
  • 2011-01-31
  • 1970-01-01
相关资源
最近更新 更多