JDBC database connection pool exercises


"JDBC database connection pool exercises"

multiple choice

1.
Among the following options, the method to move the cursor down one line from the current position is ( ).

A. next()
B. absolute(int row)
C. previous()
D. last()
Reference answer :
A
Answer description :
next() moves the cursor down one line from the current position; absolute(int row) moves the cursor to the specified row of this ResultSet object; previous() moves the cursor to the previous row of this ResultSet object; last() moves the cursor to the last row of this ResultSet object.
2.
Please sort the following JDBC programming steps (). A Release resources B Obtain a physical connection to the database C Execute SQL commands D Register JDBC Driver E Create different types of Statement F If there is a result set, process the result set

A, ABCDEF
B, DBECFA
C, BDCEFA
D, FEDCBA
reference answer :
B
Answer explanation :
The use of JDBC can be carried out in the following steps: 1. Load and register the database driver 2. Obtain the database connection through DriverManager 3. Obtain through the Connection object Statement object 4. Use the Statement object to execute SQL statements 5. Operate the ResultSet result set 6. Close the connection and release resources
3.
In the following options, the method used to send parameterized SQL statements to the database is ().

A. prepareCall(String sql)
B. prepareStatement(String sql)
C. registerDriver(Driverdriver)
D. createStatement()
reference answer :
B
Answer description :
prepareStatement(String sql) is used to create a PreparedStatement object and parameterize the SQL statement Send to the database
4,
the following options, the object that can be used to store the result set is ().

A, ResultSet
B, Connection
C, Statement
D, PreparedStatement
reference answer :
A
Answer explanation :
ResultSet interface is used to save the result set returned when JDBC executes the query; Connection interface represents the connection between Java program and database; Statement interface is used to execute static SQL statement, and return a result object; PreparedStatement is a sub-interface of Statement, used to execute precompiled SQL statement.
5.
JDBC is responsible for establishing a connection with the database, and the interface that provides a unified standard data source is ().

A, DriverManager
B, Connection
C, Statement
D, ResultSet
Reference answer :
B
Answer explanation :
Connection interface represents the connection between the Java program and the database. Only after obtaining the connection object can the database be accessed and the data table operated.
6.
Among the following options, the method used to execute addition, deletion and modification statements in the QueryRunner class is ().

A, insert
B, update
C, delete
D, query
Reference answer :
B
Answer description :
The update() method of the QueryRunner class can perform an update (insert, delete, update) operation
7.
The following descriptions of common methods of the QueryRunner class are wrong yes().

A. Object query (Connection conn, String sql, ResultSetHandler rsh, Object[] params) execute query operation, the incoming Connection object can be empty B. Object query (
String sql, ResultSetHandler rsh, Object[] params) execute query operation
C. Object query (Connection conn, String sql, ResultSetHandler rsh) executes a query operation that does not require replacement parameters
D. int update (Connection conn, String sql, ResultSetHandler rsh) executes an update (insert, delete, update) operation reference
answer :
A
Answer description :
Object query(Connection conn,String sql,ResultSetHandler rsh,Object[] params) Execute query operation, the incoming Connection object cannot be empty.
8.
In the API provided by the DBUtils toolkit, ( ) is used to encapsulate the result set.

A. PreparedStatement
B. DBUtils
C. QueryRunner
D. ResultSetHandler
Reference answer :
D
Answer description :
The ResultSetHandler interface is used to process the ResultSet result set. It can convert the data in the result set into different forms.
9.
In the following description about the implementation class of the custom ResultSetHandler interface, the correct one is ().

A. By rewriting the handle() method, the processing of the result set is realized.
B. By rewriting the update() method, the processing of the result set is realized.
C. By rewriting the query() method, the processing of the result set is realized
. D. By rewriting Write the delete() method to realize the processing of the result set
Reference answer :
A
Answer description :
The ResultSetHandler interface provides a separate method handle (java.sql.ResultSet rs), you can customize a class that implements the ResultSetHandler interface, and then rewrite it The handle () method realizes the processing of the result set
10.
The method used to set the input parameters of the string type in the PreparedStatement interface is ().

