如果
Preferences API不够全面并且Hibernate / JPA似乎过多,那么可能是最简单的中间替代方案,也是最容易学习的,在数据库中存储应用程序数据的方法是
Java JDBC.
简单的例子如下;
// Database URL including driver, username, password, database name etc.
String URL = "jdbc:oracle:thin:username/password@amrood:1521:EMP";
Connection conn = DriverManager.getConnection(URL);
// Create your SQL Query.
PreparedStatement st = conn.prepareStatement("UPDATE YourTable SET YourColumn = 12 WHERE TableID = 1337");
// Execute (If your statement doesn't need to return results).
st.execute();
SQLite可以在本地和您的应用程序中运行.驱动程序也可以集成.无需服务器.因此,在这两者之间,您可以以SQL数据库的形式进行简单的本地持久化.
编辑:
在看到您只需要持久设置/首选项后,我会推荐较旧的Properties API(上面链接的教程),因为Preferences API会更优雅地处理文件位置.
例;
// This will define a node in which the preferences can be stored
prefs = Preferences.userRoot().node(this.getClass().getName());
// First we get the values
// Define a boolean value with the default true
prefs.getBoolean("Test1", true);
// Define a string with default "Hello World"
prefs.get("Test2", "Hello World");
// Define a integer with default 50
prefs.getInt("Test3", 50);
// now set the values
prefs.putBoolean("Test1", false);
prefs.put("Test2", "Hello Europa");
prefs.putInt("Test3", 45);
// Delete the preference settings for the first value
prefs.remove("Test1");