Advertisement
STANAANDREY

pgsql con

Aug 16th, 2021 (edited)
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. package com.company;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6. import java.util.Properties;
  7.  
  8. public class Main {
  9.     public static void main(String[] args) throws Exception {
  10.         try {
  11.             String url = "jdbc:postgresql://localhost/mydb1";
  12.             var props = new Properties();
  13.             props.setProperty("user", "postgres");
  14.             props.setProperty("password", "postgres_password");
  15.             props.setProperty("ssl", "allow");
  16.             Connection con = DriverManager.getConnection(url, props);
  17.             Statement stmt = con.createStatement();
  18.             ResultSet rs = stmt.executeQuery("SELECT * FROM users");
  19.             while (rs.next()) {
  20.                 int id = rs.getInt("id");
  21.                 var name = rs.getString("name");
  22.                 System.out.printf("%d %s\n", id, name);
  23.             }
  24.  
  25.         } catch (Exception e) {
  26.             System.out.println(e.getMessage());
  27.             System.out.println("error");
  28.         }
  29.  
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement