The jdbcTemplate operations of the Spring framework for the dao layer include crud and jdbctemplatecrud.

Source: Internet
Author: User

The jdbcTemplate operations of the Spring framework for the dao layer include crud and jdbctemplatecrud.

Crud refers to the basic operations on the database or persistent layer, including

Create, Retrieve, Update, and Delete)

Spring not only encapsulates JDBC, but also Hibernate, and Ibatis

JdbcTemplate is similar to the small dbutils framework in Java Web.

The encapsulation simplifies the code and requires support for jar packages. jdbcTemplate also requires two jar packages:

Spring-jdbc-4.2.4.RELEASE.jar

Spring-tx-4.2.4.RELEASE.jar

 

There were 10 jar packages that implemented functions such as beans annotation aop. Copy them below

Four core JAR packages in the Spring compressed package

Beans, context, core, and expression

:

Https://pan.baidu.com/s/1qXLHzAW

And the log jar package

Commons-logging and log4j

:

Https://pan.baidu.com/s/1mimTW5i

Add another

Spring-aop-5.0.1.RELEASE.jar (for annotation, included in the Spring-framework Library)

Add more

Spring-aspects-5.0.1.RELEASE.jar(Included in the Spring-framework Library)

Aspectjweaver-1.8.12.jar (http://mvnrepository.com/artifact/org.aspectj/aspectjweaver)

Aopalliance-1.0.jar (http://mvnrepository.com/artifact/aopalliance/aopalliance/1.0)

 

A brief review of the original Java database connection operations

Package com. swift; // The imported package is java. SQL. connection instead of com. mysql. jdbc. connectionimport java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; import java. util. arrayList; import java. util. list; public class TestJDBC {public static void main (String [] args) {Connection conn = null; Statement st = null; ResultSet rs = null; try {// 1. Load the driver Class. forName ("com. mysql. jdbc. driver ");} catch (ClassNotFoundException e) {e. printStackTrace ();} try {// 2. Link to the database and use com. mysql. jdbc. the Connection package has an error listing <User> List = new ArrayList <User> (); conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/sw_database? User = root & password = root "); // 3. Create a connection statement st = conn. createStatement (); // 4. Execute an SQL statement to obtain the result set rs1_st.exe cuteQuery ("select * from sw_user"); // 5. Obtain the database field generation object in a loop while (rs. next () {int id = rs. getInt ("id"); String username = rs. getString ("username"); String password = rs. getString ("password"); User user = new User (id, username, password); list. add (user);} // 6. traverse the Object list for (User user: list) {System. out. println (user. toString ());} Catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();} finally {// 7. Close the result set try {if (rs! = Null) {rs. close () ;}} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();} // 7. Close the connection statement try {if (st! = Null) {st. close () ;}} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();} // 7. Close the database connection try {if (conn! = Null) {conn. close () ;}} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}}

 

String url = "jdbc: mysql: // localhost: 3306/sw_database ";
String u = "root ";
String p = "root ";
Conn = DriverManager. getConnection (url, u, p );

You can use three parameters to connect to the database.

If you forget to import the package connecting to the mysql database, the above java. lang. ClassNotFoundException: com. mysql. jdbc. Driver problem will occur.

Mysql-connector-java-5.1.7-bin.jar

Link: https://pan.baidu.com/s/1geBRqqn password: 8jxm

After the code is successfully executed, the result is displayed.

 

 

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.