A. setInt method
B. setString method
C. executeUpdate method
D. execute method
reference answer :
B
answer explanation :
setInt(int parameterIndex, int x) set the specified parameter to a given int value; setString(int parameterIndex, String x) Set the specified parameter to the given String value; executeUpdate() executes the SQL statement in this PreparedStatement object; executeQuery() executes the SQL query in this PreparedStatement object, and this method returns a ResultSet object.
11.
Among the following options, the method used to return the result set pointer to the ResultSet class before the first record is ( ).

A, first()
B, last()
C, beforeFirst()
D, next()
Reference answer :
C
Answer description :
last() moves the cursor to the last row of this ResultSet object; beforeFirst() moves the cursor to this ResultSet The beginning of the object, that is, before the first row; next() moves the cursor down one row from the current position.
12.
Among the methods of the Statement interface below, () is used to execute various SQL statements.

A. executeUpdate(String sql)
B. executeQuery(String sql)
C. execute(String sql)
D. executeDelete(String sql)
Reference answer :
C
Answer explanation :
execute(String sql) is used to execute various SQL statements, this method Returns a value of type boolean. If it is true, it means that the executed SQL statement has query results, and the query results can be obtained through the getResultSet() method of Statement. 13. In the Statement interface, the given SQL
statement
can be executed and may return multiple The method of the result is ( )

A. execute method
B. executeQuery method
C. executeUpdate method
D. getMaxRows method
reference answer :
B
answer explanation :
execute(String sql) is used to execute various SQL statements, this method returns a boolean type value, if it is true, it means The executed SQL statement has query results; executeUpdate(String sql) is used to execute insert, update and delete statements in SQL. This method returns a value of type int, indicating the number of records affected by the SQL statement in the database; executeQuery(String sql) is used to execute the select statement in SQL, and this method returns a ResultSet object representing the query result.
14.
Among the following options, the one that can execute SQL statements with parameter placeholders is ().

A, Statement
B, Connection
C, PreparedStatement
D, ResultSet
reference answer :
C
Answer explanation :
Connection interface represents the connection between Java program and database; Statement interface is used to execute static SQL statement and return a result object; PreparedStatement interface extends the For the execution of SQL statements with parameters, the SQL statement in this interface can use the placeholder "?" to replace the parameters, and then use the setter() method to assign values ​​to the parameters of the SQL statement; the result set.
15.
Regarding the next() method in the ResultSet interface, the correct statement is ( ).

A. Move the cursor to the previous row of the ResultSet object
B. Move the cursor to the beginning of the ResultSet object
C. Move the cursor to the end of the ResultSet object
D. Move the cursor down one row from the current position
Reference answer :
D
Answer Explanation :
None
16.
Among the following options, the method to move the cursor down one line from the current position is ().

A. next()
B. absolute(introw)
C. previous()
D. last()
Reference answer :
A
Answer description :
next() moves the cursor down one line from the current position; absolute(int row) moves the cursor to The specified row of this ResultSet object; previous() moves the cursor to the previous row of this ResultSet object; last() moves the cursor to the last row of this ResultSet object.
17.
Among the following statements to realize database connection, the one used to specify the JDBC driver is ().

A. Class.forName("com.mysql.cj.jdbc.Driver");
B. String strConn="jdbc:sqlserver://LIUZC:1433;DatabaseName=ShopSystem";
C. String strUser="sa";
D 、conn=DriverManager.getConnection(strConn,strUser,strPassword);
Reference answer :
A
Answer explanation :
Item A: com.mysql.cj.jdbc.Driver is the specified driver B item: database connection address Item C: database name item D : Get the database connection through DriverManager
18.
The method used to create a Statement object in the Connection interface to call the database query is ( ).

A. createStatement()
B. prepareCall()
C. prepareStatement()
D. rollback()
Reference answer :
A
Answer description :
createStatement() is used to create a Statement object and send the SQL statement to the database prepareStatement(String sql) is used Create a PreparedStatement object and send the parameterized SQL statement to the database prepareCall(String sql) is used to create a CallableStatement object to call the database stored procedure
19.
In the ResultSet interface, the method that can directly move the pointer to the nth record is ( ).

