JDBC Flashcards Preview

Java8 > JDBC > Flashcards

Flashcards in JDBC Deck (40)
Loading flashcards...
1

What is database

Collection of information

2

Relational database

Database that uses table to relate the information

3

How many key interfaces we use in JDBC?

4 key interfaces

4

Where does the concrete classes that implement the interfaces comes from?

from JDBC driver

5

what to understand properly among the jars for different databases?

Each databases have different jar files with these classes

6

What are the key interfaces in JDBC?

Driver, Connection, Statement, ResultSet

7

What are the different interfaces mean?

Driver: How to connect to the database
Connection: How to communicate to the database
Statement: How to run the query
ResultSet: Knows what was returned by the SQL queries

8

Where do all the databases classes lies?

java.sql

9

What is the JDBC URL format?

It has three parts:
1. jdbc -> Protocol
2. oracle -> Product/Vendor Name
3. localhost:8080/Ganesh -> DB connection details

all seperated by ':'

10

What are the two ways to get a connection?

DriverManager or Datasource

11

What we should use? DriverManager or Datasource?

Datasource.

Datasource is a factory and it has more features than DriverManager. It can pool connections and store database connection outside our application

12

Where does the implementation class for connection exists?

Inside driver jar

13

What is the use of Class.forName()?

It loads a class. It lets the DriverManager use a Driver even if the JAR doesn't have 'META-INF/services/java.sql.Driver file'

14

What does the statement represents?

It represents the SQL statement that you want to run using the connection

15

What is the default type of ResultSet

TYPE_FORWARD_ONLY mode

16

What are the three modes of ResultSet type?

1. TYPE_FORWARD_ONLY
2. TYPE_SCROLL_INSENSITIVE
3. TYPE_SCROLL_SENSITIVE

17

What does TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE mean? Whats the difference

Both, allow to go through the data in any order either forward or backward. We can even go to a specific spot.

Difference: What happens when the data changes in actual database while we are busy querying.

TYPE_SCROLL_INSENSITIVE- will have a static view of what result set looked like when we queried. - igf data changed in table, you will not see the latest

TYPE_SCROLL_SENSITIVE - We would see the latest data when scrolling through the ResultSet

18

whats not guaranteed with TYPE_SCROLL_SENSITIVE

Most database and database drivers dont support this type

19

What are the different Concurrency mode?

CONCUR_READ_ONLY
CONCUR_UPDATEABLE

20

What is not required in concurrency mode?

Databases and drivers are not required to support concur_updateable.

21

What happens when the mode of scroll/concur mode that we request is not available?

The driver can downgrade you.

Means, if you ask TYPE_SCROLL_SENSITIVE and if it isn't available, we get TYPE_SCROLL_INSENSITIVE

Similarly if we request CONCUR_UPDATEABLE and if it is not available we get statement that is "CONCUR_READ_ONLY'

22

Which method is used for all update/insert/delete functionalities and return int?

executeUpdate(query)

23

Which query returns a ResultSet and what kind of dml command is used?

execureQuery(query)

24

Which method is used for both Select and Update

execute(sql)

25

How to find whether the data returned is a ResultSet or Int when using query()?

execute() returns boolean. If it is trues, we can get ResultSet else, we can get the count for updated data.

stmt.getResulySet() -> ResultSet
stmt.getgetUpdateCount() -> count

26

Why we shoud not use Statement and what should we use and why?

PreparedStatement.

advantages: Performance, Security, Readability

Performance: In most programs, we run similar queries multiple times. Prepared stmt figures out a plan to run SQL well and remembers it

Security: It prevents sqlInjection by using setString() or other set methods so that driver takes care of all escaping for us

Readablility: we dont want to use string concatenation

27

How to loop through the resultSet?

hasNext() method

28

Explain about the ResultSet and cursor

ResultSet has a cursor, that points to the current location in the data.

The cursor starts pointing out the location befroe the resultset. On the first loop iteration, rs.next() returns true and the cursor moves to point to the first data. At the end, rs.next() returns false. The cursor advances past the end of data.

29

JDBC counts starts with one or 0?

1

30

What happens when you attampt to access the colum that doesnt available in DB?

SQL Exception