Spring JDBC

  • Spring JDBC is a powerful mechanism to connect to The database and execute SQL queries.
  • We use JDBC template class to connect to database and perform operations.
  • JDBC is an API to perform operations with database.
  • Problems with old JDBC.
    • We need to write a lot of code as we need to repeatedly open JDBC connection, get and execute statement and close the connection.
    • Exception handling problem in which we need to handle checked exception i.e. SQL exception.
    • Repeating all these codes from one to another database, logic is a time consuming task.
  • Spring JDBC corrects all the above problems.
  • JDBC template requires following dependencies
    • DataSource
      • A source which contains all metadata of our database.
    • DriverManagerDataSource class is passed as implementation of DataSource Interface.
    • Information required by DriverManagerDataSource to Interact with database is as follows.
      • DriverClassName
      • URL of database
      • Username
      • Password
  • JDBC template has following important methods
    • Update for insert, update, delete queries.
    • Execute for select queries.
  • Swing JDBC select data using following methods.
    • For single object
      • Selecting data using Spring JDBC.
      • Public T queryForObject(String sql, Rowmapper <T> rowmapper,Object args…)
    • For multiple object
      • Public List<T> query (String sql,RowMapper <T> rowmapper)
      • Row Mapper converts result set object into object of class.

No comments:

Post a Comment

Spring Boot

What is circular/cyclic dependency in spring boot? When two services are interdependent on each other, that is to start one service, we requ...