A. absolute()
B. previous()
C. beforeFirst()
D. next()
Reference answer :
A
Answer description :
absolute(int row) moves the cursor to the specified row of the ResultSet object; previous() moves the cursor to The previous row of this ResultSet object; beforeFirst() moves the cursor to the beginning of this ResultSet object, before the first row; next() moves the cursor down one row from the current position.
20.
The JDBC class used to send simple SQL statements and implement SQL statement execution is ( ).

A, Connection class
B, Statement class
C, DriverManager class
D, ResultSet class
reference answer :
B
Answer explanation :
Connection interface represents the connection between Java program and database; Statement interface is used to execute static SQL statement and return a result object; DriverManager The class is used to load the JDBC driver and create a connection with the database; the ResultSet interface is used to save the result set returned when JDBC executes the query.
21.
Use the DBUtils tool class to query all the records in the student table. The class that can realize the encapsulation operation is ( ).

A. BeanHandler
B. BeanListHandler
C. ColumnListHandler
D. ScalarHandler
Reference answer :
B
Answer explanation :
BeanListHandler: encapsulate each row of data in the result set into a corresponding JavaBean instance and store it in the List. Querying all records in the student table should return multiple records, and the result set should be encapsulated into a collection. So choose B
22.
The following options, the method used by the DBCP database connection pool to set the number of connection pool initialization connections is ( ).

A. setInitialSize(int initialSize)
B. setMinIdle(int minIdle)
C. setMinPoolSize()
D. setInitialPoolSize()
reference answer :
A
Answer description :
setInitialSize(int initialSize): DBPC sets the number of connections initialized by the database connection pool; setMinIdle(int minIdle): DBPC sets the minimum number of idle connections in the database connection pool; setMinPoolSize(): C3P0 sets the minimum number of connections in the database connection pool; setInitialPoolSize(): C3P0 sets the number of connections initialized by the database connection pool.
23.
Regarding the difference between the getConnection() method in the DriverManager and DataSource interfaces, the correct explanation is ().

A. The former needs close(), but the latter does not need close()
B. The former's close() is to release the Connection, while the latter's close() will only return the Connection to the connection pool.
C. There is no difference between the two
. D. DataSource getConnection() will create a new database link every time
Reference answer :
B
Answer explanation :
The former is to close the connection and release the database resources; the latter is to recycle the Connection after use, and put it back into the connection pool for other thread usage.
24.
The return type of the executeQuery method defined in the interface Statement is ().

A, ResultSet
B, int
C, boolean
D, no return value
Reference answer :
A
Answer description :
executeQuery(String sql) is used to execute the select statement in SQL, and this method returns a ResultSet object representing the query result.
25.
It is known that there is a QueryRunner object runner, SQL statement: String sql = “select * from user”; In the following operation, the option to query all records is ().

A. List list=runner.query(sql, new BeanHandler(User.class));
B. List list=runner.query(sql, new BeanListHandler(User.class));Object num =runner.delete(1, sql );
C. Object list=runner.query(sql, new BeanHandler(User.class));
D. List list=runner.query(sql, new BeanHandler(User.class));
Reference answer :
B
Answer description :
select * The result of from user query is generally multiple records, and the result set should be encapsulated into a collection. It is more appropriate to use BeanListHandler.
26.
In the API provided by the DBUtils toolkit, ( ) is used to execute SQL statements.

A, PreparedStatement
B, DBUtils
C, QueryRunner
D, ResultSetHandler
reference answer :
C
Answer explanation :
QueryRunner class simplifies the code for executing SQL statements, and it can complete most of the database operations in cooperation with ResultSetHandler, greatly reducing the amount of coding.
27.
Among the following options, the query() method belonging to the QueryRunner class can execute the operation statement ().

A. SELECT statement
B. DELETE statement
C. UPDATE statement
D. INSERT statement
Reference answer :
A
Answer description :
The SQL statement that can be executed by the query() method of the QueryRunner class is a query operation, that is, the SELECT statement
28.
The following options are responsible for management The class of the JDBC driver is ().

