Java 如何让Derby使用JDBC实际创建数据库?

Java 如何让Derby使用JDBC实际创建数据库?,java,jdbc,derby,Java,Jdbc,Derby,我的DatabaseMetaData方法调用和IJ都表示没有创建名为audioPlayerDB的数据库。有人能告诉我我的代码丢失了什么吗?(我正在为桌面应用程序创建数据库,因此不需要InitialContext对象) 编辑:准备好的字符串只是一个占位符 import java.io.PrintWriter; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement

我的DatabaseMetaData方法调用和IJ都表示没有创建名为audioPlayerDB的数据库。有人能告诉我我的代码丢失了什么吗?(我正在为桌面应用程序创建数据库,因此不需要InitialContext对象)

编辑:准备好的字符串只是一个占位符

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.derby.drda.NetworkServerControl;
import org.apache.derby.jdbc.EmbeddedDataSource;

public class DatabaseInit {


static NetworkServerControl mediaServer;
static EmbeddedDataSource dataSource;
static Connection con;
static PreparedStatement preparedStatement = null;

public static void main(String[] args) throws SQLException, Exception {


    mediaServer = new NetworkServerControl();
    mediaServer.start(new PrintWriter(System.out, true));
    Thread.sleep(3000);

    dataSource = new EmbeddedDataSource();
    dataSource.setDatabaseName("audioPlayerDB");
    dataSource.setCreateDatabase("create");
    dataSource.setDescription("Embedded server for audio player");
    System.out.println(" Description= " + dataSource.getDescription());

    con = dataSource.getConnection();


    String createString = "CREATE TABLE AUDIO_LIST  "
    +  "(AUDIO_ID INT NOT NULL GENERATED ALWAYS AS IDENTITY " 
    +  "   CONSTRAINT AUDIO_PK PRIMARY KEY, " 
    +  " ENTRY_DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "
    +  " AUDIO_ITEM VARCHAR(32) NOT NULL) " ;


    preparedStatement = con.prepareStatement(createString);

    System.out.println(createString);

    // execute create SQL stetement
    preparedStatement.executeUpdate();
    DatabaseMetaData meta = con.getMetaData();
    ResultSet res = meta.getTables(null, null, null,
            new String[]{"TABLE"});
    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"));
    }

    mediaServer.shutdown();

}
}

从我读得不够好的错误消息中:

Exception in thread "main" java.sql.SQLException: Table/View 'AUDIO_LIST' already exists in Schema 'APP'.
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.EmbedPreparedStatement.executeStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeLargeUpdate(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeUpdate(Unknown Source)
at DatabaseInit.main(DatabaseInit.java:48)

使用Derby的嵌入式驱动程序时,如果要创建的数据库没有用户名,则使用的默认模式为“APP”。我在数据库创建中添加了一个用户名。DB是“创建”的,但方案在某种程度上是我的用户名。

您观察到的(默认模式与您的用户名相同)是预期的和记录的行为。请注意,在应用程序以外的默认模式中创建对象时,会根据需要创建这些模式。因此,第一次以“jdoe”的身份连接,然后在创建任何表之前列出模式,并不会向您显示模式“jdoe”。

也许您应该重新命名问题或对其重新命名,以表明问题实际上是关于如何选择默认模式名的。