Get the App
SLTechnology News&Howtos  ›  Database  › 

Some understanding of JDBC

Shulou Source: shulou.com Published: 2022-06-01 11:40:40 10月03日 Update

JDBC is a kind of java api that executes SQL statements. It is a bridge for java to access databases. It can provide a unified access interface for a variety of relational databases.

The six steps to implementing JDBC, which is to connect to the oracle:

1. Register the Driver interface (load a concrete Driver implementation class):

Class.forName ("oracle.jdbc.OracleDriver")

two。 Get database connection interface

String url = "jdbcracle:thin@127.0.0.1:1521:Xe"

String user = "username"

String password = "password"

Connection conn = DriverManager.getConnection (url,user,pass)

3. Create a Statement interface-- it can be understood as a tool for transmitting data and commands

Statement stm = conn.createStatement ()

4. Execute the sql command

5. Processing result set ResultSet

ResultSet rs = stm.executeQuery (sql)

6. Release resources and shut down the interface

The order of shutting down the interfaces here is reverse shutdown, that is, shutting down the interfaces in the order of rs,stm,conn.

In practical development, the subinterface of Statement interface, that is, PreparedStatement interface, is generally used to deal with sql statements. The reason for this is that, on the one hand, when executing similar add, delete and modify statements, PreparedStatement sends the given semi-finished sql statement to db sv for pre-compilation, and then the execute method sends the statement directly to be executed, which is much more efficient than Statement;. In terms of security, PreparedStatement interface has higher security than the former, a placeholder has only one field, while the sql statement in Statement has a big security loophole.

In the three-tier development model, ThreadLocal technology is generally used to encapsulate a JdbcUtil class to obtain the interface and close the interface, obtain the interface in the Dao layer, execute the add, delete, change and query statements, carry on the business logic processing in the service layer, manually control the submission of the transaction and finally close the interface.

Personally, I think that learning Jdbc well is the basis of learning Hibernate in the future, and it is also one of the prerequisites for skillfully mastering the Hibernate framework. Write your own experience, and please correct if there are any deficiencies.

Tags: Interface statement data security database processing command sequence development large skilled similar party business personal transaction premise semi-finished product reason post-learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Huawei vpn MariaDB MySQL