Lecture 6 - JDBC Flashcards

1
Q

What is JDBC and what is it for?

A

Gives a standard API for databases (for programming access)

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

What does java.sql.DriverManager do?

A

loads JDBC drivers and creates Connection objects

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

What does DriverManager.getConnection(String url) do?

And what is the format of the URL?

A

Looks for the driver to handle the URL connection.

jdbc:subprotocol:datasourcename

NOTE: username + password can be given in the URL.

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

What is jdbc.drivers?

A

A system property that drivers are loaded into.

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

What exception do JDBC methods usually throw?

A

SQLException

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

What is a statement object?

A

Create SQL Statement from the connection
to return Statement object

Used to query

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

How do we connect with properties to avoid hard-coding?

A

Create new java.util.Properties object (e.g.
options)

Use options.load(new FileInputStream(..))
OR
Query a property with options.getProperty

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

How do we query a database?

A
  1. create a statement using connection.createStatement();

OR prepareStatement with SQL_command template that can be filled with values (useful for user input)

  1. statement.executeQuery(some_SQL_code)
  2. a result set is returned
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do we create and fill placeholder values for prepareStatement()?

A

?
setType(position, value)
– E.g. setInt, setString, setDate, …

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

How do we work with ResultSet object?

A

Data is organized into columns and rows

call next() to get first row
returns false if no next row

column data obtained with getType()
(Ex: getInt, getString, getDate)

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

What is the issue with JDBC and why would be have to learn Jakarta Persistence?

A

Major issue is connections are expensive.

This is solved by creating a connection pool using javax.sql.DataSource.

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

ResultSet can return object that contains
information about the returned results (T or F)

A

True
ResultSetMetaData

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

Why do we NOT make a SQL query by concatenating user input?

A

Use prepared statements instead, will prevent attacks and SQL exceptions.

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

How can we access SQL warnings?

A

Less severe errors are not thrown, but generate
SQLWarning or DataTruncation warnings
getWarnings() method of any Connection,
Statement, ResultSet object

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