473,700 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

create database if not exists

7 New Member
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 24133
michaelb
534 Recognized Expert Contributor
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
shweta gandhi
7 New Member
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.g etConnection("j dbc: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 Recognized Expert Contributor
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
shweta gandhi
7 New Member
hi
thanks ur soln worked.
but How to check particular database exist or not in postgreSQL through java code?
Apr 23 '07 #5
shweta gandhi
7 New Member
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 Recognized Expert Contributor
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
nlshriraam
1 New Member
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.g etConnection("j dbc: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("us er", USER);
dbprops.put("pa ssword", PASSWORD);
try
{
myDriver = (Driver)Class.f orName(DRIVER). newInstance();
connection = DriverManager.g etConnection(UR L, dbprops);
try
{
Statement st = connection.crea teStatement();
BufferedReader bf = new BufferedReader( new InputStreamRead er(System.in));
String database = DATABASENAME; // "nlshriraam ";//bf.readLine();
st.executeUpdat e("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
8686
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 of the following fails, the database "creation" is rolledback. Otherwise, I would try deleting on error detection, but it could get messy. IF @Error = 0 BEGIN SET @ExecString = 'CREATE DATABASE ' + @FullName EXEC sp_executesql @ExecString
5
3271
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 this happens, the error returned is:
1
2368
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 when I run the code against a known database, even in SQL Management Studio I get a "Incorrect syntax near ')' error. What am i doing wrong?
2
6084
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 exist. -- Message posted via http://www.sqlmonster.com
0
2581
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 permission denied in database 'master'. An attempt to attach an auto-named database for file C:\Documents and Settings\Administrator.PEN1800\My Documents\testfromc\App_Data\WroxUnited.mdf failed. A database with the same name exists, or specified...
7
58612
by: olav78 | last post by:
create database test if not exists. Someone know how to do this in postgreSQL?
0
2594
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 functions in asp .net. It worked fine when I ran it. However when I put it into my IIS directory and try to access it from another pc I get this error: CREATE DATABASE permission denied in database 'master'. An attempt to attach an auto-named...
1
8965
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 attempt is made to create a database on a path without first enabling this feature using db2_create_db_on_path registry variable. Please enable this first and try again. When I tried to set this to yes, I get another error message "no database...
2
2199
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 given table exists within a given existing SQL database? Thanks for whatever help anyone can provide.
0
8712
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8639
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9058
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8952
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7794
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4649
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.