
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to drop a database in MongoDB using java?
MongoDB db.dropDatabase() command is used to drop a existing database.This will delete the current database. if you haven't selected any, the default (test) database will be deleted.
Syntax
db.dropDatabase()
In Java to delete a database, first of all, get the object of the required database using the getDatabase() method and delete it by invokimg the drop() method on it.
Example
import com.mongodb.MongoClient; import com.mongodb.client.MongoDatabase; public class DropingDatabase { public static void main( String args[] ) { //Creating a MongoDB client @SuppressWarnings("resource") MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Creating a database mongo.getDatabase("myDatabase"); System.out.println("Databases created successfully"); MongoDatabase database = mongo.getDatabase("myDatabase"); database.drop(); } }
Output
Database deleted successfully
- Related Articles
- How to drop a database in MongoDB?
- How to drop a MongoDB Collection using Java?
- How to drop an index in MongoDB using Java?
- How to create a database in MongoDB using Java?
- How to drop a database using JDBC API?
- How to list all the Collections in a MongoDB database using Java?
- How do I drop a MongoDB database from the command line?
- How to drop a table from a database using JDBC API?
- Drop all indexes from all the collections in a MongoDB database using the command line?
- How to drop a table from Oracle database using JDBC API?
- How to drop a collection in MongoDB?
- How to connect to a MongoDB database using a JDBC program?
- How to drop constraint on a column of a table in a database using JDBC API?
- How to Drop MySQL Table using Java?
- How can we drop an existing database by using mysqladmin?

Advertisements