By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

create database if not exists

P: 7
Someone know how to create database in postgreSQL through Java code?
How to check particular database exist or not in postgreSQL through java code?
Apr 22 '07 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 534
The query you need to execute is not depending on the client API, whether it is Java, C, or anything else.
Look at this thread for details.

It may be also helpful to review documentation on Create database and Information Schema
Apr 22 '07 #2

P: 7
thanks for reply.
but i want to create database dynamically through jdbc .
in java to get connected we have to enter database name in getconnection() method.

[ Connection con = DriverManager.getConnection("jdbc:postgresql://IpAddress:5432/" + databasename, dbusername, dbpassword); ]

but sice we don't have any database how to? & where to get connected?
how to create database at run time??
Apr 23 '07 #3

Expert 100+
P: 534
Typically you would use external utility createdb, but if you want to do it in Java, first time you can connect to template1, but once you have created some database you can connect to it and create new ones.

See more details here
Apr 23 '07 #4

P: 7
hi
thanks ur soln worked.
but How to check particular database exist or not in postgreSQL through java code?
Apr 23 '07 #5

P: 7
hi
thanks ur soln worked.
but How to check particular database exist or not in postgreSQL through java code? i.e. how to compare two databases names and structure?
Apr 23 '07 #6

Expert 100+
P: 534
hi
thanks ur soln worked.
but How to check particular database exist or not in postgreSQL through java code? i.e. how to compare two databases names and structure?
I'm not sure what "ur soln" means...
To get the list of the databases you can run this query:

Expand|Select|Wrap|Line Numbers
  1. select datname from pg_catalog.pg_database;
  2.  
if all you need is to find out whether a particular database exists you can modify this query a bit, something like this will work:

Expand|Select|Wrap|Line Numbers
  1. select count(*) from pg_catalog.pg_database where datname = 'some_name';
  2.  
I think there are some third-party tools that may help you with comparing databases, I don't have any specific pointers, but if you spend some time with Google you will find few.

If you don't want to employ any third-party tools you need to define exactly what you mean by "comparing the database structures"
Does this include just a list of tables, or also other objects, such as views, functions, triggers, rules, etc.

As I mentioned earlier studying the manual on Information schema may be very helpful to you.
Apr 23 '07 #7

P: 1
thanks for reply.
but i want to create database dynamically through jdbc .
in java to get connected we have to enter database name in getconnection() method.

[ Connection con = DriverManager.getConnection("jdbc:postgresql://IpAddress:5432/" + databasename, dbusername, dbpassword); ]

but sice we don't have any database how to? & where to get connected?
how to create database at run time??
prepare a class name and do the follow code

//public final static String DRIVER = "com.mysql.jdbc.Driver";
//public final static String URL = "jdbc:mysql://localhost/IA";
//public final static String USER = "";
//public final static String PASSWORD = "";

private static Properties dbprops = new Properties();
private static Driver myDriver = null;
private static Connection connection = null;
private static Statement statement1 = null;
public static void init() {

dbprops.put("user", USER);
dbprops.put("password", PASSWORD);
try
{
myDriver = (Driver)Class.forName(DRIVER).newInstance();
connection = DriverManager.getConnection(URL, dbprops);
try
{
Statement st = connection.createStatement();
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String database = DATABASENAME; // "nlshriraam";//bf.readLine();
st.executeUpdate("CREATE DATABASE "+database);

}
catch (SQLException s)
{

}
}
catch (Exception e)
{


}
return;
}


hope this work
regards
NLShriraam
May 10 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.