0% found this document useful (0 votes)
14 views11 pages

P 18

spring
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

P 18

spring
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
0/10/2028 09:07 ‘Spring JdocTemplate Tutorial -javatpoint MailAPI Hibernate Spring Android tips: javatpeint.comispring-Jdbe Tempate-tuloral un 0/10/2028 09:07 ‘Spring JdocTemplate Tutorial -javatpoint Spring JdbcTemplate Tutorial Spring JdbeTemplate is a powerful mechanism to connect to the database and execute SQL queries, It internally uses JDBC api, but eliminates a lot of problems of JDBC API Problems of JDBC API The problems of JDBC API are as follows ° We need to write a lot of code before and after executing the query, such as creating connection, statement, closing resultset, connection etc. ° We need to perform exception handling code on the database logic. ° We need to handle transaction. ° Repetition of all these codes from one to another database logic is a time consuming task. Advantage of Spring JdbcTemplate Spring JdbcTemplate eliminates all the above mentioned problems of JDBC API. It provides you methods to write the queries directly, so it saves a lot of work and time. tips: javatpeint.comisoring-Jdbe Tempate-tuloral an 0/10/2028 09:07 Spring JdocTemplate Tutorial -javatpoint oo — DX Spring Jdbc Approaches Spring framework provides following approaches for JDBC database access: nites javatpointconvspring-IdbeTemplate-tutorial an 0/10/2028 09:07 ‘Spring JdocTemplate Tutorial -javatpoint ° JdbcTemplate ° NamedParameterldbcTemplate ° SimpleJdbcTemplate ° ‘Simpleldbcinsert and SimpleJdbcCall JdbcTemplate class It is the central class in the Spring JDBC support classes. It takes care of creation and release of resources such as creating and closing of connection object etc. So it will not lead to any problem if you forget to close the connection. It handles the exception and provides the informative exception messages by the help of excepion classes defined in the org.springframework.dao package, We can perform all the database operations by the help of JdbcTemplate class such as insertion, updation, deletion and retrieval of the data from the database. Let's see the methods of spring JdbcTemplate class. No. Method Description 1) public int update(String query) is used to insert, update and delete records. 2) public int update(String query,Object.. is used to insert, update and delete records args) using PreparedStatement using given arguments. 3) public void execute(String query) is used to execute DDL query. 4) public T —execute(String sql, executes the = query. ~=—sby—_using PreparedStatementCallback action) PreparedStatement callback 5) public T ~~ query(String sql, is used to. fetch_~— records. using ResultSetExtractor rse) ResultSetExtractor. 6) public List query(String sql, RowMapper is used to fetch records using RowMapper. rse) tips: javatpeint.comispring-Jdbe Tempate-tuloral an 0/10/2028 09:07 ‘Spring JdocTemplate Tutorial -javatpoint Example of Spring JdbcTemplate We are assuming that you have created the following table inside the Oracle10g database. create table employee( id number(10), name varchar2(100), salary number(10) % Employee,java This class contains 3 properties with constructors and setter and getters. package comjavatpoint; public class Employee { private int id; private String name; private float salary; //no-arg and parameterized constructors Ugetters and setters } EmployeeDao,java It contains one property jdbcTemplate and three methods saveEmployeeQ, updateEmployee and deleteEmployee(. tips: javatpeint.comisoring-Jdbe Tempate-tuloral sat 0/10/2028 09:07 ‘Spring JdocTemplate Tutorial -javatpoint package comjavatpoint; import org springframeworkjdbc.core JdbcTemplate; public class EmployeeDao ( private JdbcTemplate jdbcTemplate; public void setIdbcTemplateUdbcTemplate jdbcTemplate) { this,jdbcTemplate = jdbcTemplate; ic public int saveEmployee(Employee e){ String query ="insert into employee values( “4e.getid(+","+e.getName(+","+e.getSalary()+")"; return jdbcTemplate.update(query); ) public int updateEmployee(Employee e){ String query="update employee set hame="+e.getName()+" where ii \salary="+e.getSalary()+ +egetldg+" "; return jdbcTemplate.update(query); } pul String que ic int deleteEmployee(Employee e){ ‘delete from employee where id return jdbcTemplate.update(query); applicationContext.xml The DriverManagerDataSource is used to contain the information about the database such as driver class name, connection URL, username and password, There are a property named datasource in the JdbcTemplate class of DriverManagerDataSource type. So, we need to provide the reference of DriverManagerDataSource object in the JdbcTemplate class for the datasource property. Here, we are using the JdbcTemplate object in the EmployeeDao class, so we are passing it by the setter method but you can use constructor also. tips: javatpeint.comispring-Jdbe Tempate-tuloral ont 0/10/2028 09:07 ‘Spring JdocTemplate Tutorial -javatpoint Test.java This class gets the bean from the applicationContext.xm! file and calls the saveEmployee() method, You can also call updateEmployee) and deleteEmployee() method by uncommenting the code as well package comjavatpoint; import org springframework.context ApplicationContext; import org springframework.context support.ClassPathXmlApplicationContext; public class Test ( nitpslwwwjavatpoint convspring-IdbeTemplate-tutorial am ‘ortor029 09.07 Spring Jade Template Tuloal-javatpoint public static void main(Stringl] args) ( ApplicationContext ctx=new ClassPathXmiApplicationContext("applicationContext. xml"); EmployeeDao dao=(EmployeeDao)ctx.getBean(“edao"); int status=dao.saveEmployee(new Employee(102,"Amit",35000)); System.outprintin(status); /sint status=dao.updateEmployee(new Employee(102,"Sono0",15000)); System.out printin(status); ov /*Employee e=new Employee(); esetld(102); int status=dao.deleteEmployee(e); System.out printin(status);*/ download this example (developed using MyEclipse IDE) download this example (developed using Eclipse IDE) a) a) ‘outube For Videos Join Our Youtube Channel: Join Now tips: javatpeint.comispring-JdbeTemplate-tuloral ant 0/10/2028 09:07 Feedback © Send your Feedback to feedback @javatpoint.com Help Others, Please Share Learn Latest Tutorials Splunk tutorial Splunk #Tumblr tutorial Tumblr DR Programming tutorial R Programming 2 Python Pillow tutorial Python Pillow Preparation PAptitude Aptitude SPSS tutorial SPSS React tutorial ReactlS PRIS tutorial RS: 2 Python Turtle ‘tutorial Python Turtle # Logical Reasoning tips: javatpeint.comisoring-Jdbe Tempate-tuloral i) Swagger tutorial Swagger #Regex tutorial Regex i) React Native tutorial React Native w#Keras tutorial Keras eiVerbal Ability Verbal Ability ‘Spring JdocTemplate Tutorial -javatpoint (#T-SQL tutorial ‘Transact-SQL. # Reinforcement earning tutorial Reinforcement Learning @® Python Design Patterns Python Design Patterns 2 Interview Questions gn 0/10/2028 09:07 Reasoning. Trending Technologies 2 Artificial Intelligence Artificial Intelligence #Hadoop tutorial Hadoop #) Blockchain Tutorial Blockchain B.Tech / MCA (DBMS tutorial DBMS. 2 Computer Network tutorial ‘Computer Network Ethical Hacking Ethical Hacking LBAWS Tutorial AWS ww _ReactlS Tutorial ReactS Git Tutorial Git Data Structures tutorial Data Structures Compiler Design tutorial Compiler Design 2 Computer Graphics Tutorial tips: javatpeint.comispring-Jdbe Tempate-tuloral ‘Spring JdocTemplate Tutorial BD Selenium tutorial Selenium 1 Data Science Tutorial Data Science 2) Machine Learning Tutorial Machine Learning ADAA tutorial DAA BE Computer ‘Organization and Architecture Computer Organization 2) Software Engineering javatpoint Interview Questions 2 Company Interview Questions ‘Company Questions 2 Cloud Computing Cloud Computing #2 Angular7 Tutorial Angular 7 2 DevOps Tutorial DevOps 2 Operating System Operating System 2 Discrete Mathematics Tutorial Discrete Mathematics #html tutorial Web Technology sont 0/10/2028 09:07 Cyber Security tutorial e2Java tutorial Java 2 Control Systems tutorial Control System Computer Graphics Automata ‘Tutorial Automata ez Ne Framework tutorial Net Data Mining Tutorial Data Mining ‘Spring JdocTemplate Tutorial -javatpoint Software Engineering i) C Language C++ tutorial tutorial cH C Programming leiPython tutorial = g}_—_List of Python Programs Programs 2 Data Warehouse Tutorial Data Warehouse tips: javatpeint.comisoring-Jdbe Tempate-tuloral wnt

You might also like