如何在JavaSE中使用Firebase进行CRUD操作(桌面应用程序)

时间:2019-03-10 08:27:27

标签: java firebase firebase-realtime-database crud

我正在尝试对现有Firebase数据库进行CRUD操作。但是我是Firebase的新手。如何改善以下代码?我浏览了google文档,但没有得到很多。

到目前为止,我已经完成了以下工作,

public class FirebaseDB {

    private static final String DATABASE_URL = "DB_URL";
    private static FirebaseDatabase firebaseDatabase;

    public static void main(String[] args) {
        FileInputStream serviceAccount = null;
        try {
            serviceAccount = new FileInputStream("JSON_FILE_PATH");

            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                    .setDatabaseUrl(DATABASE_URL)
                    .build();

            FirebaseApp.initializeApp(options);
            firebaseDatabase = FirebaseDatabase.getInstance(DATABASE_URL);

            FirebaseDatabase database = firebaseDatabase.getInstance();
            DatabaseReference ref = database.getReference();

            DatabaseReference usersRef = ref.child("Users");

            Map<String, User> users = new HashMap<>();
            users.put("alanisawesome", new User("June 23, 1912", "Alan Turing"));
            users.put("gracehop", new User("December 9, 1906", "Grace Hopper"));

            usersRef.setValueAsync(users);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FirebaseDB.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FirebaseDB.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                serviceAccount.close();
            } catch (IOException ex) {
                Logger.getLogger(FirebaseDB.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

如何使用MVC模式的Firebase进行CRUD?如何获得所有控制器类的数据库实例?以及如何UpdateDelete数据?

0 个答案:

没有答案