简体   繁体   中英

Play Framework for Java 2.1 and MongoDB

I'm about to build a project in Play Framework for Java 2.1 and I would like to use MongoDB to store data. I've installed both Play 2.1 and MongoDB 2.4.4.

Is it possible to build a Play! project with only the MongoDB Java Driver? I've actually tried to download the mongo-java-driver.jar which I placed in a lib / folder in the root of my play project according to this documentation . Then I've created a model in which I've added the code below but I can't get it to work. Do you know any tutorials or what am I doing wrong? I get compilation errors like unreported exception java.net.UnknownHostException; must be caught or declared to be thrown unreported exception java.net.UnknownHostException; must be caught or declared to be thrown

import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;

MongoClient mongoClient = new MongoClient();

If you take a look at the documentation , MongoClient() constructor may throw an UnknownHostException , which is a checked exception. You may either catch this exception in your code or add throws UnknownHostException declaration to your method.

Also read this for exceptions in Java and this for checked vs. unchecked exceptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM