Input
Output
import java.sql.*; public class MySQLJDBCExample { public static void main(String[] args) { Connection con = null; Statement stmt = null; ResultSet rs = null; try { // Step 1: Load MySQL JDBC Driver Class.forName("com.mysql.cj.jdbc.Driver"); // Step 2: Create connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/testdb", // DB URL "root", // Username "password" // Password ); // Step 3: Create statement stmt = con.createStatement(); // Step 4: Execute query rs = stmt.executeQuery("SELECT * FROM employees"); // Step 5: Process result set while (rs.next()) { System.out.println(rs.getInt("id") + " - " + rs.getString("name")); } } catch (Exception e) { e.printStackTrace(); } finally { // Step 6: Close resources try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (con != null) con.close(); } catch (SQLException ex) { ex.printStackTrace(); } } } }