Java JDBC PostgreSQL Connection getListDatabaseFromMaster()

Here you can find the source of getListDatabaseFromMaster()

Description

get List Database From Master

License

Open Source License

Declaration

public static List<String> getListDatabaseFromMaster() 

Method Source Code


//package com.java2s;
/*/* w  ww.  j  a v a 2 s  .  com*/
 * Copyright (C) 2016 Jo?o Paulo
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Main {
    private static String driver = "org.postgresql.Driver";
    private static String user = "postgres";
    private static String senha = "postgres";
    private static String url = "jdbc:postgresql://localhost:5432/master";

    public static List<String> getListDatabaseFromMaster() { //retorna os nomes dos BDs
        List<String> lista = new ArrayList<>();
        Connection connection;
        try {
            Class.forName(driver);
            connection = (Connection) DriverManager.getConnection(url, user, senha);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT name FROM public.system_database;");
            while (resultSet.next()) {
                lista.add(resultSet.getString("name"));
            }
            resultSet.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            System.err.print("[ERRO getListDatabaseFromMaster()] " + e);
        }
        return lista;
    }
}

Related

  1. createConnection()
  2. getConnection()
  3. getConnection()
  4. getDataFromTable(String tableName, String[] columns)
  5. getDBConnection()
  6. getPostgresConnection()
  7. getPostgresConnection()
  8. getPostgresJDBCConnection(String host, int port, String nameDB, String userDB, String passwordDB)
  9. getPostgresSQLConnection(String hostname, int portNumber, String databaseName, String dbuser, String dbpasswort)