In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how the Spring JDBC template is, the quality of the article content is high, so Xiaobian shares it for everyone to make a reference, I hope you have a certain understanding of related knowledge after reading this article.
The JDBC abstract framework provided by Spring consists of four different packages:
The core package contains JdbcTemplate. This class is one of the base classes-provided and used by the Spring Framework's JDBC support.
Data source packages are an important part of implementing unit test database access code. Its DriverManagerDataSource can be used in a way similar to what you are used to in JDBC: just create a new DriverManagerDataSource and call the setter method to set DriverClassName, Url, Username and Password.
Object packages contain classes that describe RDBMS queries, changes, and stored procedures as thread-safe, reusable objects.
Support Pack-You can find SQLException translation functionality and some utility classes here.
1)Spring JDBC Template Design Pattern
Spring JDBC implements the template design pattern, which means that parts of complex tasks that are repeated in code are implemented in template classes. This approach simplifies the use of JDBC because it handles the creation and release of resources. This helps avoid common mistakes such as forgetting to close connections. It performs core JDBC workflow tasks, such as statement creation and execution, and lets application code provide SQL and extract results.
2)Spring JDBC exception handling
The Spring Framework specifically highlights problems faced in traditional JDBC programming related to the following scenarios:
Spring provides an abstract exception layer that moves lengthy and error-prone exception handling from application code to the framework. The framework takes care of all exception handling; application code can focus on extracting results using appropriate SQL.
Spring provides an important exception class hierarchy so that you can use the appropriate SQLException subclass in your application code.
With an abstract exception layer, we successfully achieve database independence without changing exception handling. For example, if you change your database from PostgreSQL to Oracle, you don't have to change exception handling from OracleDataException to PostgresDataException. Spring is able to catch application server-specific exceptions and throw a Spring data exception.
When handling exceptions, Spring checks metadata availability from a database connection to determine database artifacts. It uses this knowledge to map SQLExceptions to specific exceptions in its own exception hierarchy. Therefore, we don't need to worry about specialized SQL status or error code issues;Spring's data access exceptions are not JDBC specific, so your DAO doesn't have to be bound to JDBC (because of the exceptions it might throw).
IV.Spring JDBC template example
In the following two lists, we will use the business logic implemented earlier with traditional JDBC as an example and show how easy it is to use the Spring JDBC version. First, we start with a simple interface.
Interface:
importjava.util.List; publicinterfaceTasksDAO{ publicListgetTasksNames(); }
Interface implementation class:
importjava.sql.ResultSet; importjava.sql.SQLException; importjava.util.Iterator; importjava.util.List; importjavax.sql.DataSource; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; importorg.springframework.jdbc.core.JdbcTemplate; importorg.springframework.jdbc.core.RowMapper; importorg.springframework.jdbc.core.support.JdbcDaoSupport; /** *wangzyspringjdbcTemplate *@authorAdministrator * */ publicclassTasksJdbcDAOextendsJdbcDaoSupportimplementsTasksDAO { publicListgetTasksNames(){ JdbcTemplatejt=getJdbcTemplate();//Get Spring JDBC template returnjt.query ("selectUSERNAME,PASSWORDfromuserinfo",newTasksRowMapper()); } //Inherited sping, encapsulated interface RowMapper classTasksRowMapperimplementsRowMapper{ publicObjectmapRow (ResultSetrs,intindex)throwsSQLException{ returnrs.getString(1);}//get column number} publicstaticvoidmain (String[]args)throwsException{ ApplicationContextctx=newClassPathXmlApplicationContext ("applicationContext.xml");//Get sping configuration file address DataSources =(DataSource)ctx.getBean("dataSourceDBDirect");//Get data source TasksJdbcDAOtaskDao=newTasksJdbcDAO(); taskDao.setDataSource(ds); IteratortskIter=taskDao.getTasksNames().iterator();//Extract data, loop while(tskIter.hasNext()){ System.out.println(tskIter.next ().toString()); } } }
appliactiong: (under src)
Property> property> bean> About Spring JDBC template is how to share here, I hope the above content can have some help for everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
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.