A, Connection class
B, Statement class
C, DriverManager class
D, ResultSet class
Reference answer :
C
Answer explanation :
Connection interface represents the connection between Java program and database; Statement interface is used to execute static SQL statement and return a result object; DriverManager The class is used to load the JDBC driver and create a connection with the database; the ResultSet interface is used to save the result set returned when JDBC executes the query.
29.
Among the following options, the method used to load and register the JDBC driver in the DBUtils class is ().

A, close () method
B, closeQuietly () method
C, commitAndCloseQuietly () method
D, loadDriver () method
reference answer :
D
answer description :
close () method: according to different parameters, you can close the connection or statement or result set. closeQuietly() method: Depending on the parameters, the connection or statement or result set can be closed, and some SQL exceptions thrown in the program can be hidden. commitAndCloseQuietly() method: close the connection after submitting the connection, and hide some SQL exceptions thrown in the program. loadDriver() method: load and register the JDBC driver, and return true if successful.
30.
Regarding the executeQuery(String sql) method, the correct statement is ().

A. Can execute insert statement
B. Can execute update statement
C. Can execute select statement
D. Can execute delete statement
Reference answer :
C
Answer description :
executeQuery() executes SQL query, and this method returns a ResultSet object.
31.
The following description about JDBC is wrong ().

A. JDBC is a Java API for executing SQL statements.
B. The JDBC API supports both a two-tier model of database access and a three-tier model.
C. JDBC consists of a set of classes and interfaces written in the Java programming language.
D. Use JDBC can only connect to SQL Server database
Reference answer :
D
Answer explanation :
JDBC is oriented to many relational databases such as MySQL and Oracle.
32.
For the following options, the DataSource interface is used to obtain the database connection ().

A, Delegate
B, Factory
C, Connection
D, getConnection
reference answer :
D
Answer description :
DataSource interface is responsible for establishing a connection with the database, and defines the method that returns a Connection object, Connection getConnection() and Connection getConnection(String username, String password).
33.
It is known that there is a QueryRunner object runner, SQL statement: String sql = "select * from user where id=?"; in the following operations, the option to query the specified record is ().

A. User user=(User)runner.query(sql, new BeanListHandler(User.class), new Object[]{id});
B. User user=(User)runner.query(sql, new BeanHandler(User. class));
C. User user=(User)runner.query(sql, new BeanListHandler(User.class), id);
D. User user=(User)runner.query(sql, new BeanHandler(User.class) , new Object[]{id});
Reference answer :
D
Answer description :
BeanHandler encapsulates the first row of data in the result set into a corresponding JavaBean instance, excluding options A and C; specifying query conditions requires the use of parameters Object[] params, option B is excluded, so the answer is D.
34.
Regarding the executeQuery(String sql) method, the correct statement is ( ).

A. Can execute insert statement
B. Can execute update statement
C. Can execute select statement
D. Can execute delete statement
Reference answer :
C
Answer description :
executeQuery(String sql) is used to execute the select statement in SQL, and this method returns a representation The ResultSet object of the query result
35.
If you assign a value to the third question mark in the following precompiled SQL, then the correct option is ( ). UPDATE emp SET ename=?,job=?,salary=? WHERE empno=?

A. pst.setInt("3",2000);
B. pst.setInt(3,2000);
C. pst.setFloat("salary",2000);
D. pst.setString("salary", "2000" );
Reference answer :
B
Answer explanation :
setInt(int parameterIndex, int x) Set the specified parameter to the given int value
36.
In the implementation class of the ResultSetHandler interface, it is used to encapsulate a column of data in the result set ( ).

A. ColumnListHandler
B. MapHandler
C. ArrayHandler
D. BeanHandler
Reference answer :
A
Answer description :
ColumnListHandler is used to encapsulate the value of a column attribute into a List collection.
37.
Known SQL statement: select count(*) from customer; In the Handler below, the most suitable one for converting the data in the result set is ().

