3. Java 7: JDBC API Flashcards
What is something you should always do when establishing a database connections using the DriverManager prior to JDBC 4.0?
Since JDBC 4.0 database drivers are loaded automatically. Prior to JDBC 4.0 they should be loaded manually:
try {
Class.forName(“oracle.jdbc.drive.OracleDriver”);
catch(ClassNotFoundException e) {}
How can you set up a connection to a database using the DriverManager? Name the three methods.
DriverManager.getConnection(String url);
DriverManager.getConnection(String url, Properties properties);
DriverManager.getConnection(String url, String user, String password);
There are two types of RowSet’s, name these two.
Connected RowSet - keeps it’s connection alive
Disconnected RowSet - closes the connection once the command (query) is finished.
What is a RowSet?
RowSet is a interface that is used to retrieve and change data from a database-source.
Given the following information, how do you execute the query using a JdbcRowSet?
user: admin
passworod: 123
url: mysql:/localhost:1433
query: select name from user
RowSetFactory factory = RowSetProvider.newFactory();
JdbcRowSet rowSet = factory.createJdbcRowSet();
rowSet.setUrl(url);
rowSet.setUsername(user);
rowSet.setPassword(password);
rowSet.setCommand(query);
rowSet.execute();
How can you create a RowSetFactory?
RowSetProvider.newFactory(); RowSetProvider.newFactory("com.sun.rowset.RowSetFactoryImpl", null);
Name the 5 sub-interfaces of the RowSet-interface.
CachedRowSet FilteredRowSet JdbcRowSet JoinRowSet WebRowSet
What are the two main goals that transactions achieve?
- Make sure that all the steps of certain logical work are executed.
- If a step fails all previous steps steps can be undone and the database will revert to a state before the first step.
How should a transaction be commited using an Connection object?
connection.commit();
What method is called when a transactie is reverted?
connection.rollback();
What is a transaction in the JDBC API?
All transaction logic happens at the Connection object. A Connection object may exist of zero or more Statement objects. When the commit method is called all Statements will be commited as the new permanent state. When the system calls the Rollback method all Statements currently inside the Connection will be rolled back.
True or False:
A JDBC connection defaults to auto-committing statements
True.
By Default every statement that is executed using a Connection is commited directly.
How can you enable/disable auto-commits using a Datasource object?
Connection connection = dataSource.getConnection();
connection.setAutoCommit(false);
Given the following code: Connection connection = dataSource.getConnection(); connection.setAutoCommit(false); connection.execute(statement1); connection.execute(statement2); connection.setAutoCommit(true); connection.execute(statement3);
Which of the statements are committed after the code completes
1,2 and 3
When chaning auto commit all open transactions are committed
Given I have a Datasource object how can I create a statement?
Connection connection = dataSource.getConnection();
connection.createStatement();