SQLexception when table exists

database, java, jdbc, postgresql, sql

Solution

Probably, the brackets enclosing the `IF NOT EXISTS` clause come verbatim from the documentation: http://www.postgresql.org/docs/9.1/static/sql-createtable.html

but they must not be present in an actual `CREATE TABLE` statement, since the brackets mean that the clause is optional.

Another thing to consider is that `IF NOT EXISTS` is a new feature of PostgreSQL 9.1, so it would fail with older versions. If you're not sure of the version you're using, run in SQL: `select version()`

Problem

I want to type a command with JDBC to create a table, but after first compilation, when table is already generated, every next one throws exception. I dont understand how is this possible, because I've put [IF NOT EXISTS] term there, so there should be no SQL error. ``` public class Test { public static void main(String[] args) { try { Connection conn = BazaDanych.Polacz(); Statement stat = conn.createStatement(); String command = "CREATE TABLE [IF NOT EXISTS] testowatabela2 (id INTEGER, wartosc DOUBLE PRECISION);"; stat.execute(command); } catch(SQLException e) { System.out.println("SQL Exception in Test"); } } } ```

Original source

Related problems