A. BeanListHandler
B. ArrayHandler
C. BeanMapHandler
D. ScalarHandler
Reference answer :
D
Answer explanation :
select count(*) from table returns a number, and ScalarHandler encapsulates a single column data, and can also encapsulate count, avg, max, min, The execution results of aggregate functions such as sum.

multiple choice

38.
Among the following options, the update() method belonging to the QueryRunner class can execute the operation statement ( ).

A. SELECT statement
B. DELETE statement
C. UPDATE statement
D. INSERT statement
Reference answer :
B, C, D
Answer description :
The update() method of the QueryRunner class can perform an update (insert, delete, update) operation
39.
For ResultSetHandler The description of the following implementation class is correct ().

A. BeanHandler: encapsulate the first row of data in the result set into a corresponding JavaBean instance.
B. BeanListHandler: Put the field value of each piece of data into a JavaBean, and then put all the JavaBean into the List collection.
C. ColumnListHandler: Put all the data of the specified column into a List collection.
D. ScalarHandler: Store a column of data in a record in the result set as an Object object.
Reference answer :
A, B, C, D
Answer explanation :
ResultSetHandler provides several common implementation classes: BeanHandler: encapsulates the first row of data in the result set into a corresponding JavaBean instance. BeanListHandler: Encapsulate each row of data in the result set into a corresponding JavaBean instance and store it in the List. ColumnListHandler: Encapsulates the value of a column attribute into the List collection. ScalarHandler: Store a column of data in a record in the result set as an Object object.
40.
Among the following options, there are ( ) ways for the C3P0 database connection pool to create data source objects.

A. Through the BasicDataSource class
B. Through the ComboPooledDataSource() construction method
C. By reading the configuration file
D. Through the ComboPooledDataSource(String configName) construction method
Reference answer :
B, D
Answer explanation :
When using the C3P0 database connection pool, you first need to create data Source object. Create a data source object by calling the constructor of the ComboPooledDataSource class. The ComboPooledDataSource class has two construction methods, namely ComboPooledDataSource() and ComboPooledDataSource(String configName).
41.
Among the following options, there are ( ) about the methods that can be used to close the data connection in the DBUtils class.

A, close () method
B, closeQuietly () method
C, commitAndCloseQuietly () method
D, loadDriver () method
reference answer :
A, B, C
answer description :
loadDriver () method is used to load and register the JDBC driver
42,
the following Regarding the construction method of the QueryRunner class, the description is correct ( ).

A. You can use the default construction method to construct
B. You cannot directly construct
C. Use the javax.sql.DataSource DataSource as a parameter construction method
D. You can use the QueryRunnerFactory factory class to construct
reference answers :
A, C
Answer description :
You can use The default construction method to construct and use javax.sql.DataSource DataSource as a parameter construction method
43.
The SQL statements that can be executed by the update(Connection conn, String sql, ResultSetHandler rsh) method of the QueryRunner class are ().

A. select
B. update
C. insert
D. delete
Reference answer :
B, C, D
Answer description :
The update(Connection conn, String sql, ResultSetHandler rsh) method of the QueryRunner class can perform an update (insert, delete, update) operation
44.
After executing the following SQL statement SELECT name, rank, serialNo FROM employee in JDBC programming, the code to get the first column of data in the result set is ( ).

A. resultSet.getString(0);
B. resultSet.getString("name");
C. resultSet.getString(1);
D. resultSet.getString("ename");
Reference answer :
B, C
Answer description :
ResultSet In the common methods of the interface, the specified data is obtained through the index of the field, and the index of the field is numbered from 1; getString(int columnIndex) is used to obtain the value of the String type of the specified field, and the parameter columnIndex represents the index of the field; getString( String columnName) is used to obtain the value of the String type of the specified field, and the parameter columnName represents the name of the field

True or False

45.
The full name of JDBC is Java Database Connectivity, which is Java database connection. It is a set of Java APIs for executing SQL statements.

Reference answer : Explanation
for:None

fill in the blank

46.
​​PreparedStatement is a subinterface of Statement, which is used to execute
precompiled
SQL statements.

