jdbctemplate

Spring boot - how to configure multiple datasources

左心房为你撑大大i 提交于 2019-11-29 12:02:17
I am trying to setup multiple data sources(MySql, Postgres & Oracle) using Spring boot. I am not using JPA. Setting up with a JdbcTemplate. I have tried setting up something like this. application.properties spring.datasource.test-oracle.username=test-oracle spring.datasource.test-oracle.password=test-password spring.datasource.test-oracle.url=dburl/test spring.datasource.test-oracle.driver-class-name=oracle.jdbc.OracleDriver spring.datasource.int-oracle.username=int-oracle spring.datasource.int-oracle.password=int-password spring.datasource.int-oracle.url=dburl/int spring.datasource.int

spring jdbctemplate 项目使用完整记录

China☆狼群 提交于 2019-11-29 07:17:53
一、前言 项目使用jdbctemplate已经一段时间了,对于jdbcTemplate的使用有了一些小小心得,这里总结后跟大家分享下。 二、spring xml 配置jdbcTemplate <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost/shop?characterEncoding=utf-8&autoReconnect=true </value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core

A ResourcePool could not acquire a resource from its primary factory or source

≡放荡痞女 提交于 2019-11-29 03:50:54
I'm trying to connect to a database in Java, using jdbcTemplate and I'm gettin the error below. I have Googled for a long time and all solutions I found didn't solve my problem. I tried several different DBs (both SQLServer and MySQL) and none worked. SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/promotion-handler-admin] threw exception [Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!] with root cause com.mchange.v2.resourcepool

Spring JdbcTemplate方法详解

泪湿孤枕 提交于 2019-11-29 02:38:45
qldbFunctionSql); String createHsqldbProcedureSql = "CREATE PROCEDURE PROCEDURE_TEST" + "(INOUT inOutName VARCHAR(100), OUT outId INT) " + "MODIFIES SQL DATA " + "BEGIN ATOMIC " + " insert into test(name) values (inOutName); " + " SET outId = IDENTITY(); " + " SET inOutName = 'Hello,' + inOutName; " + "END" ; jdbcTemplate.execute(createHsqldbProcedureSql); } 其中CREATE FUNCTION FUNCTION_TEST用于创建自定义函数,CREATE PROCEDURE PROCEDURE_TEST用于创建存储过程,注意这些创建语句是数据库相关的,本示例中的语句只适用于HSQLDB数据库。 其次修改JdbcTemplateTest的tearDown方法,修改后如下所示: java代码: Java代码 @After public void tearDown() { jdbcTemplate.execute( "DROP

How can I get the autoincremented id when I insert a record in a table via jdbctemplate

↘锁芯ラ 提交于 2019-11-29 01:42:46
private void insertIntoMyTable (Myclass m) { String query = "INSERT INTO MYTABLE (NAME) VALUES (?)"; jdbcTemplate.update(query, m.getName()); } When the above query inserts a record, the ID column in the table autoincrements. Is there a way to get this auto incremented ID back at the time of the insertion. So in this example the return value of my method would be int Check this reference. You can use jdbcTemplate.update as: EDIT Added imports as asked import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import org.springframework.jdbc.core.JdbcTemplate;

Unit testing a DAO class that uses Spring JDBC

﹥>﹥吖頭↗ 提交于 2019-11-29 01:19:48
问题 I have several DAO objects that are used to retrieve information from a database and I really want to write some automated tests for them but I'm having a hard time figuring out how to do it. I'm using Spring's JdbcTemplate to run the actual query (via a prepared statement) and map the results to the model object (via the RowMapper class). If I were to write unit tests, I'm not sure how I would/should mock the objects. For example, since there are only reads, I would use the actual database

Inserting multiple rows using JdbcTemplate

那年仲夏 提交于 2019-11-29 00:53:46
问题 How can I execute the following SQL in a scalable way using JdbcTemplate running on mySQL. In this case, scalable means: Only one SQL statement is executed on the server it works for any number of rows. Here's the statement: INSERT INTO myTable (foo, bar) VALUES ("asdf", "asdf"), ("qwer", "qwer") Assume that I have a list of POJO's with foo and bar fields. I realize that I could just iterate over the list and execute: jdbcTemplate.update("INSERT INTO myTable(foo, bar) VALUES (?, ?)", paramMap

Multiple DataSource and JdbcTemplate in Spring Boot (> 1.1.0)

青春壹個敷衍的年華 提交于 2019-11-28 23:41:39
问题 I would like to inject a specific JdbcTemplate in a Spring Boot project. I tried to follow this example for multiple DataSource configuration : http://spring.io/blog/2014/05/27/spring-boot-1-1-0-m2-available-now My code does compile and run, but only the DataSource with the @Primary annotation is taken into account, no matter what I put as @Qualifier in the SqlService class. My relevant code is the following : DatabaseConfig.java : @Configuration public class DatabaseConfig { @Bean(name =

Spring JdbcTemplate - Insert blob and return generated key

时光毁灭记忆、已成空白 提交于 2019-11-28 23:11:11
From the Spring JDBC documentation, I know how to insert a blob using JdbcTemplate final File blobIn = new File("spring2004.jpg"); final InputStream blobIs = new FileInputStream(blobIn); jdbcTemplate.execute( "INSERT INTO lob_table (id, a_blob) VALUES (?, ?)", new AbstractLobCreatingPreparedStatementCallback(lobhandler) { protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException { ps.setLong(1, 1L); lobCreator.setBlobAsBinaryStream(ps, 2, blobIs, (int)blobIn.length()); } } ); blobIs.close(); And also how to retrieve the generated key of a newly inserted row :

Return Type for jdbcTemplate.queryForList(sql, object, classType)

时间秒杀一切 提交于 2019-11-28 20:30:41
I'm executing a named query using jdbcTemplate.queryForList in the following manner: List<Conversation> conversations = jdbcTemplate.queryForList( SELECT_ALL_CONVERSATIONS_SQL_FULL, new Object[] {userId, dateFrom, dateTo}); The SQL query is: private final String SELECT_ALL_CONVERSATIONS_SQL_FULL = "select conversation.conversationID, conversation.room, " + "conversation.isExternal, conversation.startDate, " + "conversation.lastActivity, conversation.messageCount " + "from openfire.ofconversation conversation " + "WHERE conversation.conversationid IN " + "(SELECT conversation.conversationID " +