Monday, June 1, 2020

Spring JDBC Template

  • Spring JdbcTemplate 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

  • We need to write a lot of code before and after executing the query, such as creating connection, statement, closing resultset, connnection 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 JDBC Template

  • 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.

Spring Jdbc Approaches

  • JdbcTemplate
  • NamedParameterJdbcTemplate
  • SimpleJdbcTemplate
  • SimpleJdbcInsert and SimpleJdbcCall

JdbcTemplate Class

  • It is the central class in the spring  jdbc support classes. It takes care of creation and releases of resources such as creating and closing of connection object etc.
  • It handles the exception and provides the informative exception messages by the help of exception 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

Spring JDBC - who does what?

Action Spring You
Define Connection parameters
yes
Open the Connection yes
Specify the SQL statement
yes
Declare parameters and provide parameter values
yes
Prepare and execute the statementyes
Set up the loop to iterate through the resultyes
Do the work for each iteration
yes
Process any exception yes
Handle transactionsyes
Close the connection, statement and resultsetyes
Do we need SQLyog?
SQLyog is the most powerful manager, admin and GUI tool for MySQL, combining the features of MySQL Query Browser, Administrator, phpMyAdmin and other MySQL Front Ends and MySQL GUI tools in a single intuitive interface. SQLyog is a fast, easy to use and compact graphical tool for managing your MySQL databases


No comments:

Post a Comment