您的位置:首页 > 数据库 > Mongodb

mongodb进行java调用方法

2015-06-17 10:51 573 查看
http://blog.csdn.net/qew110123/article/details/46535103

</pre>进行mongodb数据的简单操作的单利模式的处理(此案例是网上找的,项目中不可能使用该方法,都会是数据库连接池的)<p></p><p></p><pre name="code" class="java">//实例化Mongo对象,连接27017端口
Mongo mongo = new Mongo("localhost", 27017);
//连接名为yourdb的数据库,假如数据库不存在的话,mongodb会自动建立
DB db = mongo.getDB("yourdb");
// Get collection from MongoDB, database named "yourDB"
//从Mongodb中获得名为yourColleection的数据集合,如果该数据集合不存在,Mongodb会为其新建立
DBCollection collection = db.getCollection("yourCollection");
// 使用BasicDBObject对象创建一个mongodb的document,并给予赋值。
BasicDBObject document = new BasicDBObject();
document.put("id", 1001);
document.put("msg", "hello world mongoDB in Java");
//将新建立的document保存到collection中去
collection.insert(document);
// 创建要查询的document
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("id", 1001);
// 使用collection的find方法查找document
DBCursor cursor = collection.find(searchQuery);
//循环输出结果
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
System.out.println("Done");


连接数据的案例在https://git.oschina.net/zongtui/zongtui-filter/commit/ffeace115d006bd252ae7aaae0d60428bd52662b#diff-7

其中使用文件为

9 个文件发生了变化

filter/config/config.xml(进行配置xml文件,进行配置mongodb的ip,端口号和数据库表的名称)
filter/lib/dom4j-1.6.1.jar
filter/lib/jaxen-1.1.1.jar
filter/lib/junit.jar
filter/lib/log4j-over-slf4j-1.6.4.jar
filter/lib/mongo-java-driver-2.12.1.jar(调用的包)
filter/src/main/java/com/zongtui/filter/config/ConfigManager.java(读取xml中的文件)
filter/src/main/java/com/zongtui/filter/mongo/MongoDB.java(调用的模板)
filter/src/main/java/com/zongtui/filter/mongodb/MongoDbConn.java(进行数据库连接池的搭建)


结下了是数据库的真正使用的方法。

进行mongodb数据库连接池的定义。

MongoDbConn.java
package com.zongtui.filter.mongodb;

import java.net.UnknownHostException;
import java.util.List;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;
import com.zongtui.filter.config.ConfigManager;

public class MongoDbConn
{
private static MongoDbConn instance = null;

private String host = null;
private int port = 0;
private List<ServerAddress> replSet = null;
private MongoOptions options = null;
private String database = null;
private String username = null;
private String password = null;
private Mongo mongo = null;
private DB db = null;

private MongoDbConn()
{
this.host = ConfigManager.getInstance().getConfigValue("db_ip");
this.port = Integer.valueOf(ConfigManager.getInstance().getConfigValue("db_port")).intValue();
this.database = ConfigManager.getInstance().getConfigValue("db_name");
//    this.host = "192.168.184.130";
//    this.port = 27017;
//    this.database = "nutch";
// (续上)此处我用的通过xml文件进行数据的读取,可以通过直接写入的方式进行数据的操作,二次进行数据的编写。

}

public static MongoDbConn getInstance()
{
if (instance == null) {
instance = new MongoDbConn();
}
return instance;
}

public void init(String database)
{
this.database = database;
}

public void init(String host, int port, String database) {
this.host = host;
this.port = port;
this.database = database;
}

public void init(String host, int port, String database, String username, String password) {
this.host = host;
this.port = port;
this.database = database;
this.username = username;
this.password = password;
}

public void init(List<ServerAddress> replSet, String database) {
this.replSet = replSet;
this.database = database;
}

public void init(List<ServerAddress> replSet, String database, String username, String password) {
this.replSet = replSet;
this.database = database;
this.username = username;
this.password = password;
}

public MongoOptions getOptions() {
return this.options;
}

public void setOptions(MongoOptions options) {
this.options = options;
}

private void connect()
{
try
{
if ((this.host != null) && (this.port != 0))
{
ServerAddress address = new ServerAddress(this.host, this.port);

if (this.options != null)
this.mongo = new Mongo(address, this.options);
else {
this.mongo = new Mongo(address);
}
}
else if (this.replSet != null)
{
if (this.options != null)
this.mongo = new Mongo(this.replSet, this.options);
else {
this.mongo = new Mongo(this.replSet);
}

}

if (this.mongo != null)
this.db = this.mongo.getDB(this.database);
else {
System.out.println("无法取得数据库实例,请确认配置信息");
//        SystemLog.getInstance().showMsg("无法取得数据库实例,请确认配置信息");
}

if ((this.username != null) && (this.password != null))
if (this.db.authenticate(this.username, this.password.toCharArray())) {
System.out.println("登陆成功:" + this.username);
//          SystemLog.getInstance().showMsg("登陆成功:" + this.username);
} else {
this.db = null;
System.out.println("登陆失败:" + this.username);
//          SystemLog.getInstance().showMsg("登陆失败:" + this.username);
}
}
catch (UnknownHostException e)
{
//      SystemLog.getInstance().showMsg("数据库地址错误");
System.out.println("数据库地址错误");
e.printStackTrace();
}
}

public DB getDBConnection()
{
if (this.db == null) {
connect();
}

return this.db;
}
}

