Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views6 pages

WT Unit 4 Part1

Uploaded by

khushisingh7628
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

WT Unit 4 Part1

Uploaded by

khushisingh7628
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

MONGODB

MongoDB is a non-relational, open-source database management system


(DBMS) that uses flexible documents to store and process data:
• Document model: MongoDB stores data in JSON-like documents that can vary
from document to document.
• Schema: MongoDB uses a flexible schema for storing data.
• Indexing: MongoDB provides full indexing support.
• Replication: MongoDB provides replication with rich and intuitive APIs.
• Scalability: MongoDB is built for horizontal scaling and geographic distribution.
• Availability: MongoDB is built for high availability.

CFRUD operations in MongoDB: CRUD stands for create read update and delete.

Create a database in MongoDB:

Change or Create a Database


You can change or create a new database by typing use then the name of the
database.

Example
Create a new database called "student":

use student

Insert Documents
There are 2 methods to insert documents into a MongoDB database.

insertOne()
To insert a single document, use the insertOne() method.

This method inserts a single object into the database.

db.student.insertOne({rollno:12,name:”ravi”,add:”Ghaziabad”})

db.student.insertOne({rollno:13,name:”ramesh”,add:”delhi”})
insertMany():

db.student.insertMany([{rollno:18,name:”abc”,add:”ghazia” },

{ rollno:20,name:”abc”,add:”ghazia”}])

find():

There are 2 methods to find and select data from a MongoDB


collection, find() and findOne().

find()
To select data from a collection in MongoDB, we can use the find() method.

This method accepts a query object. If left empty, all documents will be
returned.

Example
db.student.find()

findOne()
To select only one document, we can use the findOne() method.

This method accepts a query object. If left empty, it will return the first
document it finds.

db.student.findOne()

Delete Documents
We can delete documents by using the methods deleteOne() or deleteMany().

These methods accept a query object. The matching documents will be


deleted.

deleteOne()
The deleteOne() method will delete the first document that matches the query
provided.

Example
db.student.deleteOne({ rollno:12 })

deleteMany()
The deleteMany() method will delete all documents that match the query
provided.

Example
db.posts.deleteMany({ category: "Technology" })

Update:

Write a query to update the age of a student named Alice to 26 in the


“students” collection.
db.students.updateOne(
{ name: "Alice" },
{ $set: { age: 26 } }
)

Qurery to create database

Use student

db.student.insertOne({rollno:12,name:”aaa”,add:”Ghaziabad”}) //insert

db.student.insertOne({rollno:13,name:”bbb”,add:”Delhi”})

db.student.insertOne({rollno:14,name:”ccc”,add:”Delhi”})

db.student.find() //show all records

db.student.deleteOne({rollno:14}) //for delete records

db.students.updateOne(
{ name: "Alice" }, { $set: { age: 26 } })
Enterprise Java Beans:

EJB is an acronym for enterprise java bean. It is a specification provided by Sun


Microsystems to develop secured, robust and scalable distributed applications.

A distributed application is a collection of computer programs that run on


multiple computers or nodes within a network, and work together to achieve
a shared goal:
To run EJB application, you need an application server (EJB Container) such as Jboss,
Glassfish, Weblogic, Websphere etc. It performs:

a. life cycle management,


b. security,
c. transaction management, and
d. object pooling.

EJB application is deployed on the server, so it is called server side component also.

When use Enterprise Java Bean?


1. Application needs Remote Access. In other words, it is distributed.
2. Application needs to be scalable. EJB applications supports load balancing, clustering
and fail-over.
3. Application needs encapsulated business logic. EJB application is separated from
presentation and persistent layer.

Types of Enterprise Java Bean


There are 3 types of enterprise bean in java.

Session Bean
Session bean contains business logic that can be invoked by local, remote or
webservice client.

Message Driven Bean


Like Session Bean, it contains the business logic but it is invoked by passing message.

Entity Bean
It encapsulates the state that can be persisted in the database. It is deprecated. Now, it
is replaced with JPA (Java Persistent API).
JavaBean
In any programming language, reusability is the main concern. To achieve the same
concern, Java introduced the concept of JavaBean. It is a software component that has
been designed to be reusable in a variety of environments. In this section, we will dive
into the topic and understand the horizons of concept in this what is JavaBeans, its
advantages, disadvantages, Life cycle.

In other words, JavaBeans are classes that encapsulate multiple objects into a single
object. It also helps in accessing these object from multiple places. It contains several
elements like Constructors, getter and setter methods and much more.

A JavaBean is a Java class that should follow the following conventions:

o Class Definition: They are defined as public classes.


o Serializability (Optional): While not mandatory, they often implement
the Serializable interface to allow object data to be converted into a stream of
bytes for storage or transmission.
o No-argument Constructor: JavaBeans must have a public
constructor that takes no arguments (often called a no-arg constructor). It
simplifies object creation.
o Data Encapsulation: They encapsulate data using private member
variables (fields) to store the object's state.
o Getter and Setter Methods: For each private field, there should be a
corresponding public getter and setter method:

JavaBean Properties
A JavaBean property is a named feature that can be accessed by the user of the object.
The feature can be of any Java data type, containing the classes that you define.

A JavaBean property may be read, write, read-only, or write-only. JavaBean features are
accessed through two methods in the JavaBean's implementation class:

Properties of setter Methods


1. It must be public in nature.
2. The return type a should be void.
3. The setter method uses prefix set.
4. It should take some argument.

Properties of getter Methods


1. It must be public in nature.
2. The return type should not be void.
3. The getter method uses prefix get.
4. It does not take any argument.

Simple Example of JavaBean Class


File Name: Employee.java

1. //Employee.java
2.
3. package mypack;
4. public class Employee implements java.io.Serializable{
5. private int id;
6. private String name;
7. public Employee(){}
8. public void setId(int id){this.id=id;}
9. public int getId(){return id;}
10. public void setName(String name){this.name=name;}
11. public String getName(){return name;}
12. }

How to access the JavaBean class?


To access the JavaBean class, we should use getter and setter methods.

File Name: Test.java

1. package mypack;
2. public class Test{
3. public static void main(String args[]){
4. Employee e=new Employee();//object is created
5. e.setName("Arjun");//setting value to the object
6. System.out.println(e.getName());
7. }}
Note: There are two ways to provide values to the object. One way is by constructor and second
is by setter method.

You might also like