Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Some understanding of JDBC

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report