I tried to create the database file from inside the program I'm developing:
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());
}
}
the file is successfully created in the correct path, but as I try to connect in the H2 console I get the following exceptions:
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
The curious thing, is that if I create the file manually the console connects, but creating by using the program this problem happens
The H2 console cannot connect the database after my program running on Netbeans creates the .mv.db file in the correct path with the correct name, but it works if I create the file manually, but that's not what I need since I want the user to create his own DB
You cannot create databases by creation of files.
You need to use something like that: