33 lines
1.0 KiB
Java
33 lines
1.0 KiB
Java
package de.swingbe.postgres_java;
|
|
|
|
import java.sql.*;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
public class JavaPostgreSqlListTables {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
//connection URL for the postgres database
|
|
//jdbc:postgresql://<host>:<port>/<database name>
|
|
String url = "jdbc:postgresql://localhost:5432/testdb";
|
|
|
|
String user = "usr";
|
|
String password = "#password";
|
|
|
|
String query = "SELECT table_name FROM information_schema.tables " + "WHERE table_schema = 'public'";
|
|
|
|
try (Connection con = DriverManager.getConnection(url, user, password); PreparedStatement pst = con.prepareStatement(query); ResultSet rs = pst.executeQuery()) {
|
|
|
|
while (rs.next()) {
|
|
|
|
System.out.println(rs.getString(1));
|
|
}
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
Logger lgr = Logger.getLogger(JavaPostgreSqlListTables.class.getName());
|
|
lgr.log(Level.SEVERE, ex.getMessage(), ex);
|
|
}
|
|
}
|
|
} |