我的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();
}
}
答案 0 :(得分:1)
从我没有阅读的错误消息:
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”。我在数据库创建中添加了一个用户名。数据库是“创建的”,但该方案在某种程度上是我的用户名。
答案 1 :(得分:1)
您观察到的内容(默认架构与您的用户名相同)是预期的和记录的行为。请注意,在APP中创建对象时,将按需创建除APP之外的默认架构。因此,作为' jdoe'这是第一次,然后在创建任何表之前列出模式,不会向您显示模式' jdoe'。