473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Creating Database in DB2 Database Through Java Program

sailendrajena
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
0 1736

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

Similar topics

5
by: Hassan Naqvi | last post by:
Hi, Basically, I am Java developer. In past I have played with Oracle using Java (JDBC). But this is the time to play with IBM DB2 using Java (JDBC). So kindly help this DB2 newbie. I have a...
6
by: Jerry Spence1 | last post by:
Why doesn't the following work in my ASP program? I have imported ADOX I am trying to create a temporary database on the user's PC. The example is taken from Microsoft. Dim cat As Catalog =...
5
by: smileskhan | last post by:
Hay Friends... Here I started a new and interested thread. I hope you also enjoy it. I got a task to creat a Hospital Database in Java. But I don´t have any good clues abouts it....
22
Frinavale
by: Frinavale | last post by:
How To Use A Database In Your Program Many .NET solutions are database driven and so many of us often wonder how to access the database. To help you understand the answer to this question I've...
3
by: Begreen | last post by:
Hi All, I wrote a java program which outputs a xml file! But I would prefer this program to insert the DTD code on the fly, in the xml file when created! I want the xml file to look like...
2
by: astolpho | last post by:
I am using a slightly outdated reference book on J2EE programming. It gives 2 methods of creating a database used in its casestudies. The first is an ANT script that gives the following output: ...
1
by: peggitt | last post by:
I need help. I need code to connect my java program with a mysql database hosted by an internet service provider(domain). The java program should run locally but read and write to a database on the...
4
by: Quill_Patricia | last post by:
I have a Python script which is used to load data into a database. Up to now this script has been run by customers from the Windows command prompt using "python edg_loader.pyc". Any error messages...
5
madzman23
by: madzman23 | last post by:
Hi guyz, I kinda new here and I dont know if there is post that similar to my question, because I really needed immediately I am posting my question. Can anyone here help me how to call a Java...
1
by: rotaryfreak | last post by:
Hi everyone, So ive basically searched the interwebs and tried some examples but i cant not seem to get this working. I would like to create a Java program in which i can save data into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.