By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,643 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,643 IT Pros & Developers. It's quick & easy.

Creating Database in DB2 Database Through Java Program

sailendrajena
P: 1
Hi,

Can anyone please help me to write a code on java which will create database in DB2 database. For example manually we are creating the database in the DB2 database after that inside that using that database we are creating any number of table s. But I want to create that database inside DB2 database. So please help me in that code. I tried that code but its throwing me an error. Please help me.

My code which I had done is:
Expand|Select|Wrap|Line Numbers
  1. JDBCUtil.java
  2.  
  3. package com.lara.util;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. public class JDBCUtil
  12. {
  13.     static
  14.     {
  15.         try
  16.         {
  17.             Class.forName("com.ibm.db2.jcc.DB2Driver");
  18.         }
  19.         catch(ClassNotFoundException ex)
  20.         {
  21.             ex.printStackTrace();
  22.         }
  23.     }
  24.     public static Connection getConnection() throws SQLException
  25.     {
  26.         return DriverManager.getConnection("jdbc:db2://localhost:50000/SADATA", "db2admin", "password");
  27.     }
  28.     public static Connection getConnectionForDatabase() throws SQLException
  29.     {
  30.         return DriverManager.getConnection("jdbc:db2://localhost:50000", "db2admin", "password");
  31.     }
  32.     public static void closeDbResources(ResultSet rs, Statement stmt, Connection con)
  33.     {
  34.         try
  35.         {
  36.             if(rs != null)
  37.             {
  38.                 rs.close();
  39.                 rs = null;
  40.             }
  41.         }
  42.         catch(SQLException ex)
  43.         {
  44.             ex.printStackTrace();
  45.         }
  46.         try
  47.         {
  48.             if(stmt != null)
  49.             {
  50.                 stmt.close();
  51.                 stmt = null;
  52.             }
  53.         }
  54.         catch(SQLException ex)
  55.         {
  56.             ex.printStackTrace();
  57.         }
  58.         try
  59.         {
  60.             if(con != null)
  61.             {
  62.                 con.close();
  63.                 con = null;
  64.             }
  65.         }
  66.         catch(SQLException ex)
  67.         {
  68.             ex.printStackTrace();
  69.         }
  70.     }
  71. }


A.java
Expand|Select|Wrap|Line Numbers
  1. import java.sql.Connection;
  2. import java.sql.PreparedStatement;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5.  
  6. import com.lara.util.JDBCUtil;
  7. import com.lara.util.JDBCUtilForMySQL;
  8.  
  9.  
  10. public class A 
  11. {
  12.     public static void main(String[] args)
  13.     {
  14.         Connection con = null;
  15.         Statement stmt = null;
  16.         try
  17.         {
  18.             con  = JDBCUtil.getConnection();
  19.             PreparedStatement pstmt = con.prepareStatement("create database DBtry");
  20.             int update = pstmt.executeUpdate();
  21.             System.out.println("Database Created");
  22.         }
  23.         catch(SQLException ex)
  24.         {
  25.             ex.printStackTrace();
  26.         }
  27.         finally
  28.         {
  29.             JDBCUtil.closeDbResources(null, stmt, con);
  30.         }
  31.  
  32.  
  33.     }
  34. }


Exception which I am getting is:

Expand|Select|Wrap|Line Numbers
  1. com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=database;create ;TABLESPACE, DRIVER=3.63.75
  2.     at com.ibm.db2.jcc.am.fd.a(fd.java:679)
  3.     at com.ibm.db2.jcc.am.fd.a(fd.java:60)
  4.     at com.ibm.db2.jcc.am.fd.a(fd.java:127)
  5.     at com.ibm.db2.jcc.am.yn.c(yn.java:2644)
  6.     at com.ibm.db2.jcc.am.yn.d(yn.java:2632)
  7.     at com.ibm.db2.jcc.am.yn.a(yn.java:2097)
  8.     at com.ibm.db2.jcc.am.zn.a(zn.java:7197)
  9.     at com.ibm.db2.jcc.t4.cb.h(cb.java:141)
  10.     at com.ibm.db2.jcc.t4.cb.b(cb.java:41)
  11.     at com.ibm.db2.jcc.t4.q.a(q.java:32)
  12.     at com.ibm.db2.jcc.t4.sb.i(sb.java:135)
  13.     at com.ibm.db2.jcc.am.yn.gb(yn.java:2066)
  14.     at com.ibm.db2.jcc.am.zn.pc(zn.java:3446)
  15.     at com.ibm.db2.jcc.am.zn.b(zn.java:4236)
  16.     at com.ibm.db2.jcc.am.zn.dc(zn.java:757)
  17.     at com.ibm.db2.jcc.am.zn.executeUpdate(zn.java:740)
  18.     at com.lara.classes.A.main(A.java:48)


Please help me from this exception.


Thanks & Regards
Sailendra Jena
Aug 2 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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