In this Tutorial, we will take a look at how to write in Simple Java SE JDBC a prepared Select Parameterized Select,
Table:CREATE TABLE `students` (
`student_id` int NOT NULL AUTO_INCREMENT,
`student_name` varchar(45) NOT NULL,
`student_dob` datetime NOT NULL,
`student_address` varchar(45) NOT NULL,
PRIMARY KEY (`student_id`,`student_dob`);
Data in the table:
select * from students;
+------------+--------------+---------------------+-----------------+
| student_id | student_name | student_dob | student_address |
+------------+--------------+---------------------+-----------------+
| 1 | Mike | 2001-07-22 00:00:00 | New York City |
| 2 | Alex | 2002-08-13 00:00:00 | Chicago |
| 3 | Sam | 2022-01-25 00:00:00 | Ohio |
+------------+--------------+---------------------+-----------------+
Java Example:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Java SE JDBC Parameterized Select
* Query example.
*/
public class JavaJDBCParameterizedSelectExample {
private static final String MYSQL_JDBC_DRIVER_CLASS = "com.mysql.cj.jdbc.Driver";
private static final String MYSQL_DB_URL = "jdbc:mysql://localhost:3306/my_uat";
private static final String MYSQL_DB_USER = "root";
private static final String MYSQL_DB_USER_PASSWORD = "root123";
private static final String SQL_PARAMETERIZED_QUERY =
"SELECT * from students where student_name=? or student_address=?";
private static String studentName="Mike";
private static String studentAddress="Chicago";
public static void main(String[] args) {
try(Connection connection = DriverManager.getConnection(MYSQL_DB_URL,MYSQL_DB_USER,MYSQL_DB_USER_PASSWORD)) {
try {
Class.forName(MYSQL_JDBC_DRIVER_CLASS);
} catch (ClassNotFoundException e) {
System.out.println("Database Vendor Driver class not found!");
e.printStackTrace();
}
PreparedStatement statement = connection.prepareStatement(SQL_PARAMETERIZED_QUERY);
statement.setString(1, studentName);
statement.setString(2, studentAddress);
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()) {
System.out.println(resultSet.getInt(1)+" "+
resultSet.getString(2)+" "+
resultSet.getDate(3)+" "+
resultSet.getString(4));
}
} catch (SQLException e) {
System.out.println("Error occured while executing query: " + SQL_PARAMETERIZED_QUERY);
e.printStackTrace();
}
}
}
Output:
1 Mike 2001-07-22 New York City
2 Alex 2002-08-13 Chicago
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- Find Nearest Gas Station using Google Map App on your Phone - Google
- Fix: ValueError: operands could not be broadcast together with shapes [Python numpy] - Python
- Fix: AssertionError in Python - Python
- Encode or Decode Base64 String using Mac Terminal Command - MacOS
- [Solved] SharePoint Illegal operation attempted on a registry key that has been marked for deletion - SharePoint
- Update Powershell Using Command Line - Powershell
- How to install python pip on Ubuntu using apt command - Linux
- Fix - sudo: systemctl: command not found - Ubuntu