JDBC and JdbcTemplate differences

Source: Internet
Author: User
Tags one table

Article from 78478571

1. Background information

Java programmers in the future work is very important to deal with the database, but the code and database is two irrelevant.

How do you use code to manipulate a database and use JDBC at this time?

2. Knowledge Analysis

What is JDBC?

①JDBC (Java Database Connectivity,java connection) is a Java API for executing SQL statements.

You can provide unified access to a variety of relational databases, consisting of a set of classes and interfaces written in the Java language.

And many of this template, is templates, is the spring framework for us to provide.

So JdbcTemplate is spring's encapsulation of JDBC, and the popular point is that spring's template for JDBC encapsulation

3. Frequently Asked Questions

What does JDBC do?

What did JdbcTemplate do?

How did JdbcTemplate do that?

4. Solution

The principle of JDBC is to get the database connection through the connection class.

The SQL statement is then processed through the PreparedStatement class, and then the data is passed in through its. SetObject method.

Finally, the update is performed by means of executeupdate () and. ExecuteQuery (), which is the basic principle of JDBC.

JdbcTemplate Help us save a lot of trouble.

② Specifies the database connection parameters.

Open the database connection.

Precompile and executes SQL statements.

Traverse the query results, if necessary.

Handles any exceptions that are thrown.

Process transactions.

To close a database connection

So jdbctemplate how to do it, it's related to spring.

The SPRINGIOC container manages the data source that manages the database connection as if it were a normal Java bean.

The data source is then injected into the encapsulated class JdbcTemplate:

The DataSource attribute of the JdbcTemplate is the data source of the injected configuration

5. Code combat 6. Expand Thinking

What is the difference between jdbctemplate and JDBC?

JDBC requires a database connection every time, then processes the SQL statements, passes the values, and shuts down the database.

There may even be times when a database forget to close causes the connection to be occupied.

In the future work, the customer's needs are certainly not static, which leads to changes in the database content often.

By JdbcTemplate We just need to change the part that needs to be changed, and we don't need to make global modifications.

Spring will do all the JDBC bottom-level detail work for us.

7. References

① Baidu

②http://www.cnblogs.com/binyue/p/4037628.html

8. More discussion

(1) What are the benefits of using jdbc.properties?

If we need to change the configuration of the connection database, we can change it once in jdbc.properties. And the JDBC words need to be deleted and changed. Each configuration of the connection database needs to be changed.

(2) If a database connection resource is actually in use, what is the workaround

Restart the MySQL server by modifying the value of the Max_user_connections parameter in the My.ini or my.cnf file in the MySQL installation directory.

(3) Under what circumstances are JdbcTemplate and mybatis used, JDBC template and Spring+mybatis? , what are the pros and cons compared to MyBatis?

These three questions are in fact similar. JdbcTemplate and MyBatis are not the same, MyBatis SQL statements are placed in the Mapper.xml file. The equivalent is configured in the environment, ready to use. And JdbcTemplate is written in the implementation of the class, the use of the time to re-invoke the configuration and so on. It is possible to operate on more than one table in the future, and it is very convenient to use the configuration in the environment.

(4) How to control the open shutdown of the database.

Class.forName ("Com.mysql.jdbc.Driver"); This initialization drive is where the database link is opened

Connection c = drivermanager.//Connection database,

Close Data Link

Ps.close ();

C.close ();

The JdbcTemplate is encapsulated in the JdbcTemplate.

(5) Is there a difference in efficiency between JDBC and jdbctemplate?

In terms of efficiency alone, JDBC is actually a little more efficient than jdbctemplate. JdbcTemplate makes it easier for us to make changes and understand the logic.

(6) JdbcTemplate implementation executes multiple SQL statements.

A batch processing statement that uses MySQL directly.

Another kind of splicing SQL statement to submit how many pieces of data directly.

(7) What are the shortcomings of JdbcTemplate

Must be used in conjunction with the spring framework, does not support cross-platform databases, and does not have a cache by default.

JDBC and JdbcTemplate differences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.