Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.j256.ormlite.dao.Dao;
- import com.j256.ormlite.dao.DaoManager;
- import com.j256.ormlite.field.DatabaseField;
- import com.j256.ormlite.jdbc.JdbcConnectionSource;
- import com.j256.ormlite.table.DatabaseTable;
- import com.j256.ormlite.table.TableUtils;
- import javafx.beans.property.SimpleStringProperty;
- import javafx.beans.property.StringProperty;
- import java.io.File;
- import java.nio.file.FileSystems;
- import java.util.List;
- /**
- * @since 2016-07-15 04:12
- */
- public class Main {
- @DatabaseTable(tableName = "a")
- public static class A {
- @DatabaseField(generatedId = true, columnName = "id")
- private long id;
- @DatabaseField(columnName = "value", persisterClass = StringPropertyPersister.class)
- private final StringProperty value;
- protected A() {
- value = new SimpleStringProperty();
- }
- public A(String token) {
- value = new SimpleStringProperty(token);
- }
- public String getValue() { return value.get(); }
- public StringProperty valueProperty() { return value; }
- }
- private static void initDB() throws Exception {
- final File path = FileSystems.getDefault().getPath(System.getProperty("user.home"), "Desktop", "test").toFile();
- final String DATABASE_URL = "jdbc:h2:" + path.getAbsolutePath();
- Class.forName("org.h2.Driver");
- connectionSource = new JdbcConnectionSource(DATABASE_URL);
- connectionSource.initialize();
- TableUtils.createTableIfNotExists(connectionSource, A.class);
- }
- private static void insertMockData() throws Exception {
- Dao<A, Integer> dao = DaoManager.createDao(connectionSource, A.class);
- dao.executeRaw("INSERT INTO a (id, value) VALUES (NULL, NULL)");
- }
- private static List<A> fetchResults() throws Exception {
- Dao<A, Integer> dao = DaoManager.createDao(connectionSource, A.class);
- return dao.queryForAll();
- }
- private static JdbcConnectionSource connectionSource;
- public static void main(String[] args) throws Exception {
- initDB();
- insertMockData();
- List<A> results = fetchResults();
- for (A result : results) {
- System.out.println(result.getValue()); // throws NullPointerException
- }
- }
- }
Add Comment
Please, Sign In to add comment