
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
-
Have Questions? Post them here!
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- SharePoint Designer Workflow error - Coercion Failed: Input cannot be null for this coercion - SharePoint
- Show Chrome Developer Console Keyboard Shortcut on macOS - Chrome
- Change Sublime Text 3 white background color theme - Sublime-Text
- How to create SharePoint List Item programmatically using C#.net - SharePoint
- Remove AirDrop Icon from macOS Menu Bar - MacOS
- ADT quit unexpectedly error on Mac OSX Android Eclipse SDK - Android
- A Guide to Dynamically Obtaining Browser Screen Width and Height with jQuery [Updated 2023] - jQuery
- How to ignore files in git using .gitignore file - Git