JDBC, SQL and HIBERNATE Flashcards

1
Q

What is a JDBC Driver?

A

JDBC Driver is a software component that enables java applications to interact with the database. There are 4 types of JDBC drivers:

	○ JDBC-ODBC bridge driver
	○ Native-API driver (partially java driver)
	○ Network protocol driver (fully java driver) Thin driver (fully java driver)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the steps to connect to the database in Java?

A

There are 5 steps to follow:
○ Registering the driver class
○ Creating the connections (using DriverManager.getConnection())
○ Creating a statement (Statement statement)
○ Executing queries
Closing the connection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the different JDBC API components?

A

Interfaces: Connection, Statement, Prepared statement, ResultSet, ResultSet MetaData, Database MetaData, Callable Statement;

Classes: Driver Manager, Blob, Clob, Types, SQL Exception;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between execute, executeQuery and executeUpdate?

A

executeQuery()

This method is used to execute the SQL statements which retrieve some data from the database.

This method returns a ResultSet() object which contains the results returned by the query.

This method is used to execute only select queries.

executeUpdate()

This method is used to execute the SQL statements which update or modify the database.

This method returns an int value which represents the number of rows affected by the query. This value will be 0 for statements which return nothing.

This method is used to execute only non-select queries.

Example:
DML -> INSERT, UPDATE and DELETE
DDL -> CREATE, ALTER
---
Execute()

This method can be used for any kind of SQL statements.

This method returns a boolean value. TURE indicates that query returned a ResultSet object and FALSE indicates that query returned an int value or returned nothing.

This method can be used for both select and non-select queries.

This method can be used for any type of SQL statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

SQL how to do table JOIN?

A
SELECT
		  product.name AS product_name,
		  category.name AS category_name
		FROM product
		JOIN category ON product.category_id=category.id;

Selects records that have matching values in both tables.

	i. The join is done by the JOIN operator. 
	ii. In the FROM clause, the name of the first table (product) is followed by a JOIN keyword then by the name of the second table (category). 
	iii. This is then followed by the keyword ON and by the condition for joining the rows from the different tables. 
	iv. The category name is assigned based on the column id of the d table, which is equivalent to category_id in the table product (product.category_id=category.id).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Hibernate Framework?

A

Hibernate is a framework which is used to develop persistence logic which is independent of Database software. In JDBC to develop persistence logic we deal with primitive types. Whereas Hibernate framework we use Objects to develop persistence logic which are independent of database software.

Hibernate provides reference implementation of Java Persistence API that makes it a great choice as ORM tool with benefits of loose coupling. 

Hibernate configurations are flexible and can be done from an XML configuration file as well as programmatically.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the important benefits of using Hibernate Framework?

A
  1. Hibernate eliminates all the boiler-plate code.
    2. Hibernate framework provides support for XML as well as JPA annotations (java persistent api’s).
    3. Hibernate provides a powerful query language (HQL) that is similar to SQL.
    4. Hibernate is an open source project from Red Hat Community and used worldwide.
    5. For database vendor specific features, hibernate is suitable because we can also execute native sql queries.
  2. Hibernate cache helps us in getting better performance.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain Hibernate architecture?

A

Hibernate has a layered architecture which helps the user to operate without having to know the underlying APIs. Hibernate makes use of the database and configuration data to provide persistence services (and persistent objects) to the application.
The Hibernate architecture is categorized in four layers.

	○ Java application layer
	○ Hibernate framework layer
	○ Backhand api layer
	○ Database layer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between the get and load method?

A

Get()

Returns null if object is not found.

Get() methods always hits the database.

It returns a real object not proxy.

Load()

Throws ObjectNotFoundException() if object is not found.

Load() methods doesn’t hit the database.

Returns proxy object.

Should be used if you are sure that the instance exists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the advantages of Hibernate over JDBC?

A

If you’re writing a very small app - there is no need for Hibernate.
Hibernate level is required when you’re writing an enterprise app, where you need to save time etc.

	1. Hibernate removes boiler-plate code that comes with JDBC API.
	2. Hibernate supports inheritance, associations and collections which are not present in JDBC API.
	3. Hibernate implicitly provides transaction management.
	4. JDBC API throws SQLException that is a checked exception, so we need to write a lot of try-catch block code.
	5. Hibernate Query Language (HQL) is more object oriented and close to java programming language. For JDBC, we need to write native sql queries. 6. Hibernate supports caching that is better for performance, JDBC queries are not cached hence performance is low.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly