MongoDB Interface class functions

Source: Internet
Author: User
Tags class definition mongoclient mongodb interface throw exception import database

"interface class definition" [Java] view Plaincopy
  1. /**
  2. * Project Name: Spidercrawler
  3. * File name: Mongodbdao.java
  4. * Description: TODO (describe what the file does in a word)
  5. */
  6. Package Com.newsTest.dao;
  7. Import java.util.ArrayList;
  8. Import Com.mongodb.DB; Import Database db
  9. Import com.mongodb.DBCollection; Import Collection Dbcollection
  10. Import Com.mongodb.DBObject; Import Object DBObject
  11. /**
  12. * Class Name: Mongodbdao
  13. * Package Name: Com.newsTest.dao
  14. * Description: TODO (here is a word describing the role of this class)
  15. */
  16. Public interface Mongodbdao {
  17. /**
  18. * Method Name: Getdb
  19. * Description: Gets the specified MongoDB database
  20. * @param dbName
  21. * @return
  22. */
  23. Public DB Getdb (String dbName);
  24. /**
  25. * Method Name: GetCollection
  26. * Description: Gets the collection collection for the specified MongoDB database
  27. * @param dbName database name
  28. * @param CollectionName Database Collection
  29. * @return
  30. */
  31. Public dbcollection GetCollection (string dbName, string collectionname);
  32. /**
  33. * Method Name: InSert
  34. * Description: Adds the given keys and the corresponding values to the specified database
  35. * @param dbName
  36. * @param collectionname
  37. * @param keys
  38. * @param values
  39. * @return
  40. */
  41. public boolean InSert (String dbName, String CollectionName, string[] keys, object[] values);
  42. /**
  43. *
  44. * Method Name: Delete
  45. * ZHOUYH
  46. * Created time: 2014-8-30 04:09:00
  47. * Description: Delete database dbname, specify values for keys and corresponding values
  48. * @param dbName
  49. * @param collectionname
  50. * @param keys
  51. * @param values
  52. * @return
  53. */
  54. public boolean Delete (String dbName, String CollectionName, string[] keys, object[] values);
  55. /**
  56. * Method Name: Find
  57. * Description: Find the value of the specified keys and corresponding values from the database dbname
  58. * @param dbName
  59. * @param collectionname
  60. * @param keys
  61. * @param values
  62. * @param num
  63. * @return
  64. */
  65. Public arraylist<dbobject> Find (string dbName, String CollectionName, string[] keys, object[] values, int num);
  66. /**
  67. * Method Name: Update
  68. * ZHOUYH
  69. * Created time: 2014-8-30 04:17:54
  70. * Description: Update database dbname, update oldvalue with specified newvalue
  71. * @param dbName
  72. * @param collectionname
  73. * @param oldValue
  74. * @param newvalue
  75. * @return
  76. */
  77. public boolean update (string dbName, String collectionname, DBObject oldValue, DBObject newvalue);
  78. /**
  79. * Method Name: Isexit
  80. * Description: Determines whether the given keys and corresponding values exist in the CollectionName collection of the specified dbname
  81. * @param dbName
  82. * @param collectionname
  83. * @param keys
  84. * @param values
  85. * @return
  86. */
  87. public boolean Isexit (String dbName, String CollectionName, String key, Object value);
  88. }
"Excuse class implementation" [Java] view Plaincopy
  1. /**
  2. * Project Name: Spidercrawler
  3. * File name: Mongodbdaoimpl.java
  4. * Description: TODO (describe what the file does in a word)
  5. */
  6. Package Com.newsTest.dao.impl;
  7. Import java.net.UnknownHostException; Import the URL of the associated exception jar package
  8. Import java.util.ArrayList;
  9. Import Com.newsTest.dao.MongoDBDao; Import the definition of an interface class
  10. Import Com.mongodb.BasicDBObject;
  11. Import Com.mongodb.DB;
  12. Import com.mongodb.DBCollection;
  13. Import Com.mongodb.DBCursor; Import Cursors
  14. Import Com.mongodb.DBObject;
  15. Import com.mongodb.MongoClient;
  16. Import com.mongodb.MongoClientOptions;
  17. Import com.mongodb.MongoException;
  18. Import Com.mongodb.WriteResult;
  19. /**
  20. * Class Name: Mongodbdaoimpl
  21. * Package Name: Com.newsTest.dao.impl
  22. * Description: TODO (here is a word describing the role of this class)
  23. */
  24. public class Mongodbdaoimpl implements mongodbdao{
  25. /*mongodbdao is the interface that you defined earlier, not the system comes with * /
  26. /**
  27. * The Mongoclient instance represents the database connection pool, is thread-safe, can be shared by multi-thread, the client only maintains one instance under the multithreading condition.
  28. * MONGO is non-thread-safe and is currently recommended in the MongoDB API for mongoclient replacement MONGO
  29. */
  30. Private mongoclient mongoclient = null;
  31. /**
  32. * Private Constructors
  33. */
  34. Private Mongodbdaoimpl () {
  35. if (mongoclient = = null) {
  36. Mongoclientoptions.builder build = new Mongoclientoptions.builder ();
  37. Build.connectionsperhost (50);
  38. The maximum number of connection that can be established with the target database is 50
  39. Build.autoconnectretry (TRUE);
  40. Automatic re-connect database startup
  41. Build.threadsallowedtoblockforconnectionmultiplier (50);
  42. If all current connection are in use, there can be 50 threads queued for each connection
  43. /*
  44. * When a thread accesses a database, the maximum wait time is 2 minutes before a successful access to an available database connection
  45. * This is dangerous, if more than maxwaittime have not obtained this connection, the thread will throw exception
  46. * So here the Maxwaittime set should be large enough to avoid database access failures due to excessive queuing threads
  47. */
  48. Build.maxwaittime (1000*60*2);
  49. Build.connecttimeout (1000*60*1); The timeout setting to connect to the database is set to 1 minutes
  50. Mongoclientoptions myoptions = Build.build ();
  51. try {
  52. Database Connection Instance
  53. Mongoclient = new Mongoclient ("127.0.0.1", myoptions);
  54. } catch (Unknownhostexception e) {
  55. TODO here write exception-handling code
  56. E.printstacktrace ();
  57. } catch (Mongoexception e) {
  58. E.printstacktrace ();
  59. }
  60. }
  61. }

MongoDB Interface class functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.