SQLexception when a table exists

I want to enter a command with JDBC to create a table, but after the first compilation, when the table has already been generated, each next one throws an exception. I don’t understand how this is possible, because I put [IF NOT EXISTS] 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"); } } } 
+4
source share
2 answers

The brackets containing the IF NOT EXISTS clause are probably listed in the transcript from the documentation: http://www.postgresql.org/docs/9.1/static/sql-createtable.html

but they should not be present in the actual CREATE TABLE expression, since the brackets mean that the clause is optional .

Another thing to keep in mind is that IF NOT EXISTS is a new feature of PostgreSQL 9.1, so it does not cope with older versions. If you are not sure about the version you are using, run in SQL: select version()

+4
source

to 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] . According to this answer , it doesn't matter if the table exists or not.

0
source

Source: https://habr.com/ru/post/1413154/


All Articles