Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hello 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);
            }

and 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 you're help

share|improve this question
    
What exactly are you trying i.e. the code –  Mark Mar 6 '14 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. –  DrColossos Mar 6 '14 at 11:27

2 Answers 2

up vote 0 down vote accepted

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.