Java-MongoDB Tutorial
从mongodb官网复制的,看起来方便点...http://www.agoit.com/images/smiles/icon_biggrin.gifhttp://www.mongodb.org/display/DOCS/Java+Tutorial
Introduction
This page is a brief overview of working with the MongoDB Java Driver.
For more information about the Java API, please refer to the online API Documentation for Java Driver
A Quick Tour
Using the Java driver is very simple. First, be sure to include the driver jar mongo.jar in your classpath. The following code snippets come from the examples/QuickTour.java example code found in the driver.
Making A Connection
To make a connection to a MongoDB, you need to have at the minimum, the name of a database to connect to. The database doesn't have to exist - if it doesn't, MongoDB will create it for you.
Additionally, you can specify the server address and port when connecting. The following example shows three ways to connect to the database mydb on the local machine :
<div class="code panel" style=""><div class="codeContent panelContent" style="padding-top: 0px; padding-right: 12px; padding-bottom: 0px; padding-left: 12px; font-size: 0.95em; margin: 0px;">import com.mongodb.Mongo;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.BasicDBObject;import com.mongodb.DBObject;import com.mongodb.DBCursor;import com.mongodb.ServerAddress;import java.util.Arrays;Mongo m = new Mongo();// orMongo m = new Mongo( "localhost" );// orMongo m = new Mongo( "localhost" , 27017 );// or, to connect to a replica set, supply a seed list of membersMongo m = new Mongo(Arrays.asList(new ServerAddress("localhost", 27017), new ServerAddress("localhost", 27018), new ServerAddress("localhost", 27019)));DB db = m.getDB( "mydb" );
页:
[1]