Reference answer :
[Precompile]
Answer explanation :
Statement provides a subclass PreparedStatement. The PreparedStatement object can precompile SQL statements, and the precompiled information will be stored in the PreparedStatement object.
47.updateand delete statements
in SQL

Reference answer :
[update]
Answer description :
The executeUpdate() method is used to execute insert, update and delete statements in SQL. This method returns a value of type int, indicating the number of records in the database affected by the SQL statement.
48.
The Driver
interface is an interface that all JDBC drivers must implement. This interface is specially provided for database manufacturers.

Reference answer :
【Driver】
Answer description :
No
49.
Among the DBUtils tools, the class used to provide routine operations such as closing connections and loading JDBC drivers is
DBUtils
.

Reference answer :
【DBUtils】
Answer explanation :
The DBUtils class mainly provides methods such as loading JDBC drivers and closing resources. The methods in the DBUtils class are generally static methods and can be called directly using the class name.
50.
The executeQuery(String sql) method of the Statement interface is used to execute the SQL
select
statement.

Reference answer :
[select]
Answer explanation :
no
51.
The full Chinese name of DBCP is
database connection pool
.

Reference answer :
[Database Connection Pool]
Answer explanation :
DBCP is the Database Connection Pool (DataBase Connection Pool), which is an open source connection pool implementation under the Apache organization, and is also a connection pool component used by the Tomcat server.
52.
JDBC
is Java Database Connectivity, which is Java database connection. It is a set of Java APIs for executing SQL statements.

Reference answer :
[JDBC]
Answer explanation :
The full name of JDBC is Java Database Connectivity, which is Java database connection. It is a set of Java APIs for executing SQL statements.
53.
DBUtils core class library mainly includes
DBUtils
class, QueryRunner class and ResultSetHandler interface.

Reference answer :
【DBUtils】
Answer explanation :
DBUtils core class library mainly includes DBUtils class, QueryRunner class and ResultSetHandler interface. The DBUtils tool mainly performs all operations of JDBC through these three core APIs.
54.
To customize the class that implements the ResultSetHandler interface, as long as
the handle()
method is rewritten, the result set processing can be realized.

Reference answer :
[handle()]
Answer explanation :
The ResultSetHandler interface provides a separate method handle (java.sql.ResultSet rs). If the above implementation class does not provide the desired function, you can customize a class that implements the ResultSetHandler interface. Then rewrite the handle() method to realize the processing of the result set.
55.
In the ResultSet interface, the method that can move the cursor to the previous row of the ResultSet object is
previous()
.

Reference answer :
【previous()】
Answer description :
None
56.
The ResultSetHandler interface is used to process
the ResultSet
result set, and it can convert the data in the result set into different forms.

Reference answer :
[ResultSet]
Answer explanation :
None
57.
The database connection pool is responsible for allocating, managing and
releasing
database connections, which allows applications to reuse existing database connections instead of re-establishing them.

Reference answer :
[Release]
Answer description :
None
58.["prepareStatement(String sql)"]
of the Connection interfaceis used to create a PreparedStatement object to send parameterized SQL statements to the database.

Reference answer :prepareStatement
()Answer
description :
NoneCreate a data source object.



Reference answer :
[Configuration file]
Answer description :
There are two ways to create a data source object, as follows. 1. Create data source objects directly through the BasicDataSource class; 2. Create a data source object by reading the configuration file
60.
In the JDBC common API,
the DriverManager
class is used to load the JDBC driver and create a connection with the database.

Reference answer :
【DriverManager】
Answer description :
None
61.
A class that implements the javax.sql.DataSource interface is called
a data source
.

Reference answer :
【Data source】
Answer explanation :
It is customary to call a class that implements the javax.sql.DataSource interface a data source. As the name implies, a data source is the source of data. Every time a database connection is created, the database connection information will be stored in the data source.
62.
In the ResultSet interface, the method that can move the cursor to the last row of the ResultSet object is
afterLast()
.

Reference answer :
【last()】
Answer description :
None

Guess you like

Origin blog.csdn.net/weixin_48053866/article/details/126256833