← Back to feed
Renewal·서른의 생활코딩

ex) Java JDBC - Database Connection Example

NS
normalstory
cover image

// Java JDBC Database Connection Example
// Demonstrates connecting to a database using JDBC driver
// Executing SQL queries and processing ResultSet

import java.sql.*;

public class JDBCExample {
  public static void main(String[] args) {
    try {
      // 1. Load the JDBC driver
      Class.forName("oracle.jdbc.driver.OracleDriver");
      
      // 2. Establish connection
      Connection conn = DriverManager.getConnection(
        "jdbc:oracle:thin:@localhost:1521:xe", "scott", "tiger");
      
      // 3. Create statement
      Statement stmt = conn.createStatement();
      
      // 4. Execute query and get results
      ResultSet rs = stmt.executeQuery("SELECT * FROM emp");
      
      // 5. Process results
      while(rs.next()) {
        System.out.println(rs.getString("ename"));
      }
      
      // 6. Close resources (reverse order)
      rs.close();
      stmt.close();
      conn.close();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.