Java.MongoDB.java Source code

Java tutorial

Introduction

Here is the source code for Java.MongoDB.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Java;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;

/**
 *
 * @author Percy
 */
public class MongoDB {

    private Mongo m = null;
    private DB db = null;
    public DBCollection dbCol = null;
    public DBCursor cursor = null;

    //    public MongoDB() {
    //        m = new Mongo("127.0.0.1", 27017);
    //        db = m.getDB("GMS");
    //    }

    public MongoDB(String table) {
        m = new Mongo("127.0.0.1", 27017);
        db = m.getDB("GMS");
        dbCol = db.getCollection(table);
    }

    public void stopMondo() {
        if (null != m) {
            try {
                m.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            m = null;
            db = null;
        }
    }

    public static String GetDeviceID(String deviceName) {
        MongoDB d_db = new MongoDB("Device");
        d_db.cursor = d_db.dbCol.find(new BasicDBObject().append("DeviceName", deviceName));
        while (d_db.cursor.hasNext()) {
            return d_db.cursor.next().get("DeviceID").toString();
        }
        return "4068643";
    }
}