| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

08-31-09, 16:47
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 4
|
|
|
Trying to create a database programmatically using JDBC
|
|
Hi,
I'm making a personal project which is essentially a Java desktop application which uses a DB2 Express database. I have it working currently, but only when connecting to a server&database. If I want to be able to put it in an install package I need some way to setup the database&server using the code itself.
This is basically what the code looks like:
Class.forName(jdbcDriver);
Connection connection = DriverManager.getConnection(url, username, password);
Statement st = connection.createStatement();
int result = st.executeUpdate("CREATE DATABASE testdb");
The issue I'm having at the moment is that, I can't get to the CREATE DATABASE line as it crashed at the second line because it you need to connect to an existing database.
I've tried googling the answer and all that but most people seem to be just creating the database externally.
Thanks for any help
|
|

08-31-09, 16:54
|
|
Registered User
|
|
Join Date: Dec 2008
Posts: 76
|
|
To create a database you have to attach to the instance, not connect to a database.
__________________
RD
|
|

08-31-09, 17:03
|
|
Registered User
|
|
Join Date: Jan 2003
Posts: 3,575
|
|
|
|
Also "CREATE DATABASE" is not SQL so you cannot use JDBC to execute it.
Andy
|
|

09-02-09, 08:21
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 4
|
|
Quote:
|
Originally Posted by rdutton
To create a database you have to attach to the instance, not connect to a database.
|
Umm, there is a different set of commands for this? It's just the Java DriverManager doesn't seem to have another way to create a connection other then the getConnection function.
I dunno, maybe I'm just going about it the wrong way. Perhaps I should be using a Java derby database since it seems to have an embeded mode and it allows you to create, start and stop a database using easily JDBC.
Quote:
|
Originally Posted by ARWinner
Also "CREATE DATABASE" is not SQL so you cannot use JDBC to execute it.
Andy
|
Well this link says that it is an SQL command, SQL CREATE DATABASE Statement
Kinda confused 
|
|

09-02-09, 08:32
|
|
Registered User
|
|
Join Date: Jan 2003
Posts: 3,575
|
|
Quote:
|
Originally Posted by moridin84
|
Well this DB2 (THE Manual) link: DB2 Universal Database will show that DB2 treats it as a command not SQL (look in the reference sections)
If you want to create a database in DB2 using java, then you will have to use JNDI.
Andy
|
|

09-02-09, 12:36
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 4
|
|
Ah I see, CREATE SCHEMA seems to be the correct command then, thanks. Anyway, I won't be getting anywhere in this way so let me start over.
Basically I want to do this
Application Starts
1) Check to see if the database has been created, if not then create it
2) Start Database
3) Connect to database
Application Closes:
1) Shutdown database
To be, this seems to be pretty basic stuff which any desktop application which uses a database to store information internally would need to be able to do. I can't seem to figure or find enough information to do it though.
Obviously I'm not expecting people here to tell me how to do that all, I would however be grateful to any who would help me in any of the following ways.
1) Point out where in the DB2 nanual(if it's there) how to do this is explained
2) Point out any articles which explain how to do this
3) Give me a phrase (or a series of phrases )which describes this situation and will get me decent articles if I use them in a google search
4) Point out any forums which might be better suited to provide information on this.
Thanks for any help
|
|

09-02-09, 12:54
|
|
:-)
|
|
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
|
|
Judging by your requirements, you need a single-user local database embedded in the application. I'd say DB2 is an overkill. I think you should consider using an appropriate product, such as Derby, which you have mentioned, or any other Java-based embeddable database engine.
|
|

09-02-09, 12:55
|
|
Registered User
|
|
Join Date: Dec 2008
Posts: 76
|
|
You probably could do this with MS Access, but creating a database in an enterprise class database system is a bit more complex and not easily done by API.
Simplified: in DB2 an instance holds databases, databases hold tables.
You attach to the instance with the ATTACH command to be able to create a database. You connect to a database with the CONNECT command to create tables.
JDBC does not provide for the ATTACH command.
__________________
RD
|
|

09-03-09, 03:02
|
|
Registered User
|
|
Join Date: Apr 2008
Location: Iasi, Romania
Posts: 317
|
|
Your approach is more appropriate when you are using MS SQL Server Express. But even there, you have to use SQL-DMO, as ODBC/JDBC is not able to help you with such administrative tasks.
__________________
Florin Aparaschivei
Iasi, Romania
|
|

09-04-09, 08:58
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 4
|
|
Ah I see, I didn't want to simply switch from DB2 if I could figure out a way to get it working but if that's simply how it is then I'll guess I'll just try derby.
Thanks alot for all the help people.
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|