简体   繁体   English

Play Framework for Java 2.1和MongoDB

[英]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. 我即将在Play Framework for Java 2.1中构建一个项目,我想使用MongoDB来存储数据。 I've installed both Play 2.1 and MongoDB 2.4.4. 我已经安装了Play 2.1和MongoDB 2.4.4。

Is it possible to build a Play! 是否有可能建立一个游戏! project with only the MongoDB Java Driver? 只使用MongoDB Java驱动程序的项目? 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 . 我实际上已经尝试下载mongo-java-driver.jar根据本文档 ,我将它放在我的play项目root中的lib /文件夹中。 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 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. 如果您查看文档MongoClient()构造函数可能会抛出一个UnknownHostException ,这是一个已检查的异常。 You may either catch this exception in your code or add throws UnknownHostException declaration to your method. 您可以在代码中catch此异常,或者向您的方法添加throws UnknownHostException声明。

Also read this for exceptions in Java and this for checked vs. unchecked exceptions. 另请阅读在Java异常, 对于检查与unchecked异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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