JDBC Flashcards

1
Q

What module is used to implement JDBC functionalities?

A

java.sql

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

What package contains JDBC classes?

A

java.sql.*

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

How does JDBC URL format look like?

A

jdbc:oracledb://localhost:54332/db

jdbc protocol
driver name
address and port
database name

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

What type of JDBC is used in exams?

A

DriverManager

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

What is the starting number of columns?

A

They count from 1, not 0

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

How is JDBC roughly structured?

A

Driver -> Connection -> PreparedStatement, CallableStatement -> ResultSet

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

How do we turn on auto. commit of the transactions?

A

conn.setAutoCommit(true)
This auto. commits current transaction, and every transaction that comes after it is auto. commited

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

How can we turn on manual commit of the transactions?

A

conn.setAutoCommit(false)
Then we can conn.commit() or conn.rollback()
If we close connection before committing or roll backing, transaction may or may not be committed.

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

How do we use bookmarks in JDBC?

A

We need to set autocommit to false.
Then we can set a new Savepoint().
When we want to go back to that savepoint, we just conn.rollback(savepoint);

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

What do we need to close after using in JDBC?

A

Connection, PreparedStatement, ResultSet

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

When is Statement used?

A

When we don’t need to provide user input data into SQL

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

When is PreparedStatement used?

A

When we need to provide user input data into SQL. It also provides better performance when queries are used repeatedly, because DB caches prepared statements

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

How do we create connection to DB?

A

Connection conn = DriverManager.gerConnection(url);
Best defined inside try-with-resources clause.

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

How do we create Statement or PreparedStatement?

A

PreparedStatement ps = conn.prepareStatement(“SQL here”);
Best defined inside try-with-resources clause.

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

Can SQL string in statement be empty?

A

No, there must be a query

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

Can Statement be used multiple times?

A

Yes

17
Q

What extra properties does PreparedStatement has?

A

First comes type, ResultSet.TYPE_FORWARD_ONLY, TYPE_SCROLL_INSENSITIVE, TYPE_SCROLL_SENSITIVE,
then comes concurrency - CONCUR_READ_ONLY, CONCUR_UPDATEABLE. Both must be set if used.

18
Q

What PreparedStatement method do we use for INSERT, UPDATE or DELETE?

A

executeUpdate()

19
Q

What does PreparedStatement’s executeUpdate() metod return?

A

Number of affected rows

20
Q

What PreparedStatement method do we use for SELECT?

A

executeQuery()

21
Q

Where is best place to do PreparedStatement’s executeQuery(), or put ResultSet in general?

A

In try-with-resources clause

22
Q

What is alternative to executeQuery() or executeUpdate?

A

ps.execute()
Then we can get result set with: ResultSet rs = ps.getResultSet()
Or we can get number of affected rows: int result = ps.getUpdateCount()

23
Q

How do we use variables in PreparedStatement?

A

Query must contain ? at places where variables need to be.
Then we define variables by their type :
ps.setInt(1, 1)
ps.setLong(2, 3L)
ps.setNull(3, null)
There must be exact number of parameters set as there are ? in query.

24
Q

How do we execute database procedures without any parameters?

A

String sql = “{call procedure_name()}”;
1. conn.prepareCall(sql)
2. cs.executeQuery()

25
Q

How do we call database procedures that have IN parameters?

A

String sql = “{call procedure_name(?)}”;
1. conn.prepareCall(sql)
2. cs.setString(prefix”, “Z”)
3. cs.executeQuery()

26
Q

How do we execute database procedures that have OUT parameters?

A

String sql = “{? = call procedure_name(?)}”;
try (Connection con = DriverManager.getConnection(“”)) {
try (CallableStatement ps = con.prepareCall(“”)) {
ps.execute();
ps.registerOutParameter(1, Types.VARCHAR);
try (ResultSet rs = ps.getResultSet()) {
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
}
}

27
Q

How do we execute database procedures that have IN OUT parameters?

A

String sql = “{call procedure_name(?)}”;
1. conn.prepareCall(sql)
2. cs.setString(prefix”, “Z”)
3. cs.registerOutParameter(1, Types.INTEGER);
4. cs.execute();
5. cs.getInt(“num”);

28
Q

How do we read from ResultSet?

A

We call rs.next(), which moves cursor from starting point to 0 and upwards.
rs.next() returns true until there is no more results, then it returns false.
We usually put rs.next() into while().
We get data by calling rs.getInt(“column name”), or rs.getString() etc.
We usually must check if there is something in ResultSet: