JDBC Flashcards
The JDBC classes are part of the packages java.sql.* and javax.sql.*?
Yes
JDBC 4.1 introduces RowSet and its related utility classes such as RowSetFactory and RowSetProvider?
Yes
Connection interface of the java.sql package represents a connection from application to the database?
Yes
You can get a connection object by invoking the DriverManager.getConnection() method; the method expects the URL of the database along with a database name, user name, and password?
Yes
Prior to JDBC 4.0, you would have to explicitly load the JDBC driver using the Class.forName() statement, as in the following: Class.forName(“com.mysql.jdbc.Driver”).newInstance(); Connection connection = DriverManager.getConnection(url + database, userName, password)
Yes
JDBC provides two important interfaces to support queries: Statement and Resultset.
Yes
Statement comes in three flavors: Statement, PreparedStatement, and CallableStatement
Yes
when you need to send a SQL statement to the database without any parameter. In normal cases, you need to use…
Statement
You can create an instance of Statement using…
…createStatement() method in the Connection interface
a precompiled SQL statement that can be customized using IN parameters. Usually, it is more efficient than a Statement object; hence, it is used to improve the performance, especially if a SQL statement is executed multiple times…
PreparedStatement
You can get an instance of PreparedStatement by calling the…
preparedStatement() method in the Connection interface.
used to execute stored procedures…
CallableStatement
to get an instance of CallableStatement…
You need to call the prepareCall() method in the Connection interface to get an instance of this class
If your SQL statement is a SELECT query, you can use the … method, which returns a ResultSet
executeQuery()
When you want to update a database using one of the INSERT, UPDATE, or DELETE statements, you should use the…() method, which returns an integer reflecting the updated number of rows
executeUpdate()
If you don’t know the type of SQL statement, you can use the…() method, which may return multiple resultsets or multiple update counts or a combination of both
execute()
ResultSet is a table with column headings and associated values requested by the query…
Yes
column index starts from …
1, not from 0
always call updateRow() after modifying the row contents; otherwise you will lose the changes
Yes
You can get the metadata from a Connection object to examine the capabilities of the underlying database. You can do this by calling the …
getMetaData() method in the Connection interface
The boolean absolute(int) method in ResultSet moves the cursor to the passed row number in that ResultSet object
Yes
The boolean absolute(int) method if the row number is negative,
…it moves to that position from the end of the ResultSet object. Assume that there are 10 entries in the ResultSet object. Calling absolute(3) will move the cursor to the third row. Calling absolute(−3) will move the cursor to the 10–3, seventh row. If you give out of range values, the cursor will move to either beginning or end
In a ResultSet object, calling absolute(1) is equivalent to calling first(), and calling absolute(−1) is equivalent to calling last
Yes
Atomicity…
Each transaction should be carried out in its entirety; if one part of the transaction fails, then the whole transaction fails
Consistency:
Consistency: The database should be in a valid state before and after the performed transaction
Isolation
Isolation: Each transaction should execute in complete isolation without knowing the existence of other transactions
Durability:
Once the transaction is complete, the changes made by the transaction are permanent (even in the occurrence of unusual events such as power loss
Transaction-Related Methods in the Connection Interface…
void setAutoCommit(boolean autoCommit) boolean getAutoCommit() Savepoint setSavepoint() Savepoint setSavepoint(String name) void releaseSavepoint(Savepoint savepoint) void rollback(Savepoint savepoint) void rollback() void commit
You can use column name or column index with ResultSet methods…
Yes The index you use is the index of the ResultSet object, not the column number in the database table
It is your responsibility to issue a correct SQL command…
Yes…a JDBC Statement will not check for its correctness. For example, if there is a syntax error in the SQL command string, then you will not get a compiler error. Rather, you’ll get a MySQLSyntaxErrorException at runtime