JDBC Flashcards
What module is used to implement JDBC functionalities?
java.sql
What package contains JDBC classes?
java.sql.*
How does JDBC URL format look like?
jdbc:oracledb://localhost:54332/db
jdbc protocol
driver name
address and port
database name
What type of JDBC is used in exams?
DriverManager
What is the starting number of columns?
They count from 1, not 0
How is JDBC roughly structured?
Driver -> Connection -> PreparedStatement, CallableStatement -> ResultSet
How do we turn on auto. commit of the transactions?
conn.setAutoCommit(true)
This auto. commits current transaction, and every transaction that comes after it is auto. commited
How can we turn on manual commit of the transactions?
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 do we use bookmarks in JDBC?
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);
What do we need to close after using in JDBC?
Connection, PreparedStatement, ResultSet
When is Statement used?
When we don’t need to provide user input data into SQL
When is PreparedStatement used?
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 do we create connection to DB?
Connection conn = DriverManager.gerConnection(url);
Best defined inside try-with-resources clause.
How do we create Statement or PreparedStatement?
PreparedStatement ps = conn.prepareStatement(“SQL here”);
Best defined inside try-with-resources clause.
Can SQL string in statement be empty?
No, there must be a query