Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2009
    Posts
    4

    Unanswered: 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

  2. #2
    Join Date
    Dec 2008
    Posts
    76
    To create a database you have to attach to the instance, not connect to a database.
    RD

  3. #3
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    Also "CREATE DATABASE" is not SQL so you cannot use JDBC to execute it.

    Andy

  4. #4
    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

  5. #5
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    Quote Originally Posted by moridin84


    Well this link says that it is an SQL command, SQL CREATE DATABASE Statement

    Kinda confused
    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

  6. #6
    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

  7. #7
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    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.
    ---
    "It does not work" is not a valid problem statement.

  8. #8
    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

  9. #9
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    561
    Provided Answers: 2
    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
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

  10. #10
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •