Spring’s JdbcTemplate and Transactions

Yes, JdbcTemplate is not a substitute for transaction management. You still benefit from database transactions, so userService.updateUser will operate in a database transaction, but if accountService.updateXXX fails, userService.updateUser will not rollback. If you don’t want to use AOP, you can use TransactionTemplate instead. See programmatic transaction management in the Spring Reference Documentation. One pattern I’ve … Read more

Using Spring JdbcTemplate to extract one string

It would help a lot to know what your SQL query looks like, but assuming it’s something like SELECT STREET_NAME FROM table WHERE ID=1; CODE: public String getStreetNameById(int id) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); String sql = “SELECT STREET_NAME FROM table WHERE ID=?”; String streetName = (String) jdbcTemplate.queryForObject( sql, new Object[] { id }, … Read more

Does Spring JDBC provide any protection from SQL injection attacks?

It most certainly does. This example is straight from the Spring 3.0 docs (but is the same in 2.*): String lastName = this.jdbcTemplate.queryForObject( “select last_name from t_actor where id = ?”, String.class, 1212L); As you can see, it strongly favors prepared statements (which it must be using behind the scenes for you): you specify the … Read more

Best practice to select data using Spring JdbcTemplate

Definitely the first way is the best practice, because in the second way you are hitting the database twice where you should actually hit it only once. This can cause performance issues. What you need to do is catch the exception EmptyResultDataAccessException and then return null back. Spring JDBC templates throws back an EmptyResultDataAccessException exception … Read more

Using prepared statements with JDBCTemplate

By default, the JDBCTemplate does its own PreparedStatement internally, if you just use the .update(String sql, Object … args) form. Spring, and your database, will manage the compiled query for you, so you don’t have to worry about opening, closing, resource protection, etc. One of the saving graces of Spring. A link to Spring 2.5’s … Read more

How to set up datasource with Spring for HikariCP?

you need to write this structure on your bean configuration (this is your datasource): <bean id=”hikariConfig” class=”com.zaxxer.hikari.HikariConfig”> <property name=”poolName” value=”springHikariCP” /> <property name=”connectionTestQuery” value=”SELECT 1″ /> <property name=”dataSourceClassName” value=”${hibernate.dataSourceClassName}” /> <property name=”maximumPoolSize” value=”${hibernate.hikari.maximumPoolSize}” /> <property name=”idleTimeout” value=”${hibernate.hikari.idleTimeout}” /> <property name=”dataSourceProperties”> <props> <prop key=”url”>${dataSource.url}</prop> <prop key=”user”>${dataSource.username}</prop> <prop key=”password”>${dataSource.password}</prop> </props> </property> </bean> <!– HikariCP configuration –> <bean … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)