Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package oopproject1; 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.MongoClient; import com.mongodb.ServerAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Baak */ public class MakaleDaoImpl implements MakaleDao { MongoClient mongo; DB db; public MakaleDaoImpl() { try { this.mongo = new MongoClient(new ServerAddress("localhost")); this.db = mongo.getDB("testdb"); } catch (UnknownHostException ex) { Logger.getLogger(MakaleDaoImpl.class.getName()).log(Level.SEVERE, null, ex); } } @Override public ArrayList<Makale> getAllMakale() { ArrayList<Makale> makaledb = new ArrayList<Makale>(); try { MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("testdb"); System.out.println("Connect to database successfully"); DBCollection coll = db.getCollection("makale"); System.out.println("Collection mycol selected successfully"); DBCursor cursor = coll.find(); while (cursor.hasNext()) { DBObject obj = cursor.next(); //do your thing System.out.println(obj.get("id")); Makale mak = new Makale(); mak.setId((String) obj.get("id")); mak.setTitle((String) obj.get("title")); mak.setAuthors((String) obj.get("author")); mak.setVenue((String) obj.get("venue")); mak.setYear((String) obj.get("year")); mak.setContent((String) obj.get("content")); mak.setFrequencies((Map<String, Integer>) obj.get("frequencies")); makaledb.add(mak); } } catch (UnknownHostException ex) { Logger.getLogger(MakaleDaoImpl.class.getName()).log(Level.SEVERE, null, ex); } return makaledb; } public void UpdateMakale(Makale m1) { MongoClient mongo; try { mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("testdb"); DBCollection table = db.getCollection("makale"); BasicDBObject document = new BasicDBObject(); BasicDBObject searchQuery = new BasicDBObject().append("id", m1.getId()); document.put("id", m1.getId()); document.put("title", m1.getTitle()); document.put("author", m1.getAuthors()); document.put("venue", m1.getVenue()); document.put("year", m1.getYear()); document.put("content", m1.getContent()); document.put("frequencies", m1.getFrequencies()); //iinde map<> o yzden veri tabanna nasl aktarlacak. table.insert(document); } catch (UnknownHostException ex) { Logger.getLogger(MakaleDaoImpl.class.getName()).log(Level.SEVERE, null, ex); } } @Override public Makale getMakale(String id) { Makale m1 = new Makale(); m1 = null; MongoClient mongo; try { mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("testdb"); DBCollection table = db.getCollection("makale"); BasicDBObject searchQuery = new BasicDBObject(); searchQuery.put("id", id); DBCursor cursor = table.find(searchQuery); while (cursor.hasNext()) { DBObject smt = cursor.next(); System.out.println(smt); // m1.setId(id); } } catch (UnknownHostException ex) { Logger.getLogger(MakaleDaoImpl.class.getName()).log(Level.SEVERE, null, ex); } return m1; } }