
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
-
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:
- [Fix] Microsoft 53003 Error - Microsoft
- Setting $JAVA_HOME Environment Variable in macOS - MacOS
- How to add hint text in bootstrap input text field and text area - Bootstrap
- Unzip a Zip file from Terminal Command - HowTos
- 12 August - International Youth Day celebrated worldwide - News
- Enable Dark Mode in Google Search - Google
- Build-in Snipping Tool Alternative on Mac - MacOS
- 21 Useful Android Emulator Short-cut Keyboard Keys - Android