In this Tutorial, we will take a look at how to execute a Select Statement (Read part of CRUD Operation) with the help of Java SE JDBC. We have used the MySQL Driver but you can use any other like Oracle, PostgreSQL, or, MS SQL.
Step 1: Create Database Table
Create Statement:CREATE TABLE `student` (
`student_id` int NOT NULL,
`student_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`student_id`);
Step 2: Insert Some Records to Database Table
Insert Queries:insert into student values(1,"Alex");
insert into student values(2,"Luke");
insert into student values(3,"Mike");
Step 3: Declaring JDBC Configuration Details:
private static String MYSQL_JDBC_DRIVER_CLASS = "com.mysql.cj.jdbc.Driver";
private static String MYSQL_DB_URL = "jdbc:mysql://localhost:3306/my_uat";
private static String MYSQL_DB_USER = "root";
private static String MYSQL_DB_USER_PASSWORD = "root123";
private static String SQL_QUERY = "Select * rom student";
Note that all these parameters may differ based on your setup
❗️Make sure to add the connector jar to your classpath!
Complete Code Exampleimport java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Java EE JDBC Select Query Example
*/
public class JavaJDBCExample {
private static String MYSQL_JDBC_DRIVER_CLASS = "com.mysql.cj.jdbc.Driver";
private static String MYSQL_DB_URL = "jdbc:mysql://localhost:3306/my_uat";
private static String MYSQL_DB_USER = "root";
private static String MYSQL_DB_USER_PASSWORD = "root123";
//SQL Select Statement Query Example with Java JDBC
private static String SQL_QUERY = "Select * from student";
public static void main(String[] args) {
try(Connection connection = DriverManager.getConnection(MYSQL_DB_URL,MYSQL_DB_USER,MYSQL_DB_USER_PASSWORD)) {
Class.forName(MYSQL_JDBC_DRIVER_CLASS);
Statement statement =connection.createStatement();
ResultSet resultSet = statement.executeQuery(SQL_QUERY);
while(resultSet.next()) {
System.out.println(resultSet.getInt(1)+" "+resultSet.getString(2));
}
} catch (ClassNotFoundException e) {
System.out.println("MySQL Driver class not found!");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Error occured while executing query: " + SQL_QUERY);
e.printStackTrace();
}
}
}
Output:
1 Alex 2 Luke 3 Mike
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:
- list of jars required for hibernate 4.x.x - Java
- How to Show Line Numbers in Google Colab Cells - Google
- Fix: sudo: unable to open Read-only file system - Linux
- Enable macOS Stage Manager - MacOS
- Change CSS Background Opacity with Examples - CSS
- How to Open Finder using Mac Terminal - MacOS
- Python: Split a String into a Dictionary - Python
- How to fix: You will need Google Chrome to install most apps, extensions and themes. - Chrome