JDBC and JDBC template

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[JDBC and JDBC template]

. Background introduction

JDBC and JDBC template are programming languages that we often use to operate databases. Today, let's learn about JDBC and JDBC template

What are the differences and how to operate the database.

II Knowledge analysis

What is JDBC?

JDBC (Java database connectivity) is a Java API for executing SQL statements,

It can provide unified access to multiple relational databases. It consists of a set of classes and interfaces written in Java language.

What is a JDBC template

Compared with JDBC, JDBC template has one more template in its name, and this template is the template, which is provided to us by the spring framework,

Therefore, the JDBC template is spring's encapsulation of JDBC. Generally speaking, it is spring's encapsulation of JDBC.

III common problem

How does JDBC operate on the database?

How does the JDBC template operate on the database?

Implementation principle of JDBC template

Advantages and disadvantages of JDBC template and JDBC

IV Solution

The principle of JDBC is to obtain the connection to the database through the connection class,

Then process the SQL statement through the Preparedstatement class,

Then pass it The setobject method passes in data,

Finally, the method is adopted Executeupdate() and ExecuteQuery () performs updates, which is the basic principle of JDBC

JDBC template saves us a lot of trouble

Specify database connection parameters

Open database connection

Precompile and execute SQL statements

Traverse the query results (if necessary)

Handle any exceptions thrown

Handle business

Close database connection

This has something to do with spring

The spring IOC container manages the data sources that manage database connections as ordinary Java beans,

Then inject the data source into the encapsulation class jdbctemplate,

The datasource property of the jdbctemplate is the data source of the injection configuration

JDBC

Advantages: operation period: fast and efficient

Disadvantages: Editor: large amount of code, cumbersome exception handling, and does not support database cross platform

JDBCTemplate

Advantages: Runtime: efficient, embedded in spring framework, supporting AOP based declarative transactions

Disadvantages: it must be used together with the spring framework, does not support database cross platform, and there is no cache by default

V Coding practice

Vi Extended thinking

1. Why use class in JDBC forName(jdbcName)

Class. Forname ("com. Mysql. JDBC. Driver") actually calls the registerdriver method of drivermanager to register a JDBC Driver of MySQL

Class. Function of forname (string classname)

The first is the name specified under classpath The class file is loaded into the memory of the Java virtual machine.

The second is to initialize this class.

2. Why delete class Forname ("com. Mysql. JDBC. Driver") can still run?

January 23, 1996 jdk1 0 release, the Java language has the first official version of the running environment. JDBC is on February 19, 1997, in jdk1 1. It can be seen from the version that JDBC belongs to some of the most basic function points of Java technology. That's at jdk1 After 5, there is no need to explicitly call class Forname ("com. Mysql. JDBC. Driver"), the drivermanager will automatically load the appropriate driver, but the prerequisite is that there must be a driver jar package under classpath.

3. Why use placeholder "?"

Beautify code and prevent SQL injection attacks ("'or' 1 '=' 1")

VII reference

https://blog.csdn.net/sx_ygl/article/details/54018072

https://www.cnblogs.com/xrq730/p/4851648.html

VIII More discussion

1. Question: how to control the opening and closing of the database

Answer: class forName("com.MysqL.jdbc.Driver"); The initialization driver opens the database link

Connection c = DriverManager.// Connect to the database,

//Close data link

ps.close();

c.close();

The opening and closing of JDBC template is encapsulated in JDBC template

2. Question: is there any difference in efficiency between JDBC and JDBC template

Answer: in terms of efficiency alone, JDBC is actually a little more efficient than JDBC template JDBC template is more convenient for us to change the content and understand the logic

3. Question: if the database connection resources are occupied, what are the solutions

Answer: modify my. In the MySQL installation directory Ini or my Max in CNF file_ user_ The value of the connections parameter, restart the MySQL server.

Thank you, elder martial brother. This tutorial is based on their previous technology sharing.

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>