jdbctemplate

Spring Boot多数据源配置之Template

喜夏-厌秋 提交于 2019-11-26 19:45:12
Spring Boot多数据源配置之Template 前言 代码实现 依赖引入 配置数据源 加载配置文件 配置JdbcTemplate实例 测试 前言   多数据源配置也算是一个常见的开发需求,在Spring和Spring Boot中,对此都有相应的解决方案,不过一般来说,如果有多数据源的需求,还是建议首选分布式数据库中间件 MyCat 去解决相关问题。当然如果一些简单的需求,还是可以使用多数据源的,Spring Boot中,JdbcTemplate、MyBatis以及JPA都可以配置多数据源,本文就先和大伙聊一聊 JdbcTemplate 中多数据源的配置。 代码实现 依赖引入 和单数据源一样,需要引入如下的依赖 < dependencies > < dependency > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-jdbc </ artifactId > </ dependency > < dependency > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-web </ artifactId > </ dependency

identity from sql insert via jdbctemplate

风格不统一 提交于 2019-11-26 17:59:12
问题 Is it possible to get the @@identity from the SQL insert on a Spring jdbc template call? If so, how? 回答1: The JDBCTemplate.update method is overloaded to take an object called a GeneratedKeyHolder which you can use to retrieve the autogenerated key. For example (code taken from here): final String INSERT_SQL = "insert into my_test (name) values(?)"; final String name = "Rob"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update( new PreparedStatementCreator() { public

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

做~自己de王妃 提交于 2019-11-26 12:50:57
问题 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:

Spring JdbcTemplate与Spring NamedParameterJdbcTemplate

梦想的初衷 提交于 2019-11-26 10:29:09
JdbcTemplate简介   Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。   JdbcTemplate位于 中。其全限定命名为org.springframework.jdbc.core.JdbcTemplate。   要使用JdbcTemlate还需一个 这个包包含了一下事务和异常控制    JdbcTemplate主要提供以下五类方法: execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句; update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句; query方法及queryForXXX方法:用于执行查询相关语句; call方法:用于执行存储过程、函数相关语句。 案例分析 1.新建一个属性配置文件 1 jdbc.user=root 2 jdbc.password=123456 3 jdbc.driverClass=com.mysql.jdbc.Driver 4 jdbc.jdbcUrl=jdbc\:mysql\:///test 2.配置Spring配置文件applicationContext.xml 1 <context:property-placeholder

how to copy a data from file to PostgreSQL using JDBC?

喜你入骨 提交于 2019-11-26 09:37:45
问题 I want to copy data from file to PostgreSQL DB using JDBC. I was using JDBC statement object to copy the file into DB. It is very slow. I came to know that we can also use copy out command to copy file to DB. But, how do i do with JDBC. Even good reference material having an example of copy in JDBC would help. PS: thanks in advance 回答1: This works... import java.io.FileReader; import java.sql.Connection; import java.sql.DriverManager; import org.postgresql.copy.CopyManager; import org

How to set up datasource with Spring for HikariCP?

故事扮演 提交于 2019-11-26 08:06:06
问题 Hi I\'m trying to use HikariCP with Spring for connection pool. I\'m using jdbcTempLate and JdbcdaoSupport. This is my spring configuration file for datasource: <bean id=\"dataSource\" class=\"com.zaxxer.hikari.HikariDataSource\"> <property name=\"dataSourceClassName\" value=\"oracle.jdbc.driver.OracleDriver\"/> <property name=\"dataSource.url\" value=\"jdbc:oracle:thin:@localhost:1521:XE\"/> <property name=\"dataSource.user\" value=\"username\"/> <property name=\"dataSource.password\" value=

Spring JDBC Template for calling Stored Procedures

五迷三道 提交于 2019-11-26 06:13:00
问题 What is the correct way to invoke stored procedures using modern day (circa 2012) Spring JDBC Template? Say, I have a stored procedure that declares both IN and OUT parameters, something like this: mypkg.doSomething( id OUT int, name IN String, date IN Date ) I have come across CallableStatementCreator based approaches where we have to explicitly register IN and OUT parameters. Consider the following method in JdbcTemplate class: public Map<String, Object> call(CallableStatementCreator csc,

Error when connect to impala with JDBC under kerberos authrication

≡放荡痞女 提交于 2019-11-26 04:00:30
问题 I create a class SecureImpalaDataSource that extends DriverManagerDataSource, and use UserGroupInformation.doAs() to get a Connection to impala with keytab file. But I get the error as follow: java.sql.SQLException: [Simba]ImpalaJDBCDriver Error initialized or created transport for authentication: [Simba]ImpalaJDBCDriver Unable to connect to server: null. But I am successful when I get the connection with kerberos ticket cache in a test demo. Anyone can help me? 回答1: Forget about the Hadoop

How to execute IN() SQL queries with Spring&#39;s JDBCTemplate effectivly?

岁酱吖の 提交于 2019-11-26 03:02:19
I was wondering if there is a more elegant way to do IN() queries with Spring's JDBCTemplate. Currently I do something like that: StringBuilder jobTypeInClauseBuilder = new StringBuilder(); for(int i = 0; i < jobTypes.length; i++) { Type jobType = jobTypes[i]; if(i != 0) { jobTypeInClauseBuilder.append(','); } jobTypeInClauseBuilder.append(jobType.convert()); } Which is quite painful since if I have nine lines just for building the clause for the IN() query. I would like to have something like the parameter substitution of prepared statements yawn You want a parameter source: Set<Integer> ids

How to execute IN() SQL queries with Spring&#39;s JDBCTemplate effectivly?

[亡魂溺海] 提交于 2019-11-26 01:51:16
问题 I was wondering if there is a more elegant way to do IN() queries with Spring\'s JDBCTemplate. Currently I do something like that: StringBuilder jobTypeInClauseBuilder = new StringBuilder(); for(int i = 0; i < jobTypes.length; i++) { Type jobType = jobTypes[i]; if(i != 0) { jobTypeInClauseBuilder.append(\',\'); } jobTypeInClauseBuilder.append(jobType.convert()); } Which is quite painful since if I have nine lines just for building the clause for the IN() query. I would like to have something