我尝试从我正在开发的程序内部创建数据库文件:
if (!nome.getText().isEmpty()) {
if (disco.getSelectedIndex() == 2) {
if (!outroDisco.getText().isEmpty() && outroDisco.getText().length() == 1) {
nomeDiretorio = outroDisco.getText() + ":/" + "Users/" + System.getProperty("user.name") + "/";
} else {
JOptionPane.showMessageDialog(null, "Informar disco onde H2 foi instalado, somente a letra");
}
} else {
nomeDiretorio = (String) disco.getSelectedItem() + "/Users/" + System.getProperty("user.name") + "/";
}
nomeArquivo = nome.getText() + ".mv.db";
File diretorio = new File(nomeDiretorio);
if (!diretorio.exists()) {
diretorio.mkdirs();
}
File arquivo = new File(diretorio, nomeArquivo);
try {
arquivo.createNewFile();
String conteudo = "Este é o conteúdo do arquivo.";
FileOutputStream fos = new FileOutputStream(arquivo);
fos.write(conteudo.getBytes());
fos.close();
System.out.println("Arquivo criado com sucesso em: " + arquivo.getAbsolutePath());
} catch (IOException e) {
System.out.println("Erro ao criar o arquivo: " + e.getMessage());
}
}
在正确的路径中成功创建了文件,但当我尝试在H2控制台中连接时,出现以下异常:
IO Exception: "C:/Users/lopes/teste3.mv.db" [90028-214] 90028/90028 (Help)
org.h2.jdbc.JdbcSQLNonTransientException: IO Exception: "C:/Users/lopes/teste3.mv.db" [90028-214]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:554)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:477)
at org.h2.message.DbException.get(DbException.java:212)
at org.h2.mvstore.db.Store.convertMVStoreException(Store.java:171)
at org.h2.mvstore.db.Store.<init>(Store.java:145)
at org.h2.engine.Database.<init>(Database.java:324)
at org.h2.engine.Engine.openSession(Engine.java:92)
at org.h2.engine.Engine.openSession(Engine.java:222)
at org.h2.engine.Engine.createSession(Engine.java:201)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:338)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:122)
at org.h2.util.JdbcUtils.getConnection(JdbcUtils.java:288)
at org.h2.server.web.WebServer.getConnection(WebServer.java:808)
at org.h2.server.web.WebApp.test(WebApp.java:972)
at org.h2.server.web.WebApp.process(WebApp.java:241)
at org.h2.server.web.WebApp.processRequest(WebApp.java:176)
at org.h2.server.web.WebThread.process(WebThread.java:152)
at org.h2.server.web.WebThread.run(WebThread.java:101)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: org.h2.mvstore.MVStoreException: Reading from file sun.nio.ch.FileChannelImpl@790020ad failed at 31 (length 31), read 31, remaining 8161 [2.1.214/1]
at org.h2.mvstore.DataUtils.newMVStoreException(DataUtils.java:1004)
at org.h2.mvstore.DataUtils.readFully(DataUtils.java:470)
at org.h2.mvstore.FileStore.readFully(FileStore.java:98)
at org.h2.mvstore.MVStore.readStoreHeader(MVStore.java:837)
at org.h2.mvstore.MVStore.<init>(MVStore.java:463)
at org.h2.mvstore.MVStore$Builder.open(MVStore.java:4082)
at org.h2.mvstore.db.Store.<init>(Store.java:136)
... 14 more
Caused by: java.io.EOFException
at org.h2.mvstore.DataUtils.readFully(DataUtils.java:458)
... 19 more
奇怪的是,如果我手动创建文件,控制台会连接,但使用程序创建时会发生此问题
当我的程序在Netbeans上运行并在正确的路径中以正确的名称创建了. mv.db文件后,H2控制台无法连接数据库,但如果我手动创建该文件,它就可以工作,但这不是我所需要的,因为我希望用户创建自己的DB
2条答案
按热度按时间r6hnlfcb1#
不能通过创建文件来创建数据库。
你需要使用这样的东西:
hfsqlsce2#
问题解决了该文件必须是空白的,我是写在它,你可以看到里面的'尝试'