成都网站建设设计

将想法与焦点和您一起共享

Java操作Mongodb数据库实现数据的增删查改功能示例

本文实例讲述了Java操作MongoDB数据库实现数据的增删查改功能。分享给大家供大家参考,具体如下:

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、网站制作、阿坝州网络推广、成都小程序开发、阿坝州网络营销、阿坝州企业策划、阿坝州品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供阿坝州建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

首先,我们在windows下安装mongodb数据库,安装教程可查看前面一篇文章:https://www.jb51.net/article/85605.htm

代码如下:

package io.mogo;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.WriteResult;
/**
 * Hello world!
 *
 */
public class App
{
  private static final String host = "localhost";
  private static final int port = 27017;
  private static final String userName = "";
  private static final String password = "";
  private static final String dataBaseName = "test";
  private static DB db;
  public static void main( String[] args ) throws Exception
  {
    System.out.println( "Hello World!" );
    connMongoDB();
    find("person", "name", "xiaoming");
    DBObject newObj = new BasicDBObject();
    newObj.put("name", "xiaoming");
    newObj.put("desc", "i am xiaoming");
    update("person", "name", "xiaoming", newObj);
    find("person", "name", "xiaoming");
  }
  public static void connMongoDB() throws Exception {
    Mongo mongo = new Mongo(host, port);
    db = mongo.getDB(dataBaseName);
    if (!StringUtils.isEmpty(userName) || !StringUtils.isEmpty(password)) {
      db.authenticate(userName, password.toCharArray());
    }
  }
  public static void find(String tableName, String key, String value){
   DBObject obj = new BasicDBObject();
   obj.put(key, value);
   DBCollection dbCollection = db.getCollection(tableName);
   DBCursor dbCursor = dbCollection.find(obj);
   while(dbCursor.hasNext()){
     Map map = (Map)dbCursor.next();
     System.out.println(map);
   }
  }
  public static boolean delete(String tableName, String key, String value){
    DBObject obj = new BasicDBObject();
    obj.put(key, value);
    DBCollection dbCollection = db.getCollection(tableName);
    WriteResult result = dbCollection.remove(obj);
    if(result.getN() > 0){
      System.out.println("删除数据成功!");
      return true;
    }else{
      return false;
    }
  }
  public static boolean insert(String tableName, DBObject obj){
    DBCollection dbCollection = db.getCollection(tableName);
    long num = dbCollection.count();
    dbCollection.insert(obj);
    if(dbCollection.count() - num > 0){
      System.out.println("插入数据成功!");
      return true;
    }else{
      return false;
    }
  }
  public static boolean update(String tableName, String key, String value, DBObject newValue){
    DBObject obj = new BasicDBObject();
    obj.put(key, value);
    DBCollection dbCollection = db.getCollection(tableName);
    DBObject oldValue = dbCollection.findOne(obj);
    WriteResult result = dbCollection.update(oldValue, newValue);
    if(result.getN() > 0){
      System.out.println("数据更新成功!");
      return true;
    }else
      return false;
  }
}

附:完整实例代码点击此处本站下载

更多关于java相关内容感兴趣的读者可查看本站专题:《Java+MySQL数据库程序设计总结》、《Java操作Excel技巧总结》、《Java数据结构与算法教程》、《Java文件与目录操作技巧汇总》及《Java操作DOM节点技巧总结》

希望本文所述对大家java程序设计有所帮助。


当前题目:Java操作Mongodb数据库实现数据的增删查改功能示例
当前链接:http://chengdu.cdxwcx.cn/article/gcpgpp.html