JDBC Flashcards

1
Q

What is database

A

Collection of information

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

Relational database

A

Database that uses table to relate the information

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

How many key interfaces we use in JDBC?

A

4 key interfaces

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

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

A

from JDBC driver

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

what to understand properly among the jars for different databases?

A

Each databases have different jar files with these classes

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

What are the key interfaces in JDBC?

A

Driver, Connection, Statement, ResultSet

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

What are the different interfaces mean?

A

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

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

Where do all the databases classes lies?

A

java.sql

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

What is the JDBC URL format?

A

It has three parts:

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

all seperated by ‘:’

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

What are the two ways to get a connection?

A

DriverManager or Datasource

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

What we should use? DriverManager or Datasource?

A

Datasource.

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

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

Where does the implementation class for connection exists?

A

Inside driver jar

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

What is the use of Class.forName()?

A

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’

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

What does the statement represents?

A

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

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

What is the default type of ResultSet

A

TYPE_FORWARD_ONLY mode

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

What are the three modes of ResultSet type?

A
  1. TYPE_FORWARD_ONLY
  2. TYPE_SCROLL_INSENSITIVE
  3. TYPE_SCROLL_SENSITIVE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE mean? Whats the difference

A

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
Q

whats not guaranteed with TYPE_SCROLL_SENSITIVE

A

Most database and database drivers dont support this type

19
Q

What are the different Concurrency mode?

A

CONCUR_READ_ONLY

CONCUR_UPDATEABLE

20
Q

What is not required in concurrency mode?

A

Databases and drivers are not required to support concur_updateable.

21
Q

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

A

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
Q

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

A

executeUpdate(query)

23
Q

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

A

execureQuery(query)

24
Q

Which method is used for both Select and Update

A

execute(sql)

25
Q

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

A

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
Q

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

A

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
Q

How to loop through the resultSet?

A

hasNext() method

28
Q

Explain about the ResultSet and cursor

A

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
Q

JDBC counts starts with one or 0?

A

1

30
Q

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

A

SQL Exception

31
Q

Is there getChar() method?

A

No

32
Q

What is the method that allows us to go back to previous row in resultset?

A

previous()

33
Q

Method to get start and end of the resultSet?

A

first() and last() .

both return boolean whether they are successfull in finding the row or not

34
Q

Which methods return void and why?

A

beforeFirst() and afterLast() method returns void as there is always possibility that they can be in a spot that doesnt have data

35
Q

Which is the method that takes the cursor to the exact row?

A

absolute(int num).

Positive number means, its starts from first and moves the position down to the numbered row.

zero means moves the cursor immediately before the first.

Negative means counting from the end of the result rather than from beginning

36
Q

Method that helps to move forward or backward the cursor to current position?

A

relative(int num) method

37
Q

What does relative(int) method returns?

A

Boolean.

Returns true if the cursor points to a data

38
Q

Is it mandatory to close all resources like connection/Resultset/Statement?

A

No, closing any resource automatically closes the resources that it created.

Eg.., Closes Connection, automatically closes Statement and ResultSet

Closing the Statement, automatically closes the ResultSet

39
Q

What is more important in closing the resources?

A
  1. The order in which the resources are closed is more important.

First, RsultSet must be closed followd by Statement and Connection

40
Q

Very important point to remenmber on ResultSet Closing?

A

JDBC automatically closes a ResultSet when

you run another SQL statement from the same Statement