• 1 reply
  • Latest Post - ‏2013-04-02T12:10:16Z by SystemAdmin
17917 Posts

Pinned topic Create Db2 database through jdbc

‏2013-04-02T06:14:05Z |
I want to create a db2 database programmatically through jdbc, but i dont know how to do it.

classname = "";
constring = "jdbc:db2://localhost:50000/";
con = DriverManager.getConnection(constring,"myusr","mypwd");
stmt = con.createStatement();
stmt.executeUpdate("CREATE DATABASE test");

This does not allow me to create a new database stating the reason that my constring is not valid. But in a similar fashion i am able to create database for the mysql.
  • SystemAdmin
    17917 Posts

    Re: Create Db2 database through jdbc


    Actually, you cannot create databases via JDBC, you need to do that via another way, for example via JNI invoking the DB2 API ( or CLI.

    The code provided does not work to create a database, you have to change it completly. In order to create a database, it is necessary to attach to an exisiting instance in the server. You cannot be connected to a database, when creating a new one, but JDBC only works with an active connection. The Create Database statement is not SQL, so you cannot use it in a Java SQL Statement.

    You can also create the database with the help of the Data Server Client (Runtime), that provides a set of commands that allows you to create the database. You call them from Java via Runtime.exec, but remember to have the DB2 environment loaded and the instance cataloged with the remote server (if any)

    MySQL allows you to do many things, even if they are conceptually wrong.