In: Computer Science
Because the database server can cache and reuse ___________________ statements, they are more efficient than regular SQL statements.
When you work with a result set, the ____________ identifies the current position in the result set.
You use the __________________ class to work with data returned from a SQL query.
What does the invoices result set contain after the code that
follows is executed?
String sql = "SELECT InvoiceDate, InvoiceTotal "
+
"FROM Invoices WHERE InvoiceTotal > ?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setDouble(1, 100);
ResultSet invoices = ps.executeQuery();
a. |
All rows from the Invoices table |
|
b. |
Rows from the Invoices table where InvoiceTotal is greater than 100 |
|
c. |
Rows from the Invoices table where InvoiceTotal is greater than 0 |
|
d. |
Rows from the Invoices table where InvoiceTotal is greater than 1 |
Answers:
Because the database server can cache and reuse Prepared Statements statements, they are more efficient than regular SQL statements.
Explanation : Prepared Statements are used for executing sql queries dynamically.They are pre-compiled SQL statements invoked with different paramters and reduce the time of SQL statement compilation.Prepared Statements are very faster as they are compiled only once.
When you work with a result set, the getRow()
identifies the current position of the pointer in the result
set.
We can toggle the cursors front and back using specific methods
(last(),beforeFirst()) available from Result Set Object.
Explanation: Method getRow() points to current
position of the cursor in Result Set. The initial position of
the
cursor starts with 0.
You use the ResultSet to work with data returned from a SQL query.
Explanation: ResultSet Java Object is used to
store the results retrieved from executing queries using Statements
available from JDBC. Result available in ResultSet object are
retrieved one by one using next method and there are set of get
methods to access colums from each rows.
----------------------------------------------------------------------------------------------------------------------------------------------------------
Solution : B. Rows from the Invoices table where InvoiceTotal is greater than 100
Explanation :
String sql = "SELECT InvoiceDate, InvoiceTotal "
+ "FROM Invoices WHERE InvoiceTotal > ?";
PreparedStatement ps = connection.prepareStatement(sql);
// In the place of '?' parameter value 100 is passed.
ps.setDouble(1, 100);
//result is executed and results are stored here.
ResultSet invoices = ps.executeQuery();
The query returns only fields mentioned InvoiceDate and InvoiceTotal from Invoices table where the InvoiceTotal is greaterthan 100. In case if there is a alteration in Query as select * from Invoices WHERE InvoiceTotal > ?; The result will return all the column from the rows matching criteria.