Query Existence of PSQL Table

Asked

Viewed 54 times

2

Well, I searched many places until I found the answer to my problem and I found interesting the idea of posting here what I found to help Unity, in case someone needs to perform a similar CRUD, Performing a select in Resultset to know if it exists or not. If there is no create table, if there are rs.next(); to make changes, add column and such. This was the way I found for a system that needs constant updates and sometimes insert new columns in tables already created.

this is a migration of a non-DAO Pattern class with DBMS Firebird to Postgresql

I hope somehow that can help. If there is a more succinct way to write this method I ask you to comment, I have performed many tests in various ways to get here.

private static void HOTEIS_QUARTOS(){        
    try
    {            
        try (Connection conn = Class_Conexao.GetConnection();                  
            Statement stmt = conn.createStatement(); 
            ResultSet rs = stmt.executeQuery("SELECT EXISTS (SELECT * FROM pg_tables WHERE schemaname = 'public' AND tablename = 'hoteis_hospedagens')")) { 
            if(rs.next())
            {                        
                stmt.executeUpdate("CREATE TABLE if not exists HOTEIS_QUARTOS( "
                                + "HOTEL_QUARTO_ID SERIAL PRIMARY KEY, "
                                + "HOTEL_QUARTO_NUMERO VARCHAR(50) NOT NULL, "
                                + "HOTEL_QUARTO_DESCRICAO VARCHAR(500) NOT NULL, "
                                + "HOTEL_QUARTO_CAMA_CASAL INT default '0' NOT NULL,"                     
                                + "HOTEL_QUARTO_CAMA_SOLTEIRO INT default '0' NOT NULL,"                     
                                + "HOTEL_QUARTO_VALOR_DIARIA NUMERIC default '0.00' NOT NULL,"                     
                                + "HOTEL_QUARTO_STATUS INT default '1' NOT NULL)");                    
                System.out.println("TABELA HOTEIS_QUARTOS CRIADA");                    
            }                
            if(rs.isClosed()){
                stmt.execute("ALTER TABLE HOTEIS_QUARTOS ADD COLUMN if not exists HOTEIS_QUARTO_NUMERO VARCHAR(50) NOT NULL");              
                stmt.execute("ALTER TABLE HOTEIS_QUARTOS ADD COLUMN if not exists HOTEIS_QUARTO_CAMA_CASAL INT default '0' NOT NULL");                
                stmt.execute("ALTER TABLE HOTEIS_QUARTOS ADD COLUMN if not exists HOTEIS_QUARTO_CAMA_SOLTEIRO INT default '0' NOT NULL");              
                stmt.execute("ALTER TABLE HOTEIS_QUARTOS ADD COLUMN if not exists HOTEIS_QUARTO_DIARIA NUMERIC default '0.00' NOT NULL");              
                stmt.execute("ALTER TABLE HOTEIS_QUARTOS ADD COLUMN if not exists HOTEIS_QUARTO_STATUS INT default '1' NOT NULL");
                System.out.println("ALTERAR TABELA HOTEIS quartos"); 
                System.out.println("---------------------------------");
            }                
        }
    }
  • 1

    In this case, apparently you had a problem and you found a solution, and so you wanted to share it here, correct? In that case, the correct thing would be for you to post your problem (and only it) as a question and then answer it as if you were answering a question that you know the answer to

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.