473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

create database if not exists

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
7 23933
michaelb
534 Expert 512MB
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
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
michaelb
534 Expert 512MB
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
hi
thanks ur soln worked.
but How to check particular database exist or not in postgreSQL through java code?
Apr 23 '07 #5
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
michaelb
534 Expert 512MB
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
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

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

Similar topics

2
by: Robin Tucker | last post by:
I have some code that dynamically creates a database (name is @FullName) and then creates a table within that database. Is it possible to wrap these things into a transaction such that if any one...
5
by: Frank van Vugt | last post by:
Hi, I noticed that when using the single commandline: drop database <name>; create database <name>; this sometimes fails due to a pg_autovacuum process running on the background. When...
1
by: RSH | last post by:
I'm creating databases programatically in .Net and I want to verify that the database doesn't exist before creating it. i found the SQL code: IF NOT EXISTS(SELECT * FROM <databasename>) But...
2
by: Cismail via SQLMonster.com | last post by:
Hi, Is there a simple way to verify if a database exists? I'm writing a stored procedure that will accept a database name as an input parameter, and create the database if it does't already...
0
by: casper | last post by:
Hi, I use asp.net 2.0 and VWD (and sqlserver 2005 express) on windows 2000 sp4.. Starting from VWD, it works. Starting from the browser, the code below produces this error: CREATE DATABASE...
7
by: olav78 | last post by:
create database test if not exists. Someone know how to do this in postgreSQL?
0
by: ravi.govindaraju | last post by:
Hi all, I am new to asp.net and sqlserver. I just installed vs2005 and sqlserverexpress edition on my home pc on which I have full admin rights. I wrote a program that uses the built in log in...
1
by: Sue | last post by:
I just installed the free DB2 Express. I am going through the wizard and I am unable to create a database. I keep getting the error "create database path does not exist". The log file says "An...
2
by: BobLewiston | last post by:
Could someone please give me the simplest possible C# code snippets / SQL queries to determine whether: * a given SQL database exists, * a given existing SQL database is accessible, and * a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.