Database Access Servlets - Quiz Results
The answers you selected are indicated below, along with text that explains the correct answers.
1.
What are the consequences of making your database connection in init()?
Please select the best answer.
A.
Your servlet will be slower
B.
All your users will share one database connection
C.
You must add a getConnection() method to your servlet
D.
Your program will open a new connection for each user that loads the servlet
The correct answer is B.
All your users will share one database connection. If you have many servlets, each will have a connection. A is incorrect because it is actually a little faster to open the database once than to open and close it every time getPost() is called. C is incorrect because you do not need to add such a function to your servlet. D is incorrect because each user will use the same database connection.
2.
What is the full name of the Connection class discussed in this module?
Please select the best answer.
A.
javax.sql.Connection
B.
java.jdbc.sql.Connection
C.
java.sql.Connection
D.
javax.servlet.Connection
The correct answer is C.
The class is called java.sql.Connection and you import java.sql.* to use it easily. The other answers are incorrect.
3.
What is the name of the class in the java.sql package that represents the information extracted from the database?
Please select the best answer.
A.
Statement
B.
RecordSet
C.
ResultSet
D.
Connection
The correct answer is C.
A ResultSet object holds the results of your SELECT statement. B is incorrect because RecordSet is the name given to database information in other programming languages. A is incorrect because a Statement object represents the line of SQL you executed against the database. D is incorrect because a Connection object represents your connection to the database.
4.
After executing a statement of SQL to get a result set, what must you do before extracting information from the result set with getString()?
Please select the best answer.
A.
Close the database connection
B.
Close the statement object
C.
Reset the result set
D.
Call next() to move to the first record of the result set
The correct answer is D.
Before you can extract information, you must be looking at one record in the result set. You do this by calling the next() method of the ResultSet object. If it returns false, there is no next record. A is incorrect because theres no need to close the database before using the result set. B is incorrect because you should close the statement eventually, but you dont need to do so before using the result set. C is incorrect because a result set cannot be reset.
5.
When a database contains dates, numbers, or other non-string fields, calling getString() for one of those fields:
Please select the best answer.
A.
Will throw an exception
B.
Converts the value to a string
C.
Returns an empty string
D.
Will change the field type in the database
The correct answer is B.
Calling getString() gets you the string representation of the data in the database field. A is incorrect because an exception is not thrown. C is incorrect because the data will be converted to a valid string. D is incorrect because nothing you do with a result set can have any effect on the database from which it was extracted.
6.
What will be the effect of the SQL statement UPDATE PEOPLE SET AGE=20 ?
Please select the best answer.
A.
The records with 20 in the age field will be returned to a result set
B.
The age field from every record in the table will be returned to a result set
C.
The age field on every record in the table will be set to 20
D.
The age field on every record in the database will be set to 20
The correct answer is C.
Because this UPDATE statement is missing a WHERE clause, it will update every record in the PEOPLE table. A and B are incorrect because nothing is put into a result set from an UPDATE statement. D is incorrect because if this database contains more than one table, only the PEOPLE table will be affected, not the others.
7.
What does it mean when the ResultSet next() method returns false?
Please select the best answer.
A.
Your database connection has been closed
B.
You forgot to close your result set
C.
There is no next record in the result set
D.
Your SQL statement was invalid
The correct answer is C.
If next() returns false, there is no next record. If you are looking for the first record, it means there is no first record and the result set is empty. A is incorrect because a closed database connection will have more serious consequences than an empty result set. B is incorrect because you shouldnt close your result set until after you have all the information you need from it. D is incorrect because an empty result set means your SQL is valid, but the information you want is not in the database.