Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.Properties;
- public class Main {
- public static void main(String[] args) throws Exception {
- try {
- String url = "jdbc:postgresql://localhost/mydb1";
- var props = new Properties();
- props.setProperty("user", "postgres");
- props.setProperty("password", "postgres_password");
- props.setProperty("ssl", "allow");
- Connection con = DriverManager.getConnection(url, props);
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM users");
- while (rs.next()) {
- int id = rs.getInt("id");
- var name = rs.getString("name");
- System.out.printf("%d %s\n", id, name);
- }
- } catch (Exception e) {
- System.out.println(e.getMessage());
- System.out.println("error");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement