欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-25 18:04:45
本文实例讲述了java操作mongodb数据库实现数据的增删查改功能。分享给大家供大家参考,具体如下: 首先,我们在windows下安装mongodb数据库,安装教程可查...

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

首先,我们在windows下安装mongodb数据库,安装教程可查看前面一篇文章:

代码如下:

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程序设计有所帮助。