进行mongodb数据的操作

package com.zongtui.filter.mongodb;

import java.net.UnknownHostException;
import java.util.List;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;
import com.zongtui.filter.config.ConfigManager;

public class MongoDbConn
{
private static MongoDbConn instance = null;

private String host = null;
private int port = 0;
private List<ServerAddress> replSet = null;
private MongoOptions options = null;
private String database = null;
private
4000
String username = null;
private String password = null;
private Mongo mongo = null;
private DB db = null;

private MongoDbConn()
{
this.host = ConfigManager.getInstance().getConfigValue("db_ip");
this.port = Integer.valueOf(ConfigManager.getInstance().getConfigValue("db_port")).intValue();
this.database = ConfigManager.getInstance().getConfigValue("db_name");
//    this.host = "192.168.184.130";
//    this.port = 27017;
//    this.database = "nutch";

}

public static MongoDbConn getInstance()
{
if (instance == null) {
instance = new MongoDbConn();
}
return instance;
}

public void init(String database)
{
this.database = database;
}

public void init(String host, int port, String database) {
this.host = host;
this.port = port;
this.database = database;
}

public void init(String host, int port, String database, String username, String password) {
this.host = host;
this.port = port;
this.database = database;
this.username = username;
this.password = password;
}

public void init(List<ServerAddress> replSet, String database) {
this.replSet = replSet;
this.database = database;
}

public void init(List<ServerAddress> replSet, String database, String username, String password) {
this.replSet = replSet;
this.database = database;
this.username = username;
this.password = password;
}

public MongoOptions getOptions() {
return this.options;
}

public void setOptions(MongoOptions options) {
this.options = options;
}

private void connect()
{
try
{
if ((this.host != null) && (this.port != 0))
{
ServerAddress address = new ServerAddress(this.host, this.port);

if (this.options != null)
this.mongo = new Mongo(address, this.options);
else {
this.mongo = new Mongo(address);
}
}
else if (this.replSet != null)
{
if (this.options != null)
this.mongo = new Mongo(this.replSet, this.options);
else {
this.mongo = new Mongo(this.replSet);
}

}

if (this.mongo != null)
this.db = this.mongo.getDB(this.database);
else {
System.out.println("无法取得数据库实例,请确认配置信息");
//        SystemLog.getInstance().showMsg("无法取得数据库实例,请确认配置信息");
}

if ((this.username != null) && (this.password != null))
if (this.db.authenticate(this.username, this.password.toCharArray())) {
System.out.println("登陆成功:" + this.username);
//          SystemLog.getInstance().showMsg("登陆成功:" + this.username);
} else {
this.db = null;
System.out.println("登陆失败:" + this.username);
//          SystemLog.getInstance().showMsg("登陆失败:" + this.username);
}
}
catch (UnknownHostException e)
{
//      SystemLog.getInstance().showMsg("数据库地址错误");
System.out.println("数据库地址错误");
e.printStackTrace();
}
}

public DB getDBConnection()
{
if (this.db == null) {
connect();
}

return this.db;
}
}

在程序中进行xml进行数据操作,可以上
http://blog.csdn.net/qew110123/article/details/46535103
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息