java 本地保存数据_有一个简单的,基于Java的本地方法来保存数据吗?

如果

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");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值