jdbctemplate

spring中JDBCTemplate的简单应用

依然范特西╮ 提交于 2019-11-27 14:15:17
package cn.itcast.datasource.jdbctemplate;import cn.itcast.utils.JDBCUtils;import org.springframework.jdbc.core.JdbcTemplate;import javax.sql.DataSource;/** * @author newcityman * @date 2019/8/17 - 0:51 * JdbcTemplate入门 */public class JdbcTemplateDemo01 { public static void main(String[] args) {// 1、导入相应jar包// 2、创建JDBCTemplate对象 JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()) ;// 3、调用方法 String sql ="update account set balance=? where id=?"; int i = template.update(sql, 8000, 4); if (i>0){ System.out.println("数据修改成功"); }else{ System.out.println("数据修改失败"); } }} 来源: https:/

spring基于XML的声明式事务控制

杀马特。学长 韩版系。学妹 提交于 2019-11-27 14:09:21
<?xml version="1.0" encoding="utf-8" ?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop

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

佐手、 提交于 2019-11-27 12:57:25
问题 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

identity from sql insert via jdbctemplate

末鹿安然 提交于 2019-11-27 11:05:46
Is it possible to get the @@identity from the SQL insert on a Spring jdbc template call? If so, how? 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 PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps =

using Spring JdbcTemplate - injecting datasource vs jdbcTemplate

吃可爱长大的小学妹 提交于 2019-11-27 10:29:50
问题 As per Spring documentation, the steps to use Spring JdbcTemplate is as follows: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework

Using a prepared statement and variable bind Order By in Java with JDBC driver

走远了吗. 提交于 2019-11-27 09:03:16
I'm using jdbcTemplate to make JDBC connections to a mySQL DB prepared statements to protect myself as much as possible from SQL injection attacks in need to accept requests from the user to sort the data on any of a dozen different columns the following statement jdbcTemplate.query("SELECT * FROM TABLE1 ORDER BY ? ?", colName, sortOrder); Of course this doesn't work, because the variable bindings aren't supposed to specify column names just parameter values for expressions in the query. So...how are people solving this issue? Just doing the sort in Java code seems like an easy solution, but

spring 3.1: jdbcTemplate auto commit to false.

百般思念 提交于 2019-11-27 07:24:48
问题 Hi Is their a way to set autocommit to false in spring jdbctemplate. The thing is instead of transaction (where their is rollback option), I want to have query committed at end of transaction. So instead of insert --> commit --> rollback. I want insert --> fail --> (no commit). 回答1: I did not understand your whole question, but I can answer the first part: Is there a way to set autocommit to false in spring jdbctemplate? The autocommit configuration is normally set on the connection itself.

【翻译】spring-data 之JdbcTemplate 使用

旧时模样 提交于 2019-11-27 07:22:30
文档 Jdbc的使用 基础的代码结构: 一个 Application 作为入口。 IUserRepository 和 UserRepository 作为具体的实现。 applicationContext.xml 定义spring的配置。 db.properties 保存数据库相关的信息。 环境搭建步骤 新建项目 新建一个maven项目,编辑 pom.xml 文件,如下。除了mysql的驱动,其他是必须的。 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lou.spring.demo5.tx</groupId> <artifactId>lou-spring-demo5-tx</artifactId> <version>1.0-SNAPSHOT<

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

时间秒杀一切 提交于 2019-11-27 06:02:48
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 seem to me that I should just get back a null instead

Spring的事务管理难点剖析(1):DAO和事务管理的牵绊

徘徊边缘 提交于 2019-11-27 05:53:10
有些人很少使用Spring而不使用Spring事务管理器的应用,因此常常有人会问:是否用了Spring,就一定要用Spring事务管理器,否则就无法进行数据的持久化操作呢?事务管理器和DAO是什么关系呢? 也许是DAO和事务管理如影随行的缘故吧,这个看似简单的问题实实在在地存在着,从初学者心中涌出,萦绕在老手的脑际。答案当然是否定的!我们都知道:事务管理是保证数据操作的事务性(即原子性、一致性、隔离性、持久性,即所谓的ACID),脱离了事务性,DAO照样可以顺利地进行数据的操作。 JDBC访问数据库 下面,我们来看一段使用Spring JDBC进行数据访问的代码: package com.baobaotao.withouttx.jdbc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.context.ApplicationContext; import org.springframework.context.support