jdbctemplate

Using prepared statements with JDBCTemplate

浪尽此生 提交于 2019-12-03 04:14:38
问题 I'm using the JDBC template and want to read from a database using prepared statements. I iterate over many lines in a .csv file, and on every line I execute some SQL select queries with corresponding values. I want to speed up my reading from the database but I don't know how to get the JDBC template to work with prepared statements. There is the PreparedStatementCreator and the PreparedStatementSetter. As in this example both of them are created with anonymous inner classes. But inside the

Spring JDBC transaction manager

匿名 (未验证) 提交于 2019-12-03 03:03:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I try to write a transaction manager using JDBC in Spring. my app-servlet.xml <!-- JDBC Config --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> <!-- dataSource TransactionManager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="UserDAOImpl

JdbcTemplate queryForInt/Long is deprecated in Spring 3.2.2. What should it be replaced by?

旧巷老猫 提交于 2019-12-03 02:54:05
问题 The queryforInt/queryforLong methods in JdbcTemplate are deprecated in Spring 3.2. I can't find out why or what is considered the best practice to replace existing code using these methods. A typical method: int rowCount = jscoreJdbcTemplate.queryForInt( "SELECT count(*) FROM _player WHERE nameKey = ? AND teamClub = ?", playerNameKey.toUpperCase(), teamNameKey.toUpperCase() ); OK the above method needs to be re-written as follows: Object[] params = new Object[] { playerNameKey.toUpperCase(),

how to convert jndi lookup from xml to java config

匿名 (未验证) 提交于 2019-12-03 02:52:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Currently I'm converting the xml to java config. But I stuck at some part that I have been research for several days. Here the problem: Xml config: <jee:jndi-lookup id="dbDataSource" jndi-name="${db.jndi}" resource-ref="true" /> <beans:bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" > <beans:property name="dataSource" ref="dbDataSource"></beans:property> </beans:bean> So far I managed to convert this code: <jee:jndi-lookup id="dbDataSource" jndi-name="${db.jndi}" resource-ref="true" /> to this : @Bean(name =

Roll back A if B goes wrong. spring boot, jdbctemplate

旧城冷巷雨未停 提交于 2019-12-03 02:51:53
问题 I have a method, 'databaseChanges', which call 2 operations: A, B in iterative way. 'A' first, 'B' last. 'A' & 'B' can be C reate, U pdate D elete functionalities in my persistent storage, Oracle Database 11g. Let's say, 'A' update a record in table Users, attribute zip, where id = 1. 'B' insert a record in table hobbies. Scenario: databaseChanges method is been called, 'A' operates and update the record. 'B' operates and try to insert a record, something happen, an exception is been thrown,

JdbcTemplate query close database connection

匿名 (未验证) 提交于 2019-12-03 02:51:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I use jpa with hibernate. I have following method: @Transactional public void myMethod(){ ... firstJDBCTemplateQuery(); secondJDBCTemplateQuery(); ... } firstJDBCTemplateQuery works, but it closes connection to database. When second secondJDBCTempolateQuery is executed java.sql.SQLException: Connection is closed exception is thrown what causes org.springframework.transaction.TransactionSystemException: Could not roll back JPA transaction ... My configuration: EDIT <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

Jdbctemplate query for string: EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0

匿名 (未验证) 提交于 2019-12-03 02:14:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using Jdbctemplate to retrieve a single String value from the db. Here is my method. public String test() { String cert=null; String sql = "select ID_NMB_SRZ from codb_owner.TR_LTM_SLS_RTN where id_str_rt = '999' and ID_NMB_SRZ = '60230009999999'"; cert = (String) jdbc.queryForObject(sql, String.class); return cert; } In my scenario it is complete possible to NOT get a hit on my query so my question is how do I get around the following error message. EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0 It would

Spring JDBC with Tomcat DBCP and multiple datasources

匿名 (未验证) 提交于 2019-12-03 01:45:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am using spring with have multiple datasources (catering to multiple mysql dbs) and using tomcat dbcp. I am getting some weird exceptions like procedure not found - when the proc is definitely present in the db cannot borrow from pool - local dev setup, so definitely the pool is not full the Problem I feel might be this, need inputs from everyone: I have one jdbcTemplate object defined in my spring.xml , on every query that I need to fire, I call jdbcTemplate.setDataSource() to set the appropriate datasource and then use

Multiple JdbcTemplate instances or not?

回眸只為那壹抹淺笑 提交于 2019-12-03 01:30:40
From what I understand, both DataSource and JdbcTemplates are threadsafe , so you can configure a single instance of a JdbcTemplate and then safely inject this shared reference into multiple DAOs (or repositories) . Also DataSource should be a Spring singleton, since it manages the connection pool. The official Spring Documentation JdbcTemplate best practices explains the alternatives (excerpts from the manual are in italics, and my notes between square brackets: configure a DataSource in your Spring configuration file, and then dependency-inject that shared DataSource bean into your DAO

Does Spring JDBC provide any protection from SQL injection attacks?

孤人 提交于 2019-12-03 01:15:07
Spring's JdbcTemplate abstraction provides a lot of functionality, but can it be used in such a way that provides protection from SQL injection attacks? For example, like the kind of protection you would get using PreparedStatement with properly defined parameterization. Donal Fellows 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