c13 JSE JDBC NoSQL
c13 JSE JDBC NoSQL
presentation
Java Programming – Software App Development
Cristian Toma
1 2 3
JDBC, RDBMS
& NoSQL
Concepts
NoSQL
Programming
Exchange
Ideas
1
JDBC Driver Types, JDBC API
JDBC Concepts
1. JDBC Concepts
JDBC Concepts:
Java Database Connectivity (JDBC) is an Application
Programming Interface(API) used to connect Java
application with Database. JDBC is used to interact with
various type of Database such as Oracle, MS Access, My
SQL and SQL Server (even SQLite). JDBC can also be
defined as the platform-independent interface between a
relational database and Java programming. It allows Java
program to execute SQL statement and retrieve result from
database.
http://www.studytonight.com/java/introduction-to-jdbc
1. JDBC Concepts
JDBC Driver Types:
http://www.studytonight.com/java/introduction-to-jdbc
1. JDBC Concepts
JDBC Driver 1:
http://www.studytonight.com/java/introduction-to-jdbc
1. JDBC Concepts
JDBC Driver 2:
http://www.studytonight.com/java/introduction-to-jdbc
1. JDBC Concepts
JDBC Driver 3:
http://www.studytonight.com/java/introduction-to-jdbc
1. JDBC Concepts
JDBC Driver 4:
http://www.studytonight.com/java/introduction-to-jdbc
1. JDBC Concepts
JDBC API:
http://www.studytonight.com/java/introduction-to-jdbc
1. JDBC Concepts
JDBC API:
http://www.studytonight.com/java/introduction-to-jdbc
1. JDBC Concepts
JDBC Steps:
http://www.studytonight.com/java/introduction-to-jdbc
1. JDBC Concepts
1 - JDBC Steps:
http://www.studytonight.com/java/introduction-to-jdbc
1. JDBC Concepts
2 - JDBC Steps:
http://www.studytonight.com/java/introduction-to-jdbc
1. JDBC Concepts
3 - JDBC Steps:
http://www.studytonight.com/java/introduction-to-jdbc
1. JDBC Concepts
4 - JDBC Steps:
http://www.studytonight.com/java/introduction-to-jdbc
1. NoSQL Concepts
MongoDB is an open-source document database and leading NoSQL
database. MongoDB is written in C++. MongoDB is a cross-platform,
document oriented database that provides, high performance, high
availability, and easy scalability. MongoDB works on concept of collection
and document.
Database
Database is a physical container for collections. Each database gets its
own set of files on the file system. A single MongoDB server typically has
multiple databases.
Collection
Collection is a group of MongoDB documents. It is the equivalent of an
RDBMS table. A collection exists within a single database. Collections do
not enforce a schema. Documents within a collection can have different
fields. Typically, all documents in a collection are of similar or related
purpose.
Document
A document is a set of key-value pairs. Documents have dynamic
schema. Dynamic schema means that documents in the same collection
do not need to have the same set of fields or structure, and common
fields in a collection's documents may hold different types of data.
https://www.tutorials point.com/mongodb/index.htm
1. NoSQL Concepts
The following table shows the relationship of RDBMS terminology with MongoDB.
https://www.tutorials point.com/mongodb/index.htm
1. NoSQL Concepts
Sample Document
$ mongo
> db.stats()
1. NoSQL Concepts – Data Modelling
• Combine objects into one document if you will use them together.
Otherwise separate them (but make sure there should not be need of
joins).
In RDBMS schema,
design for above
requirements will have
minimum three tables.
https://www.tutorials point.com/mongodb/index.htm
1. NoSQL Hands-On
MongoDB use DATABASE_NAME is used to create database. The command will create a new
database if it doesn't exist, otherwise it will return the existing database.
Syntax
Basic syntax of use DATABASE statement is as follows − use DATABASE_NAME
Example
If you want to create a database with name <mydb>, then use DATABASE statement would
be as follows −
>use mydb
switched to db mydb
To check your currently selected database, use the command db
>db mydb
If you want to check your databases list, use the command show dbs.
>show dbs
local 0.78125GB
test 0.23012GB
Your created database (mydb) is not present in list. To display database, you need to insert at
least one document into it.
>db.movie.insert({"name":"tutorials point"})
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
In MongoDB default database is test. If you didn't create any database, then collections will be
stored in test database. https://www.tutorials point.com/mongodb/index.htm
1. NoSQL Hands-On
The dropDatabase() Method – DELETE DATABASE
MongoDB db.create
Collection(name,
options) is used to
create collection.
In the
command, name is
name of collection
to be
created. Options is
a document and is
used to specify
configuration of
collection.
https://www.tutorials point.com/mongodb/index.htm
1. NoSQL Hands-On
The createCollection() Method – CREATE COLLECTION <-> “TABLE”
Examples
Basic syntax of createCollection() method without options is as follows −
>use test
switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 }
You can check the created collection by using the command show collections.
>show collections
mycollection
system.indexes
The following example shows the syntax of createCollection() method with few
important options −
>db.createCollection("mycol", { capped : true, autoIndexId : true, size : 6142800, max : 10000 } )
{ "ok" : 1 }
In MongoDB, you don't need to create collection. MongoDB creates collection
automatically, when you insert some document.
>db.tutorialspoint.insert({"name" : "tutorialspoint"})
>show collections
mycol
mycollection
system.indexes
tutorialspoint
https://www.tutorials point.com/mongodb/index.htm
1. NoSQL Hands-On
The drop() Method – DELETE COLLECTION <-> “TABLE”
MongoDB's db.collection.drop() is used to drop a collection from the database.
Syntax
Basic syntax of drop() command is as follows −
db.COLLECTION_NAME.drop()
Example
First, check the available collections into your database mydb.
>use mydb
switched to db mydb
>show collections
mycol
mycollection
system.indexes
tutorialspoint
Now drop the collection with the name mycollection.
>db.mycollection.drop()
true
Again check the list of collections into database.
>show collections
mycol
system.indexes
tutorialspoint
drop() method will return true, if the selected collection is dropped successfully,
otherwise it will return false. https://www.tutorials point.com/mongodb/index.htm
1. NoSQL Concepts
MongoDB supports many datatypes. Some of them are −
• String − This is the most commonly used datatype to store the data. String in
MongoDB must be UTF-8 valid.
• Integer − This type is used to store a numerical value. Integer can be 32 bit or
64 bit depending upon your server.
• Boolean − This type is used to store a boolean (true/ false) value.
• Double − This type is used to store floating point values.
• Min/ Max keys − This type is used to compare a value against the lowest and
highest BSON elements.
• Arrays − This type is used to store arrays or list or multiple values into one key.
• Timestamp − ctimestamp. This can be handy for recording when a document has
been modified or added.
• Object − This datatype is used for embedded documents.
• Null − This type is used to store a Null value.
• Symbol − This datatype is used identically to a string; however, it's generally
reserved for languages that use a specific symbol type.
• Date − This datatype is used to store the current date or time in UNIX time
format. You can specify your own date time by creating object of Date and passing
day, month, year into it.
• Object ID − This datatype is used to store the document’s ID.
• Binary data − This datatype is used to store binary data.
• Code − This datatype is used to store JavaScript code into the document.
• Regular expression − This datatype is used to store regular expression.
https://www.tutorials point.com/mongodb/index.htm
1. NoSQL Hands-On
The insert() Method – INSERT DOCUMENT <-> “TUPLE/ROW”
To insert data into MongoDB collection, you need to use
MongoDB's insert() or save() method.
Syntax
The basic syntax of insert() command is as follows −
>db.COLLECTION_NAME.insert(document)
Example
>db.mycol.insert({ _id: ObjectId(7df78ad8902c), title: 'MongoDB Overview', description:
'MongoDB is no sql database', by: 'tutorials point', url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'], likes: 100 })
Here mycol is our collection name, as created in the previous chapter. If
the collection doesn't exist in the database, then MongoDB will create this
collection and then insert a document into it.
In the inserted document, if we don't specify the _id parameter, then
MongoDB assigns a unique ObjectId for this document.
_id is 12 bytes hexadecimal number unique for every document in a
collection. 12 bytes are divided as follows −
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tutorials point",
"url": "http://www.tutorialspoint.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
https://www.tutorials point.com/mongodb/index.htm
1. NoSQL Hands-On
The update() Method – UPDATE DOCUMENT <-> “TUPLE/ROW”
MongoDB's update() and save() methods are used to update document into a collection. The
update() method updates the values in the existing document while the save() method
replaces the existing document with the document passed in save() method.
The update() method updates the values in the existing document.
Syntax
The basic syntax of update() method is as follows −
>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
Example
Consider the mycol collection has the following data.
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
Following example will set the new title 'New MongoDB Tutorial' of the documents
whose title is 'MongoDB Overview'.
>db.mycol.update( {'title':'MongoDB Overview'}, {$set:{'title':'New MongoDB Tutorial'}} )
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
By default, MongoDB will update only a single document. To update multiple
documents, you need to set a parameter 'multi' to true.
>db.mycol.update({'title':'MongoDB Overview'}, {$set:{'title':'New MongoDB Tutorial'}},{multi:true})
https://www.tutorials point.com/mongodb/index.htm
1. NoSQL Hands-On
The save() Method – SAVE/UPDATE DOCUMENT <-> “TUPLE/ROW”
The save() method replaces the existing document with the new
document passed in the save() method.
Syntax
The basic syntax of MongoDB save() method is shown below −
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
Example
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"Tutorials Point New Topic",
"by":"Tutorials Point"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"} >
https://www.tutorials point.com/mongodb/index.htm
1. NoSQL Hands-On
The remove() Method – DELETE DOCUMENT <-> “TUPLE/ROW”
MongoDB's remove() method is used to remove a document from the
collection. remove() method accepts two parameters. One is deletion
criteria and second is justOne flag.
• deletion criteria − (Optional) deletion criteria according to documents
will be removed.
• justOne − (Optional) if set to true or 1, then remove only one document.
Syntax
Basic syntax of remove() method is as follows −
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
Example
Consider the mycol collection has the following data.
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
Following example will remove all the documents whose title is 'MongoDB
Overview'.
>db.mycol.remove({'title':'MongoDB Overview'})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
https://www.tutorials point.com/mongodb/index.htm
1. NoSQL Hands-On
The find() Method – PROJECTION of COLLECTION <-> “TABLE”
In MongoDB, projection means selecting only the necessary data rather than selecting whole of
the data of a document. If a document has 5 fields and you need to show only 3, then select
only 3 fields from them.
MongoDB's find() method, exposed in QUERY DOCUMENT section, accepts second optional
parameter that is list of fields that you want to retrieve. In MongoDB, when you
execute find() method, then it displays all fields of a document. To limit this, you need to set a
list of fields with value 1 or 0. 1 is used to show the field while 0 is used to hide the fields.
Syntax
The basic syntax of find() method with projection is as follows −
>db.COLLECTION_NAME.find( {}, {KEY:1} )
Example
Consider the collection mycol has the following data −
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
Following example will display the title of the document while querying the
document.
>db.mycol.find( {}, {"title":1, _id:0} )
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}
{"title":"Tutorials Point Overview"}
Please note _id field is always displayed while executing find() method, if you don't
want this field, then you need to set it as 0.
https://www.tutorials point.com/mongodb/index.htm
Section Conclusion
Java is suitable for
Fact:
Databases
In few samples it is simple to understand: JDBC
API, NoSQL programming and remember
databases concepts.
2
Java SQLite – SQL Insert, Select, Update, Delete + NoSQL - MongoDB
JDBC Programming
for easy sharing
3
Java Relational & NoSQL
But wait…
There’s More!
What’s
Thanks!Your Message?