In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the knowledge points of the Spring JDBC framework". In the daily operation, I believe that many people have doubts about the knowledge points of the Spring JDBC framework. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the knowledge points of the Spring JDBC framework?" Next, please follow the editor to study!
Overview of JDBC Framework
When using an ordinary JDBC database, it is troublesome to write unnecessary code to handle exceptions, open and close database connections, and so on. But the Spring JDBC framework takes care of all the low-level details, from opening the connection, preparing and executing SQL statements, handling exceptions, handling transactions, and finally closing the connection.
So when getting data from the database, all you do is define the connection parameters, specify the SQL statements to be executed, and do the required work each iteration.
Spring JDBC provides several methods and corresponding different classes and interfaces in the database. I'll show you the classic and most popular way to use the JdbcTemplate class framework. This is the central framework class that manages all database communication and exception handling.
JdbcTemplate class
The JdbcTemplate class executes SQL queries, update statements, and stored procedure calls, performs iterative result sets, and extracts return parameter values. It also catches JDBC exceptions and converts them to generic classes, more information, and exception hierarchies defined in the org.springframework.dao package.
An instance of the JdbcTemplate class is thread-safe configured. So you can configure a single instance of JdbcTemplate and then safely inject this shared reference into multiple DAOs.
A common practice when using the JdbcTemplate class is to configure the data source in your Spring configuration file, then share the data source bean dependency injection into the DAO class, and create a JdbcTemplate in the data source's set function.
Configure the data source
We create a database table Student in the database TEST. Suppose you are using MySQL databases, and if you use other databases, you can change your DDL and corresponding SQL queries.
CREATE TABLE Student (ID INT NOT NULL AUTO_INCREMENT, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, PRIMARY KEY (ID))
Now, we need to provide a data source into the JdbcTemplate, so it can configure itself to get database access. You can configure the data source in the XML file, and one of the code is as follows:
Data access object (DAO)
DAO represents the commonly used data access objects for database interaction. DAOs provides a way to read and write data to a database, and they should display this functionality through an interface, and the rest of the application will access them.
In Spring, data access objects (DAO) make it easy to use data access technologies, such as JDBC, Hibernate, JPA, or JDO, in a unified way.
Execute SQL statement
Let's look at how to use SQL and jdbcTemplate objects to perform CRUD (create, read, update, and delete) operations in database tables.
Query an integer type:
String SQL = "select count (*) from Student"; int rowCount = jdbcTemplateObject.queryForInt (SQL)
Query a long type:
String SQL = "select count (*) from Student"; long rowCount = jdbcTemplateObject.queryForLong (SQL)
A simple query that uses bound variables:
String SQL = "select age from Student where id =?"; int age = jdbcTemplateObject.queryForInt (SQL, new Object [] {10})
Query string:
String SQL = "select name from Student where id =?"; String name = jdbcTemplateObject.queryForObject (SQL, new Object [] {10}, String.class)
Query and return an object:
String SQL = "select * from Student where id =?"; Student student = jdbcTemplateObject.queryForObject (SQL, new Object [] {10}, new StudentMapper ()); public class StudentMapper implements RowMapper {public Student mapRow (ResultSet rs, int rowNum) throws SQLException {Student student = new Student (); student.setID (rs.getInt ("id")); student.setName (rs.getString ("name")); student.setAge (rs.getInt ("age")) Return student;}}
Query and return multiple objects:
String SQL = "select * from Student"; List students = jdbcTemplateObject.query (SQL, new StudentMapper ()); public class StudentMapper implements RowMapper {public Student mapRow (ResultSet rs, int rowNum) throws SQLException {Student student = new Student (); student.setID (rs.getInt ("id")); student.setName (rs.getString ("name")); student.setAge (rs.getInt ("age")); return student;}}
Insert a row in the table:
String SQL = "insert into Student (name, age) values"; jdbcTemplateObject.update (SQL, new Object [] {"Zara", 11})
Update a row in the table:
String SQL = "update Student set name =? where id =?"; jdbcTemplateObject.update (SQL, new Object [] {"Zara", 10})
Delete a row from the table:
String SQL = "delete Student where id =?"; jdbcTemplateObject.update (SQL, new Object [] {20}); execute DDL statement
You can use execute (..) in jdbcTemplate. Method to execute any SQL statement or DDL statement. The following is an example of creating a table using the CREATE statement:
String SQL = "CREATE TABLE Student (" + "ID INT NOT NULL AUTO_INCREMENT," + "NAME VARCHAR (20) NOT NULL," + "AGE INT NOT NULL," + "PRIMARY KEY (ID));" jdbcTemplateObject.execute (SQL); at this point, the study of "what are the knowledge points of the Spring JDBC framework" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.