JDBC Visualiser
Step 1 / 9
JDBCDemo.javaConnection Constants
1import java.sql.Connection;
2import java.sql.DriverManager;
3import java.sql.ResultSet;
4import java.sql.SQLException;
5import java.sql.Statement;
6
7public class JDBCDemo {
8
9// Constants — private, static, final, UPPER_CASE (industry standard)
10private static final String URL = "jdbc:mysql://localhost:3306/jdbcfeb";
11private static final String USERNAME = "root";
12private static final String PASSWORD = "root";
13
14public static void main(String[] args) {
15
16// Declare outside try so finally block can access them
17Connection con = null;
18Statement stmt = null;
19ResultSet rs = null;
20
21try {
22// Step 1 — Load the Driver Class
23Class.forName("com.mysql.cj.jdbc.Driver");
24System.out.println("Driver is loaded");
25
26// Step 2 — Establish the Connection
27con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
28System.out.println("Connection established");
29
30// Step 3 — Create a Statement
31stmt = con.createStatement();
32System.out.println("Statement is created");
33
34// Step 4 — Execute the Query (SELECT)
35rs = stmt.executeQuery("SELECT * FROM employee");
36System.out.println("Query executed");
37
38// Step 5 — Process the Result
39System.out.printf("%-5s %-12s %-20s %-12s %-10s%n",
40"ID", "Name", "Email", "Department", "Salary");
41System.out.println("--------------------------------------------------------------");
42
43while (rs.next()) {
44int id = rs.getInt("id");
45String name = rs.getString("name");
46String email = rs.getString("email");
47String department = rs.getString("department");
48int sal = rs.getInt("salary");
49
50System.out.printf("%-5d %-12s %-20s %-12s %-10d%n",
51id, name, email, department, sal);
52}
53
54} catch (ClassNotFoundException e) {
55System.out.println("Driver not found: " + e.getMessage());
56} catch (SQLException e) {
57System.out.println("Problem: " + e.getMessage());
58
59} finally {
60// Close in REVERSE order: rs → stmt → con
61try {
62if (rs != null) rs.close();
63if (stmt != null) stmt.close();
64if (con != null) con.close();
65System.out.println("Resources closed");
66} catch (SQLException e) {
67e.printStackTrace();
68}
69}
70}
71}
// Console output will appear here as steps execute
JDBC Flow DiagramDefine URL, USERNAME, PASSWORD
Initial state — click Next to beginJJava AppJDBCDemo.javaDriverManagerConnection factoryDriver / JDBCMySQL BridgeConnectionOpen tunnelStatementSQL messengerMySQLjdbcfeb DBURL = jdbc:mysql://localhost:3306/jd…Java / JDBCDatabaseData flowPreparedStmt
1/9

Connection Constants

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.