| 1 | import java.sql.Connection; |
| 2 | import java.sql.DriverManager; |
| 3 | import java.sql.ResultSet; |
| 4 | import java.sql.SQLException; |
| 5 | import java.sql.Statement; |
| 6 | |
| 7 | public class JDBCDemo { |
| 8 | |
| 9 | ▶// Constants — private, static, final, UPPER_CASE (industry standard) |
| 10 | ▶private static final String URL = "jdbc:mysql://localhost:3306/jdbcfeb"; |
| 11 | ▶private static final String USERNAME = "root"; |
| 12 | ▶private static final String PASSWORD = "root"; |
| 13 | |
| 14 | public static void main(String[] args) { |
| 15 | |
| 16 | // Declare outside try so finally block can access them |
| 17 | Connection con = null; |
| 18 | Statement stmt = null; |
| 19 | ResultSet rs = null; |
| 20 | |
| 21 | try { |
| 22 | // Step 1 — Load the Driver Class |
| 23 | Class.forName("com.mysql.cj.jdbc.Driver"); |
| 24 | System.out.println("Driver is loaded"); |
| 25 | |
| 26 | // Step 2 — Establish the Connection |
| 27 | con = DriverManager.getConnection(URL, USERNAME, PASSWORD); |
| 28 | System.out.println("Connection established"); |
| 29 | |
| 30 | // Step 3 — Create a Statement |
| 31 | stmt = con.createStatement(); |
| 32 | System.out.println("Statement is created"); |
| 33 | |
| 34 | // Step 4 — Execute the Query (SELECT) |
| 35 | rs = stmt.executeQuery("SELECT * FROM employee"); |
| 36 | System.out.println("Query executed"); |
| 37 | |
| 38 | // Step 5 — Process the Result |
| 39 | System.out.printf("%-5s %-12s %-20s %-12s %-10s%n", |
| 40 | "ID", "Name", "Email", "Department", "Salary"); |
| 41 | System.out.println("--------------------------------------------------------------"); |
| 42 | |
| 43 | while (rs.next()) { |
| 44 | int id = rs.getInt("id"); |
| 45 | String name = rs.getString("name"); |
| 46 | String email = rs.getString("email"); |
| 47 | String department = rs.getString("department"); |
| 48 | int sal = rs.getInt("salary"); |
| 49 | |
| 50 | System.out.printf("%-5d %-12s %-20s %-12s %-10d%n", |
| 51 | id, name, email, department, sal); |
| 52 | } |
| 53 | |
| 54 | } catch (ClassNotFoundException e) { |
| 55 | System.out.println("Driver not found: " + e.getMessage()); |
| 56 | } catch (SQLException e) { |
| 57 | System.out.println("Problem: " + e.getMessage()); |
| 58 | |
| 59 | } finally { |
| 60 | // Close in REVERSE order: rs → stmt → con |
| 61 | try { |
| 62 | if (rs != null) rs.close(); |
| 63 | if (stmt != null) stmt.close(); |
| 64 | if (con != null) con.close(); |
| 65 | System.out.println("Resources closed"); |
| 66 | } catch (SQLException e) { |
| 67 | e.printStackTrace(); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
Connection Constants
These are the connection details your Java program needs to find and open your database. private means only this class can see them. static means one shared copy for the whole class. final means they never change. UPPER_CASE is the Java naming convention for constants (like Math.PI). The URL format jdbc:mysql://localhost:3306/jdbcfeb tells JDBC the protocol (jdbc), the vendor (mysql), the host (localhost), the port (3306), and the database name (jdbcfeb).
Constants are defined once and shared — never hardcode credentials inline.