Re: Use Driver to Create database?

From: Brendon Bentley <bfbentley(at)gmail(dot)com>
To: "PostgreSQL JDBC" <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Use Driver to Create database?
Date: 2006-12-05 12:27:32
Message-ID: 457565B4.80301@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

How to create database with JDBC on PostgreSQL RDMS:

For a complete example that works:

//-- Note that this string ends on "postgres". That is
// the name of the default database that can always
// be connected to.
String dbUrl = "jdbc:postgresql://localhost:8988/postgres";
String dbDriver = "org.postgresql.Driver";
String dbUsername = "Db_test_client";
String dbPassword = "Db_test_pwd";
try { //-- Checks that database driver is loaded
Class.forName(dbDriver);
} catch (ClassNotFoundException ex) {
throw new DbWriteException( ex );
}
try {
//-- Connect to database
Connection dbConnection =
DriverManager.getConnection(dbUrl,dbUsername,dbPassword);

//--Make SURE auto commit is on, because you cannot
// add a database to a transaction block. A trans-
// action block is a collection of SQL statements
// that are only executed when dbConnection.commit()
// is called.
dbConnection.setAutoCommit(true);
String sql = "CREATE DATABASE testDatabase";
Statement cs = dbConnection.createStatement();
cs.execute(sql);
cs.close();
dbConnection.close();

//-- If this code still fails your login account probably
// does not have privileges to create databases

On 2006-11-19 21:42, Roland Walter wrote:
> Charlie Kelly wrote:
> > Is it possible to use the driver to create a new database (inside a
java
> > program),
> > or is it necessary to first create the database using the createdb
utility?
> >
> > If it is possible to use the driver, what is the correct syntax?
> >
>
> There is a SQL-command for that, look here:
>
>
> http://www.postgresql.org/docs/8.1/interactive/sql-createdatabase.html
>
> First connect to the postgres or template1 schema of the cluster, that
> you created with initdb. Execute the "CREATE DATABASE". Reconnect to the
> database you created.
>
> Regards,
> Roland.
> - --
> Dipl.-Phys. Roland Walter
> mailto: roland (dot)walter (dot) rwa (at) gmx (dot) net
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: You can help support the PostgreSQL project by donating at
>
> http://www.postgresql.org/about/donate
>

Browse pgsql-jdbc by date

  From Date Subject
Next Message Peter Eisentraut 2006-12-06 13:58:31 Something wrong with binding timestamp values
Previous Message Wolfgang Rinnert 2006-12-04 18:46:26 Re: web-app not working after upgrade 7.4 to 8.1