In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Explain the basic operation of database and examples in detail
Don't talk too much nonsense, go directly to the code, the comments are relatively clear, please refer to it.
Sample code:
/ * insert a DB object * / public static void addDBObject (DBCollection collection,BasicDBObject object) {collection.insert (object);} / * query DBObject * / public static DBObject getDBObjectById (String value) throws UnknownHostException according to id, MongoException {dbc = getDBCollection ("company", "users"). Find (new BasicDBObject ("_ id", new ObjectId (value)); DBObject ob = null; int i = 0; while (dbc.hasNext ()) {ob = dbc.next () IDefinder;} if (I = = 1) {return ob;} else {return null;}} / * query * / public static DBObject getDBObject (String key,String value) throws UnknownHostException based on key and value values, MongoException {dbc = getDBCollection ("company", "users") .find (new BasicDBObject (key,value)); DBObject ob = null; int i = 0; while (dbc.hasNext ()) {ob = dbc.next (); iSum + } if (I = = 1) {return ob;} else {return null;}} / * get (add) all the following aggregate names (table names) * / public static Set getCollectionsNames (String DBName) throws MongoException, UnknownHostException {return getDB (DBName). GetCollectionNames () } / * traverse the collection of db objects in the aggregation (equivalent to data in a relational database) * / public static Set getDBObjects (DBCollection collection) {Set dbObjects = new HashSet (); DBCursor cursor = collection.find (); while (cursor.hasNext ()) {DBObject object = cursor.next (); dbObjects.add (object);} return dbObjects } / * get / add aggregations (equivalent to relational database tables) * / public static DBCollection getDBCollection (String DBName,String collectionName) throws UnknownHostException, MongoException {return getDB (DBName) .getCollection (collectionName);} / * acquire / add databases * / public static DB getDB (String DBName) throws UnknownHostException, MongoException {return getMongo (). GetDB (DBName) } / * Connect database * / public static Mongo getMongo () throws UnknownHostException, MongoException {Mongo mg = null; if (mg = = null) {mg = new Mongo ();} return mg;} / * close connection * / public static void destory (Mongo mg) {if (mg! = null) {mg.close (); mg = null;} System.gc () } / * get database name * / public static List getDBNames () throws MongoException, UnknownHostException {return getMongo (). GetDatabaseNames ();} / * Delete database * / public static void deleteDB (String DBName) throws MongoException, UnknownHostException {getMongo (). DropDatabase (DBName);}
If you have any questions, please leave a message or go to the community to exchange and discuss, thank you for reading, hope to help you, thank you for your support!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.