1. 程式人生 > >Spring JDBC更新資料

Spring JDBC更新資料

以下示例將展示如何使用Spring jdbc執行更新資料庫表的記錄,這裡演示如何更新student表中指定條件的記錄。

語法:

String updateQuery = "update Student set age = ? where id = ?";
jdbcTemplateObject.update(updateQuery, age, id);

在上面語法中 -

  • updateQuery - 具有佔位符的更新查詢用來更新學生資訊。
  • jdbcTemplateObject - StudentJDBCTemplate 物件將 Student 物件更新到資料庫表中。

更新資料方法:

	public void update(Integer id, Integer age) {
		String sql = "update Student set age = ? where id = ?";
		jdbcTemplateObject.update(sql, age, id);
		System.out.println("Updated Record with ID = " + id);
		return;
	}

執行程式:

public class MainApp {

	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext("application-beans.xml");

		StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate) context.getBean("studentJDBCTemplate");

		System.out.println("----Updating Record with ID = 22 -----");
		studentJDBCTemplate.update(22, 99);
		
		System.out.println("----Listing Record with ID = 22 -----");
		Student student = studentJDBCTemplate.getStudent(22);
		System.out.print("ID : " + student.getId());
                System.out.print(", Name : " + student.getName());
                System.out.println(", Age : " + student.getAge());
	}
}

執行結果:

三月 07, 2018 8:46:05 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
資訊: Refreshing org[email protected]254989ff: startup date [Wed Mar 07 20:46:05 CST 2018]; root of context hierarchy
三月 07, 2018 8:46:05 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [application-beans.xml]
三月 07, 2018 8:46:05 下午 org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
資訊: Loaded JDBC driver: com.mysql.jdbc.Driver
----Updating Record with ID = 22 -----
Wed Mar 07 20:46:06 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Updated Record with ID = 22
----Listing Record with ID = 22 -----
Wed Mar 07 20:46:06 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
ID : 22, Name : Allen, Age : 99

相關推薦

Spring JDBC更新資料

以下示例將展示如何使用Spring jdbc執行更新資料庫表的記錄,這裡演示如何更新student表中指定條件的記錄。語法:String updateQuery = "update Student set age = ? where id = ?"; jdbcTemplate

Spring JDBC刪除資料

以下示例將展示如何使用Spring jdbc執行刪除資料庫表中的記錄,這裡演示如何刪除指定student表中的記錄。語法:String deleteQuery = "delete from Student where id = ?"; jdbcTemplateObject.u

Spring-JDBC 資料訪問

新增.jar包 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org

Spring 5 官方文件》15.使用JDBC實現資料訪問

15.1 介紹Spring JDBC框架 表格13.1很清楚的列舉了Spring框架針對JDBC操作做的一些抽象和封裝。裡面區分了哪些操作Spring已經幫你做好了、哪些操作是應用開發者需要自己負責的. 表13.1. Spring JDBC – 框架和應用開發者各自分工 操作 Sprin

JDBC更新資料庫中資料

JDBC的功能基本上歸結為三件事,即建立資料庫連線,傳送SQL語句和處理查詢結果。 JDBC詳細步驟: 1.導包(先將驅動包拷貝到專案目錄下,再解壓) 路徑:D:\oracle\prod

spring MongoTemplate 更新複雜物件資料

先說下需求(我把需求簡化了下),看下圖。現在有很多人選了自己要上的課程,資料存mongo裡,但是發現courses中id=2的【數學】,名稱錄入錯誤了,或者說id=2的數學課程,名稱需要更新成“高等數學”。   一開始按照老套路直接去update,會報一個錯。Write fa

Spring JDBC學習筆記(3):使用JdbcTemplate來獲取資料庫表和列的元資料

本篇文章才是我寫這一個系列文章的目的,原來我一般都是使用jdbc獲取資料庫的元資料,但是有點讓我很不爽,每次查資料表和列的時候都要自己寫“TABLE_NAME”和“COLUMN_HOME”等名稱,所以乾脆轉到JdbcTemplate實現上。通過JdbcTemplate獲取資

jdbc使用 PreparedStatement 介面實現更新資料操作

jdbc使用 PreparedStatement  介面實現更新資料操作 package chap04.sec03; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql

Spring jdbc批量儲存資料

在很多時候同步資料的時候需要使用jdbc實現批量儲存,當然在分散式和主從資料庫就不用考慮這種方式了,我用這種方式只是為了有限數量的資料批量插入。 資料list:list.add(bcHistory); 批量插入: 1 2 3 4 5 6 7 8 9 10 11 12 13

JDBC插入資料更新資料,使用NamedParameterJdbcTemplate物件直接操作javaBean

原來如此簡單: DButil: public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { if (nam == null) {             synchronized(DBU

Spring JDBC-資料連線洩露解讀

概述 資料連線洩漏無疑是一個可怕的夢魘。如果存在資料連線洩漏問題,應用程式將因資料連線資源的耗盡而崩潰,甚至還可能引起資料庫的崩潰。 Spring DAO 對所有支援的資料訪問技術框架都使用模板化技術進行了薄層的封裝。只要我們的應用程式都使用 Sp

spring jdbcTemplate批量更新資料

引入了spring框架之後,jdbcTemplate可以直接使用 將jdbcTemplate依賴注入 @Autowired     private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate

使用JDBC讀取本地的Excel檔案並批量更新資料

package com.jqgj.test; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; impo

ElasticSearch-jdbc從Mysql資料庫匯入和更新資料

環境: 需要的資源和版本: - git客戶端 - Elasticsearch 版本 2.2.0 安裝教程 - elasticsearch-jdbc 版本 2.2 - ik分詞器 版本 1.8 (下載後解壓將ik資料夾放在elastics

jdbc批量更新資料

有些時候,適合用jdbc。比如讀一個大的excel檔案資料到資料庫 public static List<String> welfareImport(MultipartFile file,String id,String businessType,IWelfa

Spring+JDBC組合開發

mark public image eclipse 註解 需要 比較 setname ram 搭建和配置Spring與JDBC整合的環境 使用Spring+JDBC集成步驟如下: 第一步,配置數據源。我們使用DBCP數據庫連接池。 我們首先在Eclipse中新建一

關於spring jdbc 查詢mysql數據庫時,數據集字段名有別名返回的不是別名而是原始字段名

spring jdbc mysql 別名select語句字段含有別名eg:select xm fullname from t_user;JdbcTemplate查詢返回的結果集 xm‘張三‘‘xxx‘‘xxxx‘期待結果是:fullname‘張三‘‘xxx‘‘xxx‘解決辦法;在數據庫鏈接url後面跟上?us

Spring JDBC-混合框架的事務管理

組合 manager 延遲 發生 required 應用 conf 一個 研究 ? Spring 抽象的 DAO 體系兼容多種數據訪問技術,它們各有特色,各有千秋。 Hibernate 是非常優秀的 ORM 實現方案,但對底層 SQL 的控制不太方便 M

maven初始搭建一個基礎項目(spring mvc+spring+jdbc mysql+)

mon 處理 名稱 archetype HA name -m web.xml 控制器 技術選型: 一.項目搭建: 1)創建maven項目 (我博客裏面有介紹)   選擇aptach的maven-archetype-webapp   填入groupIDhe ar

Spring JDBC數據庫開發

class .class play prop 用戶操作 source display auto getbean 針對數據庫操作,Spring框架提供了JdbcTemplate類。 1.Spring JDBC的配置 創建配置文件applicationContext.xml,添