通过java进行Derby数据库更新?

时间:2013-03-12 05:36:25

标签: java database

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

public class MainClass {

静态连接conn;

public static void main(String [] args)throws Exception {     if(args.length!= 2){       System.out.println(“用法: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:表/视图'ADDRESS'已存在于模式'SANKYA'中。     at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(未知来源)     at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)     ......还有10个 引起:错误X0Y32:表/视图'地址'已存在于模式'SANKYA'中。     at 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(未知来源)     at org.apache.derby.impl.sql.execute.CreateTableConstantAction.executeConstantAction(Unknown Source)     在org.apache.derby.impl.sql.execute.MiscResultSet.open(未知来源)     at org.apache.derby.impl.sql.GenericPreparedStatement.executeStmt(未知来源)     在org.apache.derby.impl.sql.GenericPreparedStatement.execute(未知来源)     ......还有4个

但是当我查看Eclipse中的数据库透视图时,没有完成更新,有人可以对此进行一些说明吗?

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

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

2013-03-11 05:10:17.241 GMT:  引导Derby版本Apache Software Foundation - Apache Derby - 10.5.1.1 - (764942):instance c013800d-013d-57db-3673-0000018a21b0 在数据库目录/ home / sankya / JeeteshBD

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

2013-03-11 05:10:17.639 GMT:

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


2013-03-11 05:10:27.773 GMT:  引导Derby版本Apache Software Foundation - Apache Derby - 10.5.1.1 - (764942):实例a816c00e-013d-57db-3673-0000018a21b0 在数据库目录/ home / sankya / JeeteshBD

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

2013-03-11 06:22:45.639 GMT:

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

2 个答案:

答案 0 :(得分:0)

stmt.execute(createString); 

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

如果您的表格中存在表格,则评论“stmt.execute(createString);”此... 运行应用程序肯定是..

答案 1 :(得分: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);
}