JavaJDBC technical interview questions

2. JDBC technology

1. What is JDBC and will it be used in the above?
The full name of JDBC is Java DataBase Connection, which is Java database connection, and we can use it to operate relational databases. The JDBC interface and related classes are in the java.sql and javax.sql packages. We can use it to connect to the database, execute SQL queries, store procedures, and process the returned results.
The JDBC interface enables loose coupling between Java programs and JDBC drivers, making it easier to switch between different databases.

2.What are the basic steps for JDBC to access the database?
a. Load (register) the database driver (to the JVM) b. Establish (obtain) a database connection.
c. Create (obtain) database operation objects.
d. The SQL statement that defines the operation. e. Perform database operations.
f. Obtain and operate the result set.
g. Close the object and recycle database resources (close the result set -> close the database operation object -> close the connection)

3. What is the difference between execute, executeQuery, executeUpdate?
a. The execute(String query) method of Statement is used to execute any SQL query. If the result of the query is a ResultSet, this method returns true. If the result is not a ResultSet, such as an insert or update query, it will return false.
b. The executeQuery(String query) interface of Statement is used to execute select query and return ResultSet. The ResultSet returned will not be null even if no records are queried. We usually use executeQuery to execute query statements, so if an insert or update statement is passed in, it will throw a java.util.SQLException with the error message "executeQuery method can not be used for update".
c. The executeUpdate(String query) method of Statement is used to execute the insert or update/delete (DML) statement.
d. The execute() method should be used only when you are not sure what the statement is, otherwise the executeQuery or executeUpdate method should be used.

4.What is PreparedStatement of JDBC?
The PreparedStatement object represents a precompiled SQL statement. Use the setter method it provides to pass in query variables. Since PreparedStatement is precompiled, the corresponding SQL statement can be executed multiple times efficiently. Since PreparedStatement automatically escapes special characters and avoids SQL injection attacks, it should be used as much as possible.

5. What are the advantages of PreparedStatement over Statement?
a. PreparedStatement helps prevent SQL injection because it automatically escapes special characters. b. PreparedStatement can be used for dynamic query.
c. PreparedStatement executes faster. Especially when you reuse it or execute multiple statements using its concatenated query interface.
d. It is easier to write object-oriented code using the setter method of PreparedStatement, and for Statement, we have to concatenate strings to generate query statements. String concatenation can look ugly and error-prone if there are too many arguments.

6.What is ResultSet of JDBC?
After querying the database, a ResultSet will be returned, which is like a data table of the query result set. The ResultSet object maintains a cursor that points to the current row of data. At the beginning, the cursor points to the first row. If the ResultSet's next() method is called, the cursor will move down one row. If there is no more data, the next() method will return false. It can be used in a for loop to iterate over the dataset.

7. What is the difference between java.util.Date and java.sql.Date?
java.util.Date contains date and time, while java.sql.Date only contains date information without specific time information. If you want to store time information in a database, consider using Timestamp or DateTime fields.

8. Talk about the concept of transactions and the steps to handle transactions in JDBC programming.
A transaction is a series of operations performed as a single logical unit of work, and a logical unit of work must have four properties, called Atomicity, Consistency, Isolation, and Durability
(ACID) properties, in order to be a transaction. The JDBC transaction processing has the following operations:
conn.setAutoComit(false); Set the commit method to manual commit conn.commit() Commit the transaction
conn.rollback(),
rollback Only select one for commit and rollback. The transaction is committed under normal circumstances, and rolled back if an exception occurs.

9. The principle of database connection pool. Why use connection pooling.
Database connection is a critical limited and expensive resource, and the management of database connection can significantly affect the scalability and robustness of the entire application, and affect the performance indicators of the program. The database connection pool is proposed for this problem.
The database connection pool is responsible for allocating, managing and releasing database connections. It allows applications to reuse an existing database connection instead of re-establishing one; release database connections whose idle time exceeds the maximum idle time to avoid problems caused by not releasing database connections. The database connection is missing. This technique can significantly improve the performance of database operations.

The database connection pool will create a certain number of database connections into the connection pool during initialization, and the number of these database connections is set by the minimum number of database connections. Regardless of whether these database connections are used, the connection pool will always be guaranteed to have at least this many connections. The maximum number of database connections in the connection pool limits the maximum number of connections that the connection pool can occupy. When the number of connections requested by the application to the connection pool exceeds the maximum number of connections, these requests will be added to the waiting queue.

10. What are the best practices for JDBC?
a. The database resource is very expensive, and it should be closed as soon as possible when it is used up. JDBC objects such as Connection, Statement, and ResultSet all have a close method, just call it.
b. Develop the habit of explicitly closing ResultSet, Statement, and Connection in the code. If you are using a connection pool, the connection will be put back into the pool after it is used up, but ResultSet and Statement that are not closed will cause resource leakage. .
c. Close the resource in the finally block to ensure that it can be closed normally even if an exception occurs.
d. Try to use PreparedStatement instead of Statement to avoid SQL injection, and at the same time, it can improve the efficiency of execution through precompilation and caching mechanism. e. The higher the database isolation level, the worse the performance. Make sure that the isolation level set by your database connection is optimal.
f. If you create a database connection in a WEB program, it is best to use the JDBC data source through JNDI, so that the connection can be reused.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325020326&siteId=291194637