Found 2616 Articles for Java

How to drop a database in MongoDB using java?

Maruthi Krishna
Updated on 10-Apr-2020 07:25:59

549 Views

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.Syntaxdb.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.Exampleimport 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 ... Read More

How to drop an index in MongoDB using Java?

Maruthi Krishna
Updated on 10-Apr-2020 07:24:24

463 Views

In MongoDB to drop an index, you need to use dropIndex() method.Syntaxdb.COLLECTION_NAME.dropIndex({KEY:1})In Java, you can drop an Index using the dropIndex() method, to this method you need to pass the type of the index (ascending or descending) and the field name on which you have created it.dropIndex(Indexes.ascending("name"));Exampleimport com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Indexes; import org.bson.Document; import com.mongodb.MongoClient; public class DroppingIndex {    public static void main( String args[] ) {       //Creating a MongoDB client       MongoClient mongo = new MongoClient( "localhost" , 27017 );       //Accessing the database       MongoDatabase database = ... Read More

How to limit the number of records, while retrieving data from a MongoDB collection using Java?

Maruthi Krishna
Updated on 10-Apr-2020 07:21:52

532 Views

While retrieving records from a MongoDB collection, you can limit the number of records in the result using thelimit() method.Syntaxdb.COLLECTION_NAME.find().limit(no.of records needed)The Java MongoDB library provides a method with the same name, to limit the number of records invoke this method (on the result of the find() method) by passing an integer value representing the required number of records.Exampleimport com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class LimitingRecords {    public static void main( String args[] ) {       //Creating a MongoDB client       MongoClient mongo = ... Read More

How to get the list of all the MongoDB databases using java?

Maruthi Krishna
Updated on 10-Apr-2020 07:18:10

845 Views

In MongoDB, you can view the list of databases using the show dbs command> show dbs admin config local myDatabase sampleDatabase students test testDBIn Java, you can get the list of all the databases in MongoDb using the getDatabaseNames() method.Exampleimport com.mongodb.client.MongoIterable; import com.mongodb.MongoClient; public class ListOfDatabases {    public static void main( String args[] ) {       // Creating a Mongo client       MongoClient mongo = new MongoClient( "localhost" , 27017 );       //Retrieving the list of collections       MongoIterable list = mongo.listDatabaseNames();       for (String name : list) {          System.out.println(name);       }    } }Outputadmin config local myDatabase sampleDatabase students test testDB

Explain Java MongoDB projections

Maruthi Krishna
Updated on 10-Apr-2020 07:16:54

1K+ Views

While retrieving data from MongoDb collections you can select only necessary data using projections. In Java, you can project necessary data while reading the documents from a collection using the projection() method. Invoke this method on the result of find(), bypassing the names of the required filed names as −projection(Projections.include("name", "age"));ExampleFollowing Java examples read the documents from a collection, using projection we are displaying the values of name and age fields only.import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Projections; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class ProjectionExample {    public static void main( String args[] ... Read More

How to skip documents while retrieving data from MongoDB, using java?

Maruthi Krishna
Updated on 10-Apr-2020 07:14:51

215 Views

While retrieving records from a MongoDB collection, you can skip records in the result using the skip() method.Syntaxdb.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)The Java MongoDB library provides a method with the same name, to skip records, invoke this method (on the result of the find() method) bypassing an integer value representing the number of records to skip.Exampleimport com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class SkipRecords {    public static void main( String args[] ) {       //Creating a MongoDB client       MongoClient mongo = new MongoClient( "localhost" , 27017 );   ... Read More

How to display OpenCV Mat object using Swings?

Maruthi Krishna
Updated on 10-Apr-2020 07:12:22

413 Views

The class ImageIcon is an implementation of the Icon interface that paints Icons from Images. You can display images on a Swing window using this class, the constructor of this class accepts a BufferedImage object as a parameter.Therefore to display an OpenCV image that is stored in a Mat object using Swing window, you need to convert it into a BufferedImage object and pass it as a parameter to the ImageIcon method.Exampleimport java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.InputStream; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.imgcodecs.Imgcodecs; public class DisplayingImagesUsingSwings {    public static ... Read More

How to display OpenCV Mat object using JavaFX?

Maruthi Krishna
Updated on 10-Apr-2020 07:10:36

337 Views

The JavaFX library provides a class with name ImageView using this you can display an image. This class accepts an object of the WritableImage class.To display an image Stored in OpenCV Mat object you need to convert it into a WritableImage object and pass it the ImageView class.Exampleimport java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import javax.imageio.ImageIO; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.imgcodecs.Imgcodecs; public class DisplayingImagesJavaFX extends Application {    @Override    public void start(Stage stage) throws IOException {       WritableImage writableImage = ... Read More

What is the importance of jmod format in Java 9?

raja
Updated on 09-Apr-2020 18:15:29

685 Views

Java 9 has introduced a new format called "jmod" to encapsulate modules. The jmod files can be designed to handle more content types than jar files. It can also package local codes, configuration files, local commands, and other types of data. The "jmod" format hasn't support at runtime and can be based on zip format currently. The jmod format can be used at both compile and link time and can be found in JDK_HOME\jmods directory, where JDK_HOME is a directory. The files in jmod format have a ".jmod" extension.Java 9 comes with a new tool called jmod, and it is located in the ... Read More

What are the different "/types" commands in JShell in Java 9?

raja
Updated on 09-Apr-2020 14:57:27

372 Views

JShell tool has introduced in Java 9 version. It is also called a REPL(Read-Evaluate-Print-Loop) tool that allows us to execute Java code and getting immediate results. We need to list out the declared types like class, interface, enum, and etc by using the "/types" command.Below are the different "/types" commands in JShell./types /types [ID] /types [Type_Name] /types -start /types -all/types: This command lists all active types (class, interface, enum) created in JShell./types [ID]: This command displays the type corresponding to the id [ID]./types [Type_Name]: This command displays the type corresponding to [Type_Name]./types -start: This command allows us to list the types that ... Read More

Advertisements