Class MongoDBUtil

java.lang.Object
com.project.system.MongoDBUtil

public class MongoDBUtil extends Object
The MongoDBUtil class provides utility methods to start an embedded MongoDB server, establish a connection, and access the MongoDB database.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static com.mongodb.client.MongoClient
    The MongoDB client used to connect to the database.
    private static de.flapdoodle.embed.mongo.MongodExecutable
    The executable for the embedded MongoDB instance.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Closes the MongoDB client connection and stops the embedded MongoDB server.
    static com.mongodb.client.MongoDatabase
    Starts the embedded MongoDB server and establishes a connection to the MongoDB instance.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • mongodExecutable

      private static de.flapdoodle.embed.mongo.MongodExecutable mongodExecutable
      The executable for the embedded MongoDB instance.
    • mongoClient

      private static com.mongodb.client.MongoClient mongoClient
      The MongoDB client used to connect to the database.
  • Constructor Details

    • MongoDBUtil

      public MongoDBUtil()
  • Method Details

    • getDatabase

      public static com.mongodb.client.MongoDatabase getDatabase()
      Starts the embedded MongoDB server and establishes a connection to the MongoDB instance.
      Returns:
      The MongoDatabase instance connected to the "student_course_db" database.
      Throws:
      RuntimeException - if MongoDB fails to start.
    • close

      public static void close()
      Closes the MongoDB client connection and stops the embedded MongoDB server.