0

我从网上复制了这段代码,并为我的数据库做了一些必要的更改

public class MainClass {

静态连接连接;

public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println("Usage: java JavaDBDemo"); System.exit(1); }

String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String dbName = "JeeteshBD";
String connectionURL = "jdbc:derby:" + dbName + ";";
String createString = "CREATE TABLE SANKYA.ADDRESS (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)";
Class.forName(driver);

conn = DriverManager.getConnection(connectionURL);

Statement stmt = conn.createStatement();
stmt.setPoolable(true);
stmt.execute(createString);


PreparedStatement psInsert = conn
    .prepareStatement("insert into SANKYA.ADDRESS values (?,?)");

psInsert.setString(1, args[0]);
psInsert.setString(2, args[1]);

psInsert.executeUpdate();

Statement stmt2 = conn.createStatement();
ResultSet rs = stmt2.executeQuery("select * from SANKYA.ADDRESS");
System.out.println("Addressed present in your Address Book\n\n");
int num = 0;

while (rs.next()) {
  System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address"
      + rs.getString(2));
}
rs.close();

} }

执行以下代码后,我在控制台中收到以下错误。

Exception in thread "main" java.sql.SQLException: Table/View 'ADDRESS' already exists in Schema 'SANKYA'.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at MainClass.main(MainClass.java:26)

原因:java.sql.SQLException:表/视图“地址”已存在于模式“SANKYA”中。在 org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) ... 10 更多原因:错误 X0Y32:表/视图架构“SANKYA”中已存在“地址”。在 org.apache.derby.iapi.error.StandardException.newException(Unknown Source) at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.duplicateDescriptorException(Unknown Source) at org.apache.derby.impl.sql.catalog .DataDictionaryImpl.addDescriptor(Unknown Source) at org.apache.derby.impl.sql.execute.CreateTableConstantAction.executeConstantAction(Unknown Source) at org.apache.derby.impl.sql.execute.MiscResultSet。

但是当我查看 Eclipse 中的数据库透视图时,更新还没有完成,任何人都可以对此有所了解吗?

这是在桌面上创建的 derby.log

----------------------------------------------------------------

2013-03-11 05:10:17.241 GMT:引导 Derby 版本 Apache 软件基金会 - Apache Derby - 10.5.1.1 - (764942):数据库目录 /home/sankya/JeeteshBD 上的实例 c013800d-013d-57db-3673-0000018a21b0

数据库类加载器已启动 - derby.database.classpath=''

格林威治标准时间 2013-03-11 05:10:17.639:

关闭实例 c013800d-013d-57db-3673-0000018a21b0


2013-03-11 05:10:27.773 GMT:引导 Derby 版本 Apache 软件基金会 - Apache Derby - 10.5.1.1 - (764942):数据库目录 /home/sankya/JeeteshBD 上的实例 a816c00e-013d-57db-3673-0000018a21b0

数据库类加载器已启动 - derby.database.classpath=''

格林威治标准时间 2013 年 3 月 11 日 06:22:45.639:

关闭实例 a816c00e-013d-57db-3673-0000018a21b0

4

2 回答 2

0
stmt.execute(createString); 

上面的行给出了 sql 异常。所以下一行无法执行。很明显,该表存在于您的数据库中。正确检查您的数据库..

如果表中存在表,则注释“ stmt.execute(createString);”这个 .. 运行应用程序,它肯定可以工作..

于 2013-03-12T05:40:28.340 回答
0

仅当不存在时才需要创建表。

Connection connection = DriverManager.getConnection(connectionURL);
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet rs = dbmd.getTables(null, "SANKYA", "ADDRESS", null);
if(!rs.next())
{
    Statement stmt = conn.createStatement();
    stmt.setPoolable(true);
    stmt.execute(createString);
}
于 2013-03-12T09:43:09.787 回答