jdbc:postgresqlでHello World

JDK 17

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class PGHelloWorld {

    public static void main(String[] args) {
        try (Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "username", "password");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from test1")) {
            if (rs.next()) {
                System.out.println(rs.getString(1));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

実行結果

Hello World

 

投稿日: