"interface class definition"
[Java] view Plaincopy
- /**
- * Project Name: Spidercrawler
- * File name: Mongodbdao.java
- * Description: TODO (describe what the file does in a word)
- */
- Package Com.newsTest.dao;
- Import java.util.ArrayList;
- Import Com.mongodb.DB; Import Database db
- Import com.mongodb.DBCollection; Import Collection Dbcollection
- Import Com.mongodb.DBObject; Import Object DBObject
- /**
- * Class Name: Mongodbdao
- * Package Name: Com.newsTest.dao
- * Description: TODO (here is a word describing the role of this class)
- */
- Public interface Mongodbdao {
- /**
- * Method Name: Getdb
- * Description: Gets the specified MongoDB database
- * @param dbName
- * @return
- */
- Public DB Getdb (String dbName);
- /**
- * Method Name: GetCollection
- * Description: Gets the collection collection for the specified MongoDB database
- * @param dbName database name
- * @param CollectionName Database Collection
- * @return
- */
- Public dbcollection GetCollection (string dbName, string collectionname);
- /**
- * Method Name: InSert
- * Description: Adds the given keys and the corresponding values to the specified database
- * @param dbName
- * @param collectionname
- * @param keys
- * @param values
- * @return
- */
- public boolean InSert (String dbName, String CollectionName, string[] keys, object[] values);
- /**
- *
- * Method Name: Delete
- * ZHOUYH
- * Created time: 2014-8-30 04:09:00
- * Description: Delete database dbname, specify values for keys and corresponding values
- * @param dbName
- * @param collectionname
- * @param keys
- * @param values
- * @return
- */
- public boolean Delete (String dbName, String CollectionName, string[] keys, object[] values);
- /**
- * Method Name: Find
- * Description: Find the value of the specified keys and corresponding values from the database dbname
- * @param dbName
- * @param collectionname
- * @param keys
- * @param values
- * @param num
- * @return
- */
- Public arraylist<dbobject> Find (string dbName, String CollectionName, string[] keys, object[] values, int num);
- /**
- * Method Name: Update
- * ZHOUYH
- * Created time: 2014-8-30 04:17:54
- * Description: Update database dbname, update oldvalue with specified newvalue
- * @param dbName
- * @param collectionname
- * @param oldValue
- * @param newvalue
- * @return
- */
- public boolean update (string dbName, String collectionname, DBObject oldValue, DBObject newvalue);
- /**
- * Method Name: Isexit
- * Description: Determines whether the given keys and corresponding values exist in the CollectionName collection of the specified dbname
- * @param dbName
- * @param collectionname
- * @param keys
- * @param values
- * @return
- */
- public boolean Isexit (String dbName, String CollectionName, String key, Object value);
- }
"Excuse class implementation"
[Java] view Plaincopy
- /**
- * Project Name: Spidercrawler
- * File name: Mongodbdaoimpl.java
- * Description: TODO (describe what the file does in a word)
- */
- Package Com.newsTest.dao.impl;
- Import java.net.UnknownHostException; Import the URL of the associated exception jar package
- Import java.util.ArrayList;
- Import Com.newsTest.dao.MongoDBDao; Import the definition of an interface class
- Import Com.mongodb.BasicDBObject;
- Import Com.mongodb.DB;
- Import com.mongodb.DBCollection;
- Import Com.mongodb.DBCursor; Import Cursors
- Import Com.mongodb.DBObject;
- Import com.mongodb.MongoClient;
- Import com.mongodb.MongoClientOptions;
- Import com.mongodb.MongoException;
- Import Com.mongodb.WriteResult;
- /**
- * Class Name: Mongodbdaoimpl
- * Package Name: Com.newsTest.dao.impl
- * Description: TODO (here is a word describing the role of this class)
- */
- public class Mongodbdaoimpl implements mongodbdao{
- /*mongodbdao is the interface that you defined earlier, not the system comes with * /
- /**
- * 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.
- * MONGO is non-thread-safe and is currently recommended in the MongoDB API for mongoclient replacement MONGO
- */
- Private mongoclient mongoclient = null;
- /**
- * Private Constructors
- */
- Private Mongodbdaoimpl () {
- if (mongoclient = = null) {
- Mongoclientoptions.builder build = new Mongoclientoptions.builder ();
- Build.connectionsperhost (50);
- The maximum number of connection that can be established with the target database is 50
- Build.autoconnectretry (TRUE);
- Automatic re-connect database startup
- Build.threadsallowedtoblockforconnectionmultiplier (50);
- If all current connection are in use, there can be 50 threads queued for each connection
- /*
- * When a thread accesses a database, the maximum wait time is 2 minutes before a successful access to an available database connection
- * This is dangerous, if more than maxwaittime have not obtained this connection, the thread will throw exception
- * So here the Maxwaittime set should be large enough to avoid database access failures due to excessive queuing threads
- */
- Build.maxwaittime (1000*60*2);
- Build.connecttimeout (1000*60*1); The timeout setting to connect to the database is set to 1 minutes
- Mongoclientoptions myoptions = Build.build ();
- try {
- Database Connection Instance
- Mongoclient = new Mongoclient ("127.0.0.1", myoptions);
- } catch (Unknownhostexception e) {
- TODO here write exception-handling code
- E.printstacktrace ();
- } catch (Mongoexception e) {
- E.printstacktrace ();
- }
- }
- }
MongoDB Interface class functions