블로그 이미지
LifeisSimple

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

2011. 3. 31. 17:53 Brain Trainning/NoSQL
Replication Set 에서 DB 커넥팅 방법

                  public void testFindAll() throws UnknownHostException, MongoException {

                                   // TODO Auto-generated method stub

                                   ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>();

                   addrs.add(new ServerAddress("192.168.1.101", 27017));

                   addrs.add(new ServerAddress("192.168.1.102", 27017));

                   addrs.add(new ServerAddress("192.168.1.103", 27017));
 

                   Mongo connection = new Mongo(addrs);

                     

                                   DB mongoJUnit = connection.getDB("deptDB");

                                   DBCollection dept = mongoJUnit.getCollection("deptJUnit");

                                  

                                   DBCursor cur = dept.find();

                                  

                                   int index = 1;

                                   for ( Iterator<DBObject> it = cur.iterator(); it.hasNext(); ++index) {

                                                     DBObject obj = it.next();

                                                    

                                                     System.out.println("data("+ index+"):"+obj.toString());

                                   }

                                  

                                   System.out.println(connection.getAddress());

                  }

posted by LifeisSimple