4

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");
        }
    }
}
juergen d
  • 201,996
  • 37
  • 293
  • 362
Ariel Grabijas
  • 1,472
  • 5
  • 25
  • 45
  • 2
    Change `catch(SQLException e) {` to `catch(SQLException e) { e.printStackTrace();` and copy/paste the output. – Andrew Thompson May 18 '12 at 12:07
  • 1
    Some databases do not support the IF NOT EXISTS construction. – W. Goeman May 18 '12 at 12:08
  • You get a SQLException if there is a problem with the SQL command you're tring to run - and it doesn't match the syntax of any version of SQL I know so that's possible! – Bridge May 18 '12 at 12:08
  • Should the IF NOT EXISTS be in square brackets? – Ewald May 18 '12 at 12:10
  • org.postgresql.util.PSQLException: ERROR: error in or close to "[" Position: 14 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags – Ariel Grabijas May 18 '12 at 12:14
  • Ewald it should as far as I know – Ariel Grabijas May 18 '12 at 12:29

2 Answers2

4

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()

Daniel Vérité
  • 58,074
  • 15
  • 129
  • 156
0

try

IF NOT EXISTS (SELECT 1 
           FROM sysobjects 
           WHERE xtype='u' AND name='testowatabela2')
    CREATE TABLE testowatabela2 (id INTEGER, wartosc DOUBLE PRECISION)

Edit

Just leave the [IF NOT EXISTS] part. According to this answer it does not matter if the table exists or not.

Community
  • 1
  • 1
juergen d
  • 201,996
  • 37
  • 293
  • 362
  • 1
    @Noran: This syntax won't work in PostgreSQL so don't bother trying (juergen added it before your question was tagged with PostgreSQL) –  May 18 '12 at 15:03