0

I want to create a database PostgreSQL using Java but I got an error message.

Here is my code :

try {
                Connection c=null;
                Statement stmt=null;
                Class.forName("org.postgresql.Driver");
                
                c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5432/",
            "postgres", "Admin@2014");
                
                c.setAutoCommit(false);
                System.out.println("Opened database successfully");
               
                stmt = c.createStatement();
                
               String sql = "CREATE DATABASE db OWNER postgres TABLESPACE numerique; ";
              
                stmt.executeUpdate(sql);    
                
             stmt.close();
                c.commit();
                c.close();
                
            } catch (Exception ee) {
                    System.err.println( ee.getClass().getName()+": "+ ee.getMessage() );
                    System.exit(0);
            }

Here is the error message that I got :

org.postgresql.util.PSQLException: ERREUR: CREATE DATABASE ne peut pas être exécuté dans un bloc de transaction

Thank you for your help.

2
  • What exactly are you trying i.e. the code Commented Mar 6, 2014 at 11:12
  • Your question is very badly written and worded. No code examples, no additional information on your setup, the use of "plz"... Have a look at stackoverflow.com/help/asking first and edit your question please. Commented Mar 6, 2014 at 11:27

2 Answers 2

2

Looks like you are trying to create the database inside a transaction, and that is not allowed.

CREATE DATABASE cannot be executed inside a transaction block.

See http://www.postgresql.org/docs/9.1/static/sql-createdatabase.html

But some code sample from your side would help :)

Sign up to request clarification or add additional context in comments.

Comments

1

Remove c.setAutoCommit(false); and c.commit(); from your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.