Spring JDBC and Hibernate Flashcards
Which class is used to fetch JDBC connections from database?
DataSource class is used.
Which are minimum required configurations for creating a database connection?
Driveclassname,
URL,
username,
password
Which class of DBCP is used to create and pool database connection?
BasicDataSource
Which tag is used to read properties file?
context:property-placeholder and its location property is used to define path of file.
Which utility is provided in spring that helps in reading data from database or ORM tools?
JDBCTemplate, HibernateTemplate are available which do the heavylifting of cleaning up resources.
Which methods are used to fetch data and execute DML instructions?
To get the data queryXXXmethods are used and for inserting, updating and deleting execute() method is used.
Which overloaded methods are used for querying different data types of single row and multiple rows
queryForInt queryForObject queryForList queryForMap queryForRowSet
What does queryForMap return?
queryForMap returns single row in a Map map.
Why should bind variables be used in place of appending to prepare query?
Preparing query manually makes it prone to SQL injection. So bind variables are used.
Using bind variables with JDBC template
JDBC template takes extra parameter Object[] for passing arguments. jdbcTemplate.queryForObject("Select * from Students where id=?", new Object[] {id}, new StudentMapper());
Which interface is used to map from row to domain objects?
RowMapper interface is used to convert from row to domain object. mapRow(ResultSet rs) should be implemented.
Which method is used for inserting, updating and deleting rows from table using jdbc template?
update() method is used to execute these statements and int returned represents the number of rows affected.
Which template variant can be used to create statement using names?
NamedParameterJdbcTemplate can be used. String sql = "insert into students (NAME) values (:name)"; Map params = new HashMap<>(); params.put("name", name); jdbcTemplate.update(sql, params);
How to use SQL batch operations using JDBC template?
SqlParameterSource[] params = SqlParameterSourceUtils.createBatch(employees.toArray());
jdbcTemplate.batchUpdate(“query”, params);
Which interface is provided to set batch values to prepare statement and fire batch operation in jdbctemplate
BatchPreparedStatementSetter is provided. It has method that gives you prepared statement and index that you can use to fetch value at that index and set that in prepared statement. The batching logic is implemented by the template.