Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
21 views
5 pages
X 3
Spring
Uploaded by
tresbonarchitecte
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save x3 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
21 views
5 pages
X 3
Spring
Uploaded by
tresbonarchitecte
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save x3 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 5
Search
Fullscreen
03112023 20:31 Using a List of Values ina JabeTemplate IN Clause | Baeldung “ Using a List of Values in a JdbcTemplate IN Clause thttos //adsfreestarcom/? | writen by: Gang wu htos:/winbaeldung.com/author/gangw) Spring Data (https://www.baeldung.com/category/persistence/spring-persistence/spring-data) JDBC ihttps://www.bacldung.com/tag/jdbe) SOL (https://wwwbaeldung.com/tag/sq) Get started with Spring Data JPA through the reference Learn Spring Data JPA course: >> CHECK OUT THE COURSE (/learn-spring-data-jpa-course) y (https//ads freestarcom/? Lutm_campaign-branding&iutm .medium-banner&iutm source-baeldung.com&utm.content-baeldung.incontent 2 1. Introduction Ina SOL statement, we can use the IN operator to test whether an expression matches any value ina list. As such, \we can use the IN operator instead of multiple OR conditions, In this tutorial, wel learn how to passa list of values into the IN clause of a Spring JDBC template (/spring-idbc- jdbctemplate) query. - v hitps:vww-baeldung.com/springjdbclemplate-nist 803112023 20:31 Using a List of Values ina JabeTemplate IN Clause | Baeldung 2. Passi~¢ a [.ist Parameter to /NClause The IN operator allows us to specify multiple values in a WHERE clause. For example, we can use it to find all ‘employees whose id isin a specified id ist SELECT» FRO PLOVEE WERE 18 Gy 2 3) Oo ‘Typically, the total number of values inside the IN clause is variable, Therefore, we need to create a placeholder that can support a dynamic list of values. 2.1. With JdbcTemplate \With JabcTemplate thttps//docs springio/spring framework /does/current/javadoc~ api/org/springtramework/jdbe/core/JdbcTemplate html. we can use characters as placeholders for the lst of values, The number of characters will be the same as the size of the list IBM FlashSystem - Le stockage qui fait v la différence wv mM 1M Learn (https /ads reestarcom/? Listamployee> gett loyneFranfcliat(istcinteger> 18) ( Oo Sting iat = seengsointh Soest onesnptetiee i200, Listeteployee> exployees = jdbeTemplate-query( String. format ("SELECT * FROM EMPLOYEE WHERE 1d ZN (Hs)", JnSaQ), ds. toarray() (+5, rowum) > new EMployee(rs.gotine ("id"), rs.getstring "First none"), rs.getString(*tast_ome")))5 return enployees: In this method, we first generate a placeholder string that contains ids size" characters separated with commas \Vjava-strings-concatenation). Then we put this string into the IN clause of our SQL statement. For example. if we have throe numbers in the ids st. the SQL statornent is: SELECT + FROM ENPLOVEE WHERE 46 IN (2,2,7) ia Inthe query method, we pass the idslist as a parameter to match the placeholders inside the IN clause. This way, ‘we can execute a dynamic SQL statement based on the input list of values. 2.2. With NamedParameterJdbcTemplate Another way to handle the dynamic list of values is to use NamedParameteridibcTemplate {httpsi//does.spring.o/spring/does /current/javadoc- api/org/springframework/jdbc/core/namedparam/NamedParameteridoctemplate html. For example, we can irectly create a named parameter for the input ist: hiipsulwaewcbaeidung,comlspring-jdbctemplate-insit 2603112023 2031 Using a List of Values ina JabeTemplate IN Clause | Bacldung Listeempl sse- society aseFrddicListhaned(Listeinteger> sds) ( oO ‘SclParenetersource parameters = new MapSqlParanetersourca( "ids", ics); Listeceplayee> eaployecs + nanedidbeTenplate.query( "SELECT » FROW EMPLOYEE WHERE 4d IN (:1ds)", (75, rowum) > new EMDLoyee(rs.gotine ("id"), rs.getstring "First nore regetString(*last_same")))3 return enployees: In this method, we first construct a MapSqlParameterSource thitps://docs springlo/spring/docs/eurrent/javadoc- api/org/springframework/jdbe/core/namedparam/MapSqlParameterSource htm) object that contains the input id list. Then we only use one named parameter to represent the dynamic list of values. Under the hood, NamedParameteniaycTemplate substitutes the named parameters for the placeholders, and. Uses JabcTemplate to execute the query. IBM FlashSystem - Le stockage qui fait v la différence wy 2M BM Lea : (https /ads reestarcom/? 3. Handling a Large List When we have a large number of values in alist, we should consider alternate ways to pass them into the JdbcTemplate query. For example, the Oracle database doesnt support more than 1.000 literals in an IN clause. ‘One way to do ths is to create a temporary table for the list. However, different databases can have different ‘ways to create temporary tables. For instance, we can use the CREATE GLOBAL TEMPORARY TABLE (https://docs oracle. com /edi/B28359_01/server111/b28310 /tables003.htm#ADMINI1633) statomont to croate a temporary table in the Oracle database. Lets create a temporary table for the H database: Listcemployee> getEmployeasFromLargeldList(Listeinteger> sds) ( jdocTenplave-execuce "CREATE TEMPORARY TABLE IF NOT EXISTS emptoyee.tnp (4¢ INT NOT HULL") Listeauject(]> enployeetds = new ArrayList©>()s for (integer id: ids) { enployeetds.adé(new Object] ¢ id )s } jdocTenpLave.batchupdate(“INSERT INTO employee. tmp VALUES(?)", enploysers): us cenployee> enployees = jdbcTenplate.query( ELECT + FROM EMPLOYEE WHERE id IN (SELECT 1@ FROM enptoyee.enp)", (75, rowMum) > new EMDloyee(rs.gotIne ("id"), rsigetstring "First nace") rs.getString("last_same"))); $jdocTenplave-update("ELETE FROM employee. cmp"); return employees; Hore. we frst create a temporary table to hold all the values ofthe input ist. Then we insert the input list's values into the table. v hiipsulwaewcbaeidung,comlspring-jdbctemplate-insit a603112023 20:31 Using a List of Values ina JabeTemplate IN Clause | Baeldung “ Inour resulting SQL statement, the values in the IN clause are from the temporary table, and we avoid constructing an IN clause with a large number of placeholders. Finally. after we finish the query. we can clean up the temporary table for futuro use. 4. Conclusion Inthis article, we demonstrated how to use JalbcTemplate and NamedParameteriabcTemplate to pass a list of values for the IN clause of a SQL query, We also provided an alternate way to handle a large number of list values by using a temporary table. As always, the source code for the article is available over on GitHub {hitps://githuiscom/eugenp/tutorials/tree/master/persistence-modules/spring jdbc). Get started with Spring Data JPA through the reference Learn Spring Data JPA course: >> CHECK OUT THE COURSE (/leam-spring-data-jpa-course#table) ‘An intro to Spring Data, JPA and Transaction Semantics Details with JPA Get Persistence Right with Spring Download the E-book (/persistence-with-spring) ‘Comments are closed on this article! y htipsifwnww.bacldung,comlspring-jabctomplate-insist 4603112023 20:31 “ ‘COURSES SERIES ‘ABOUT htipsifwnww.bacldung,comlspring-jabctomplate-insist Using a List of Values ina JabeTemplate IN Clause | Baeldung 56
You might also like
JDBC
PDF
No ratings yet
JDBC
121 pages
DBMS Lab
PDF
No ratings yet
DBMS Lab
80 pages
Intro DB JDBC JPA SpringData
PDF
No ratings yet
Intro DB JDBC JPA SpringData
136 pages
DBMS Lab Manual 13-57
PDF
100% (1)
DBMS Lab Manual 13-57
45 pages
Complete JDBC Programming Part 2
PDF
100% (2)
Complete JDBC Programming Part 2
119 pages
Oracle Notes
PDF
100% (1)
Oracle Notes
110 pages
DBMS 5 To 8
PDF
No ratings yet
DBMS 5 To 8
16 pages
SQL Material
PDF
No ratings yet
SQL Material
36 pages
Oracle
PDF
No ratings yet
Oracle
109 pages
Oracle Material
PDF
No ratings yet
Oracle Material
90 pages
SQL Day3
PDF
No ratings yet
SQL Day3
5 pages
SQL and Java Connection - Complete - Version
PDF
No ratings yet
SQL and Java Connection - Complete - Version
77 pages
Lab Manual
PDF
No ratings yet
Lab Manual
74 pages
CRUD in Servlet
PDF
No ratings yet
CRUD in Servlet
21 pages
P 16
PDF
No ratings yet
P 16
19 pages
Connection Polling Transactions Result Set Types
PDF
No ratings yet
Connection Polling Transactions Result Set Types
31 pages
4 Day 4
PDF
No ratings yet
4 Day 4
21 pages
Untitled
PDF
No ratings yet
Untitled
18 pages
DR Chen Database
PDF
No ratings yet
DR Chen Database
26 pages
CRUD in Servlet
PDF
No ratings yet
CRUD in Servlet
21 pages
My Bat Is Procedures
PDF
No ratings yet
My Bat Is Procedures
19 pages
Advanced Java (1-7)
PDF
No ratings yet
Advanced Java (1-7)
70 pages
Workshop04 PRJ321 Tran PDF
PDF
No ratings yet
Workshop04 PRJ321 Tran PDF
16 pages
50 Most Useful SQL Queriesx
PDF
No ratings yet
50 Most Useful SQL Queriesx
46 pages
Client Server Technology
PDF
No ratings yet
Client Server Technology
16 pages
P 14
PDF
No ratings yet
P 14
11 pages
Advanced Java Practical Solutions
PDF
No ratings yet
Advanced Java Practical Solutions
33 pages
Document
PDF
No ratings yet
Document
21 pages
Expeiment 3: Queries in SQL: Program To Create Database
PDF
No ratings yet
Expeiment 3: Queries in SQL: Program To Create Database
18 pages
Z 18
PDF
No ratings yet
Z 18
7 pages
Z 32
PDF
No ratings yet
Z 32
9 pages
SQL Project File
PDF
No ratings yet
SQL Project File
16 pages
MY SQL Project File For School
PDF
No ratings yet
MY SQL Project File For School
16 pages
Experiment:-1: Introduction About SQL
PDF
No ratings yet
Experiment:-1: Introduction About SQL
19 pages
B 3
PDF
No ratings yet
B 3
12 pages
B 2
PDF
No ratings yet
B 2
12 pages
B 5
PDF
No ratings yet
B 5
12 pages
Oose 7TH Ex
PDF
No ratings yet
Oose 7TH Ex
13 pages
Z 9
PDF
No ratings yet
Z 9
8 pages
2
PDF
No ratings yet
2
15 pages
JDBC Part-3
PDF
No ratings yet
JDBC Part-3
13 pages
Java JDBC Project Workshop
PDF
No ratings yet
Java JDBC Project Workshop
15 pages
P 18
PDF
No ratings yet
P 18
11 pages
S 6
PDF
No ratings yet
S 6
11 pages
S 3
PDF
No ratings yet
S 3
12 pages
Sample Queries
PDF
No ratings yet
Sample Queries
15 pages
JDBC Slips Answer
PDF
No ratings yet
JDBC Slips Answer
16 pages
B 1
PDF
No ratings yet
B 1
9 pages
Z 14
PDF
No ratings yet
Z 14
9 pages
Z 12
PDF
No ratings yet
Z 12
10 pages
Database Management System
PDF
No ratings yet
Database Management System
8 pages
Notes
PDF
No ratings yet
Notes
10 pages
Sample Queries
PDF
No ratings yet
Sample Queries
11 pages
Sa
PDF
No ratings yet
Sa
8 pages
S 4
PDF
No ratings yet
S 4
8 pages
FSD Unit5
PDF
No ratings yet
FSD Unit5
8 pages
S 8
PDF
No ratings yet
S 8
7 pages
Z 31
PDF
No ratings yet
Z 31
7 pages
Z 27
PDF
No ratings yet
Z 27
7 pages
Z 30
PDF
No ratings yet
Z 30
7 pages
Z34
PDF
No ratings yet
Z34
7 pages
Z 26
PDF
No ratings yet
Z 26
7 pages
Z 24
PDF
No ratings yet
Z 24
7 pages
Z 17
PDF
No ratings yet
Z 17
7 pages
Update Command
PDF
No ratings yet
Update Command
7 pages
B 2
PDF
No ratings yet
B 2
8 pages
B 9
PDF
No ratings yet
B 9
7 pages
Sample Queries
PDF
No ratings yet
Sample Queries
7 pages
Practical 6
PDF
No ratings yet
Practical 6
24 pages
B 8
PDF
No ratings yet
B 8
6 pages
Z 16
PDF
No ratings yet
Z 16
6 pages
Z 19
PDF
No ratings yet
Z 19
6 pages
Lab File Format
PDF
No ratings yet
Lab File Format
4 pages
SqlServer - Parameterized Query With in Clause C# - CodeProject
PDF
No ratings yet
SqlServer - Parameterized Query With in Clause C# - CodeProject
5 pages
P.Teja - 84 Java9th Program
PDF
No ratings yet
P.Teja - 84 Java9th Program
4 pages
B 5
PDF
No ratings yet
B 5
6 pages
Oracle Forms 10g - Demos, Tips and Techniques
PDF
No ratings yet
Oracle Forms 10g - Demos, Tips and Techniques
17 pages
JAVA 2 Connectioncode
PDF
No ratings yet
JAVA 2 Connectioncode
6 pages
CODIGO
PDF
No ratings yet
CODIGO
12 pages
JDBC Project Preparation
PDF
No ratings yet
JDBC Project Preparation
2 pages
Practical No 20
PDF
No ratings yet
Practical No 20
3 pages
Update Result Set
PDF
No ratings yet
Update Result Set
2 pages
Update Result Set
PDF
No ratings yet
Update Result Set
2 pages
Update Result Set
PDF
No ratings yet
Update Result Set
2 pages
CallableStatement Interface PDF
PDF
No ratings yet
CallableStatement Interface PDF
2 pages
Java JDBC PreparedStatement Example - HowToDoInJava
PDF
No ratings yet
Java JDBC PreparedStatement Example - HowToDoInJava
1 page
JPR Manual Answers Experiment 27
PDF
No ratings yet
JPR Manual Answers Experiment 27
2 pages
Code
PDF
No ratings yet
Code
8 pages