表/视图'帐户'已存在于Schema' APP'

时间:2015-06-29 19:13:28

标签: java database jdbc derby

我不是一位经验丰富的数据库开发人员。 使用org.apache.derby.jdbc.EmbeddedDriver运行JDBC程序。当我尝试创建ACCOUNTS表时,我使用以下代码:

  final String DRIVER =
     "org.apache.derby.jdbc.EmbeddedDriver";
  final String CONNECTION =
     "jdbc:derby:AccountDatabase;create=true";


  try {
     Class.forName(DRIVER).newInstance();
  } catch ( ... etc.
  }


  try (Connection connection =
          DriverManager.getConnection(CONNECTION);

       Statement statement =
          connection.createStatement()) {

    statement.executeUpdate(
      "create table ACCOUNTS                       "
      + "  (NAME VARCHAR(32) NOT NULL PRIMARY KEY, "
      + "   ADDRESS VARCHAR(32),                   "
      + "   BALANCE FLOAT)                         ");

但是我收到以下错误消息:

Table/View 'ACCOUNTS' already exists in Schema 'APP'.

然后,当我尝试使用以下代码更新或查询ACCOUNTS表时:

  final String DRIVER =
     "org.apache.derby.jdbc.EmbeddedDriver";
  final String CONNECTION =
     "jdbc:derby:AccountDatabase";


  try {
     Class.forName(DRIVER).newInstance();
  } catch ( .. etc.
  }


  try (Connection connection =
     DriverManager.getConnection(CONNECTION);

     Statement statement =
        connection.createStatement();

     ResultSet resultset =
        statement.executeQuery(
                         "select * from ACCOUNTS")) {

我收到以下错误消息:

java.sql.SQLException: Database 'AccountDatabase' not found.

此外,当我使用以下代码检查是否存在任何表时,我根本没有表格:

        DatabaseMetaData meta = connection.getMetaData();
        ResultSet res = meta.getTables(null, null, null,
                new String[] { "TABLE" });
        System.out.println("List of tables: ");
        while (res.next()) {
            System.out.println("   " + res.getString("TABLE_CAT") + ", "
                    + res.getString("TABLE_SCHEM") + ", "
                    + res.getString("TABLE_NAME") + ", "
                    + res.getString("TABLE_TYPE") + ", "
                    + res.getString("REMARKS"));
        }

有什么建议吗?

0 个答案:

没有答案