spring-jdbc

How to only return String objects from JdbcTemplate queryForList?

青春壹個敷衍的年華 提交于 2019-12-04 05:49:12
问题 By default, queryForList() returns each for as a Map<String, Object> . The object can be plain String , java.sql.Timestamp , etc. List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, params); Question: how can I enfore returning any values as String.class ? So I'd be having a Map<String, String> . I tried: jdbcTemplate.queryForList(sql, params, Map<String, String>.class) But that statement seems to be invalid and does not compile. 回答1: Maybe there is a better way, but the

What's the appropriate way to test code that uses MySQL-specific queries internally

瘦欲@ 提交于 2019-12-04 03:06:45
I am collecting data and store this data in a MySQL database using Java. Additionally, I use Maven for building the project, TestNG as a test framework, and Spring-Jdbc for accessing the database. I've implemented a DAO layer that encapsulates the access to the database. Besides adding data using the DAO classes I want to execute some queries which aggregate the data and store the results in some other tables (like materialized views). Now, I would like to write some testcases which check whether the DAO classes are working as they should. Therefore, I thought of using an in-memory database

how can I batchUpdate with a query that requires 2 parameters and only one of them is stored in a list

家住魔仙堡 提交于 2019-12-04 02:24:12
问题 I use Spring-JDBC to insert the list of facebook friends for a user in my MySQL database. I have a final Long that contains the user uid and a List that contains the list of his friends. my query is: final String sqlInsert="insert into fb_user_friends(fb_uid,friend_uid) values(?,?)"; I create batch parameters using SqlParameterSourceUtils SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(friendsList.toArray()); and I execute the insert using: int[] insertCounts = this.

Should I close JNDI-obtained data source?

谁说我不能喝 提交于 2019-12-04 00:53:51
问题 Update: Apparently Tomcat, starting with 7.0.11, closes the DataSource for you, so it's not available in the webapp's contextDestroyed. See: https://issues.apache.org/bugzilla/show_bug.cgi?id=25060 Hi, I'm using Spring 3.0 and Java 1.6. If I get a data source this way: <bean id="dataSource" class="my.data.Source" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:home"/> <property name

Difference Between Spring JDBC Vs Plain JDBC?

元气小坏坏 提交于 2019-12-03 22:53:09
What is the main difference between Spring JDBC VS JDBC? Tomasz Nurkiewicz Let me show you some simple example using JDBC: final Connection connection = ds.getConnection(); try { final Statement statement = connection.createStatement(); try { final ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM Orders"); try { resultSet.next(); final int c = resultSet.getInt(1); } finally { resultSet.close(); } } finally { statement.close(); } } finally { connection.close(); } It's much better when try-with-resources though: try ( Connection connection = ds.getConnection(); Statement

Some doubts about RowMapper use in JDBC in a Spring Framework application

怎甘沉沦 提交于 2019-12-03 17:07:06
问题 I am studying how to execute query on a database using JDBC in Spring Framework. I am following this tutorial: http://www.tutorialspoint.com/spring/spring_jdbc_example.htm In this tutorial I define a StudentDAO interface which only define the CRUD method that I want. Then is defined the Student class that is the entity that I want to persist on the Student database table. Then is defined the StudentMapper class that is a specific implementation of RowMapper interface that, in this case, is

Insert UTC/GMT date in Oracle database with Java and Spring

隐身守侯 提交于 2019-12-03 16:19:46
When I insert new Date() object using jdbcTemplate to Oracle database, I can see that jdbc driver or Spring jdbcTemplate insert Date using local JVM offset. SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); Date timeZoneDate = sdf.parse("09-SEP-1987"); For example when I insert Date object created in GMT this result to inserting 08-SEP-1987 in Oracle database if JVM timezone is USA. Anton Neither java.util.Date nor Oracle Date stores timezone information. In your case Jdbc driver converts your date using the JVM timezone. You can use one

How do I get Spring Boot to automatically reconnect to PostgreSQL?

半世苍凉 提交于 2019-12-03 16:10:56
问题 I am running Spring Boot connecting to a PostgreSQL database. I have verified that data is written to the database if Spring Boot is started after the database. spring.datasource.url = jdbc:postgresql://localhost/dbname spring.datasource.username = user spring.datasource.password = secret spring.datasource.driver-class-name = org.postgresql.Driver spring.datasource.testOnBorrow=true spring.datasource.validationQuery=SELECT 1 When there are changes in the database in development, I run into

Stream closeable resource with Spring MVC

ε祈祈猫儿з 提交于 2019-12-03 15:31:08
After having read this article , I wish to use Spring to stream database query results directly to a JSON response to ensure constant-memory usage (no greedy loading of a List in memory). Similar to what is done in the article with Hibernate, I assembled a greetingRepository object which returns a stream of the database contents based on a JdbcTemplate . In that implementation, I create an iterator over the queried ResultSet , and I return the stream as follows: return StreamSupport.stream(spliterator(), false).onClose(() -> { log.info("Closing ResultSetIterator stream"); JdbcUtils

Spring Boot: Jdbc javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify

荒凉一梦 提交于 2019-12-03 15:18:02
问题 I am currently learning more about implementing JDBC and using databases in a Spring Boot webapp, and I encountered the following Stack Trace written in the bottom of the post. I have created a simple Employee model, and I am trying to execute some database code on the same class which my main() lies in. The model and the main class are the only two java files existing in this whole project. I am trying to implement the following run() code that overrides the one from the interface,