Is there a simple, native Java method for saving data?

I think I'm new to basic Java - Android and .NET, have simple ways to store application-specific data for private databases, but I don't see such a method for storing data in simple OL Java.

What am I missing here? Hibernate and JPA seem a bit overkill.


Edit: I have not looked for many data storages in my application (yet) - only a permanent record of several settings, such as the current user, should be good enough. I mainly looked for analog generic settings for Android or .Net.

(This is after the fact, however, since I received many answers.)

+4
source share
4 answers

API Preferences , 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.


Edit:

, /, Preferences API ( ) API Properties, API Preferences .

// 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");
+3

- /.

Hibernate/JPA, Derby. , . http://db.apache.org/derby/

. .

+1

, "".

Properties.

public class Program {
    static final Properties Prefs = new Properties();
    static final String Default_Path = "prefs.properties";

    static void loadPreferences() throws IOException { 
        try (FileInputStream fis = new FileInputStream(Default_Path)) {
            Prefs.loadFromXML(fis);
        } catch (FileNotFoundException ex) { 
            // Okay - if not found we'll save it later
        }
    }

    static void savePreferences() throws FileNotFoundException, IOException {
        try (FileOutputStream fos = new FileOutputStream(Default_Path)) {
            Prefs.storeToXML(fos, "Program Preferences");
        }

    }

    public static void main(String[] args) throws IOException {
        loadPreferences();

        if (Prefs.getProperty("FavoriteColor") == null) {
            Prefs.setProperty("FavoriteColor", "black"); 
        }

        System.out.println(Prefs.getProperty("FavoriteColor"));

        savePreferences();
    }
}

XML, prefs.properties:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Program Preferences</comment>
<entry key="FavoriteColor">black</entry>
</properties>

  • Properties.put(). Properties.setProperty() Properties.getProperty().
  • , Properties, , Hashtable.

. . ( , , - , ...).

  • Serialzation . , , .
  • implements Serializable , Singletons .
  • implements Serializablealso reveals all of your fields except those that you declare as transient. This means that the private parts of your class are part of the public interface. Check it out if you change it later (I mean that later versions are likely to violate it without careful monitoring.)
  • this list is incomplete ...

Some argue that serialization can be done correctly, but it's hard to do it right.

+1
source

All Articles