How do i store data into a MongoDb Database using Java?

Sabiq Sabry :

I am fairly new to MongoDB and i've got an issue on how to store data from an Array into a mongodb database? For some reason when i implement the following code i get a lot of errors.

    private void storeData(String[] trainSeats, String[] customerName, String[] traincustomerSeats){

        MongoClient dbb = new MongoClient("localhost", 27017);
        DB dbs = dbb.getDB("Train Customers and Seats");
        DBCollection coll1 = dbs.getCollection("CW1");
        DBCollection coll2 = dbs.getCollection("CW2");
        DBCollection coll3 = dbs.getCollection("CW3");

        BasicDBObject oneDoc = new BasicDBObject();
        for (int k = 1; k < trainSeats.length; k++){
            oneDoc.append(String.valueOf(k), trainSeats[k]);
        }
        coll1.insert(oneDoc);

        BasicDBObject twoDoc = new BasicDBObject();
        for(int k = 1; k < customerName.length; k++){
            twoDoc.append(String.valueOf(k), customerName[k]);
        }
        coll2.insert(twoDoc);

        BasicDBObject threeDoc = new BasicDBObject();
        for(int k = 1; k < traincustomerSeats.length; k++){
            threeDoc.append(String.valueOf(k), traincustomerSeats[k]);
        }
        coll3.insert(threeDoc);

        optionEnter(trainSeats,customerName,traincustomerSeats);
    }

Here are the errors i get when i run the above code:

click to view the errors

enter image description here

umair :

I saw your error log. You are getting this error because your database name "Train Customers and Seats" have lot of spaces ' ', which is not allowed in the database name.

Rename your database to "Train_Customers_and_Seats" or "TrainCustomersAndSeats"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=300222&siteId=1