Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. static String user = "guest";
  2. static String database = "VsJogger";
  3. static char[] password = {'g','u','e','s','t'};
  4. public static void main(String[] args) {
  5. MongoCredential credential = MongoCredential.createCredential(user,
  6. database,
  7. password);
  8.  
  9. MongoClientURI uri = new MongoClientURI("mongodb://"+user+":"
  10. +String.valueOf(password)
  11. +"@localhost/?authSource="+database);
  12.  
  13. try{
  14.  
  15. // To connect to mongodb server
  16. MongoClient mongoClient = new MongoClient( uri);
  17.  
  18.  
  19. // Now connect to your databases
  20. MongoDatabase db = mongoClient.getDatabase("VsJogger");
  21. System.out.println("Connect to database successfully");
  22. MongoCollection coll = db.getCollection("Jog");
  23. MongoCursor<Document> cursor = coll.find().iterator();
  24.  
  25. int i = 1;
  26. while (cursor.hasNext()) {
  27. System.out.println("Inserted Document: "+i);
  28. System.out.println(cursor.next());
  29. i++;
  30. }
  31.  
  32.  
  33. }catch(Exception e){
  34. System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  35. }
  36.  
  37. Oct 23, 2016 3:59:45 PM com.mongodb.diagnostics.logging.JULLogger log
  38. INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
  39. Connect to database successfully
  40. Oct 23, 2016 3:59:45 PM com.mongodb.diagnostics.logging.JULLogger log
  41. INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
  42. Oct 23, 2016 3:59:46 PM com.mongodb.diagnostics.logging.JULLogger log
  43. INFO: Exception in monitor thread while connecting to server localhost:27017
  44. com.mongodb.MongoSocketOpenException: Exception opening socket
  45. at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
  46. at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
  47. at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113)
  48. at java.lang.Thread.run(Thread.java:745)
  49. Caused by: java.net.ConnectException: Connection refused: connect
  50. at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
  51. at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
  52. at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
  53. at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
  54. at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
  55. at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
  56. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  57. at java.net.Socket.connect(Socket.java:589)
  58. at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57)
  59. at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
  60. ... 3 more
  61.  
  62. com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]
  63. BUILD SUCCESSFUL (total time: 31 seconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement