英文:
H2 embedded database intellij idea <-> sources files
问题
在这里有些问题,但不明白出在哪里,我试图在我的Java项目中将H2数据库作为本地的“嵌入式”数据库使用。因此,在生成了我的数据库之后,我使用IntelliJ数据库控制台创建了表和示例数据,但是当我尝试使用源文件连接时,请求失败了。
这是我的IntelliJ入口:
(图片链接)https://i.stack.imgur.com/F5Qo9.png
这是我的源文件入口:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
private Connection conn;
private Statement st;
public Database() {
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
this.conn = DriverManager.getConnection("jdbc:h2:" + "./ava", "root", "password");
System.out.println("Status: connected");
st = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
我就是不明白为什么我不能访问我的数据,当我尝试运行“SELECT * FROM POSTE”时,我得到了以下错误信息:
org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "POSTE" non trouvé
Table "POSTE" not found; SQL statement:
SELECT * FROM POSTE [42102-200]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:453)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
at org.h2.message.DbException.get(DbException.java:205)
at org.h2.message.DbException.get(DbException.java:181)
at org.h2.command.Parser.readTableOrView(Parser.java:7628)
(...)
英文:
something wrong here but don't understand where, im trying to use H2 database as a local, "Embedded" database for my java project. So after generating my db, i created table and sample data with intellij database console but when i try to connect with sources files i fail my requests
Here is my intellij entry :
https://i.stack.imgur.com/F5Qo9.png
And here my source files entry :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
private Connection conn;
private Statement st;
public Database() {
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
this.conn = DriverManager.getConnection("jdbc:h2:" + "./ava", "root", "password");
System.out.println("Status : connected");
st = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
<!-- end snippet -->
I just don't how understand how i cannot have access to my datas, i have
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "POSTE" non trouvée
Table "POSTE" not found; SQL statement:
SELECT * FROM POSTE [42102-200]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:453)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
at org.h2.message.DbException.get(DbException.java:205)
at org.h2.message.DbException.get(DbException.java:181)
at org.h2.command.Parser.readTableOrView(Parser.java:7628)
(...)
<!-- end snippet -->
when i'm trying to req "SELECT * FROM POSTE"
答案1
得分: 0
./ava
是一个相对数据库路径。 .
表示进程的当前工作目录。您的 IDE 和从该 IDE 启动的应用程序通常具有不同的工作目录。
您需要使用绝对数据库路径(C:\path\to\db
或 /path/to/db
),或者您可以使用相对于用户的主目录的路径(~/…
)。
您还需要确保您的 IDE 中的 DB 工具和应用程序都使用完全相同版本的 H2,因为您使用的是嵌入式数据库。当您在具有相同嵌入式数据库文件的不同 H2 版本之间使用不同版本的 H2 时,该文件可能会损坏。(当您使用 H2 服务器进程时,可以使用不同版本的 H2 驱动程序安全地连接到它。)
英文:
./ava
is a relative database path. .
means the current working directory of a process. Your IDE and your application launched from that IDE usually have different working directories.
You need to use absolute database paths (C:\path\to\db
or /path/to/db
) or you can use paths relative to the home directory of your user (~/…
).
You also need to make sure that DB tool in your IDE and your application both use exactly the same version of H2, because you use embedded databases. When you use different versions of H2 with the same embedded database file, this file may be corrupted. (When you use H2 server process, you may safely connect to it using different versions of H2's drivers.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论