
As you must be aware that createArrayOf() method from the Connection Interface will give you a java.sql.SQLFeatureNotSupportedException exception. One of the "not so ideal" ways to achieve the IN Clause in MySQL is to dynamically create the number of parameters as ? in your IN Clause.
import java.sql.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class JDBCInClauseExample {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//The elements for IN Clause
ArrayList<String> studentNames = new ArrayList<>();
studentNames.add("Harry");
studentNames.add("Ron");
//Create an array of same size
String[] arrayOfQuestionMarks = new String[studentNames.size()];
//Create a array of question marks ?
for(int i=0;i< studentNames.size();i++) {
arrayOfQuestionMarks[i] = "?";
}
//Create a string of question marks ?
String inClause = String.join(",", arrayOfQuestionMarks);
String url ="jdbc:mysql://localhost:3306/my_uat";
String userName="root";
String password ="root123";
String selectQuery ="Select * from students where student_name in ("+inClause+")";
Connection connection = DriverManager.getConnection(url,userName,password);
PreparedStatement preparedStatement = connection.prepareStatement(selectQuery);
for(int i=0;i< studentNames.size();i++) {
preparedStatement.setString(i+1, studentNames.get(i));
}
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
System.out.print(resultSet.getInt(1)+"\t");
System.out.print(resultSet.getString(2)+"\t");
System.out.print(resultSet.getString(3)+"\t");
System.out.print(resultSet.getString(4)+"\t");
System.out.println("");
}
}
}
Output
1 Harry 1997-01-03 00:00:00 London
2 Ron 1999-03-19 00:00:00 Paris
-
Have Questions? Post them here!
More Posts related to Java,
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
More Posts:
- How to change default macOS Terminal Window size - MacOS
- Java code to check Internet Connection on Android Device Programmatically - Android
- wget Command on macOS Terminal - MacOS
- How to hide or remove quick launch left navigation from SharePoint Online Modern site page - SharePoint
- [Error] Microsoft Teams: We're sorry—we've run into an issue. - Microsoft
- Toast not getting displayed Android App - Android
- Get MD5 Hash as Checksum of a String in Python - Python
- Android Emulator 5.1.1 not loading on Mac OS X Android Studio - Android-Studio