473,386 Members | 1,752 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,386 software developers and data experts.

ejbCreate()

oll3i
679 512MB
i want in my ejbCreate() to create the database , tables and populate the tables
is it allowed?

my ejbCreate() connects to the database but it does not create the tables

Expand|Select|Wrap|Line Numbers
  1. public void ejbCreate() {
  2.     System.out.println("Inside ejbCreate");
  3.  
  4.         Connection connection = null;
  5.         try {
  6.             // get database connection
  7.                connection =this.getConnection(dbURL);
  8.                Statement statement = connection.createStatement();   
  9.  
  10.                // DROP the existing tables
  11.                 try
  12.                      {
  13.                      statement.execute ("DROP TABLE contacts");
  14.                      statement.execute ("DROP TABLE books");
  15.                       }
  16.                  catch (SQLException e)
  17.                  {
  18.                   // catch and ignore table not found exception
  19.                  }
  20.  
  21.                  statement.execute("CREATE TABLE contacts("+
  22.                        "CONTACT_ID INTEGER NOT NULL PRIMARY KEY,"+
  23.                        "FIRSTNAME VARCHAR(25) not null,"+
  24.                        "LASTNAME VARCHAR(25) not null,"+
  25.                        "EMAIL VARCHAR(60) not null,"+
  26.                        "CONSTRAINT UNIQUE_EMAIL UNIQUE(EMAIL)");
  27.  
  28.  
  29.  
  30.  
  31.  
  32.               statement.execute("create table BOOKS("+
  33.                     "BOOK_ID INTEGER NOT NULL PRIMARY KEY,"+
  34.                      "AUTHOR VARCHAR(100) not null,"+
  35.                     "TITLE VARCHAR(100) not null,"+
  36.                     "ISBN VARCHAR(10) not null,"+
  37.                      "ISSUE_YEAR SMALLINT not null,"+
  38.                      "CONSTRAINT UNIQUE_ISBN UNIQUE(ISBN)");
  39.  
  40.  
  41.          PreparedStatement preparedstatement = connection.prepareStatement("INSERT INTO CONTACTS (CONTACT_ID,FIRSTNAME, LASTNAME, EMAIL) VALUES (?,?,?,?)"); 
  42.  
  43.          preparedstatement.setInt (1, 1);
  44.          preparedstatement.setString (2, "Jan");
  45.          preparedstatement.setString (3, "Kowalski");
  46.          preparedstatement.setString (4, "jan_kowalski@op.pl");
  47.          preparedstatement.execute ();
  48.  
  49.  
  50.          preparedstatement.setInt (1, 2);
  51.          preparedstatement.setString (2, "Andrzej");
  52.          preparedstatement.setString (3, "Pawlowski");
  53.          preparedstatement.setString (4, "andrzej_pawlowski@op.pl");
  54.          preparedstatement.execute ();
  55.  
  56.  
  57.          preparedstatement.setInt (1, 3);
  58.          preparedstatement.setString (2, "Mariusz");
  59.          preparedstatement.setString (3, "Cygan");
  60.          preparedstatement.setString (4, "mariusz_cygan@wp.pl");
  61.          preparedstatement.execute ();
  62.  
  63.  
  64.          preparedstatement.setInt (1, 4);
  65.          preparedstatement.setString (2, "Maciej");
  66.          preparedstatement.setString (3, "Nowak");
  67.          preparedstatement.setString (4, "maciej_nowak@op.pl");
  68.          preparedstatement.execute ();
  69.  
  70.  
  71.          preparedstatement.setInt (1, 5);
  72.          preparedstatement.setString (2, "Bartosz");
  73.          preparedstatement.setString (3, "Kasprzak");
  74.          preparedstatement.setString (4, "bartosz_kasprzak@o2.pl");
  75.          preparedstatement.execute ();
  76.  
  77.  
  78.  
  79.  
  80.          System.out.println("Inserted rows into table contacts");
  81.  
  82.  
  83.          preparedstatement = connection.prepareStatement("INSERT INTO BOOKS (BOOK_ID,AUTHOR, TITLE, ISBN,ISSUE_YEAR) VALUES (?,?,?,?,?)"); 
  84.  
  85.          preparedstatement.setInt (1, 1);
  86.          preparedstatement.setString (2, "Rafe Coburn");
  87.          preparedstatement.setString (3, "SQL dla każdego");
  88.          preparedstatement.setString (4, "8371972482");
  89.          preparedstatement.setInt (5,2001);
  90.          preparedstatement.execute ();
  91.  
  92.          preparedstatement.setInt (1, 2);
  93.          preparedstatement.setString (2, "Larry Ullman");
  94.          preparedstatement.setString (3, "PHP and MySQL for dynamic web sites");
  95.          preparedstatement.setString (4, "0321186486");
  96.          preparedstatement.setInt (5,2003);
  97.          preparedstatement.execute ();
  98.  
  99.          preparedstatement.setInt (1, 3);
  100.          preparedstatement.setString (2, "David Flanagan");
  101.          preparedstatement.setString (3, "JavaScript(The Definite Guide)");
  102.          preparedstatement.setString (4, "0596000480");
  103.          preparedstatement.setInt (5,2002);
  104.          preparedstatement.execute ();
  105.  
  106.          preparedstatement.setInt (1, 4);
  107.          preparedstatement.setString (2, "Kayshav Dattari");
  108.          preparedstatement.setString (3, "Język C++(Efektywne programowanie obiektowe)");
  109.          preparedstatement.setString (4, "8373618120");
  110.          preparedstatement.setInt (5,2005);
  111.          preparedstatement.execute ();
  112.  
  113.  
  114.          preparedstatement.setInt (1, 5);
  115.          preparedstatement.setString (2, "W.Krysicki L.Wlodarski");
  116.          preparedstatement.setString (3, "Analiza Matematyczna w Zadaniach(część 1)");
  117.          preparedstatement.setString (4, "8301014601");
  118.          preparedstatement.setInt (5,1998);
  119.          preparedstatement.execute ();
  120.  
  121.          System.out.println("Inserted rows into table books");
  122.  
  123.          // close connection, statement
  124.          if (preparedstatement != null)
  125.              preparedstatement.close();
  126.          if (connection != null)
  127.              connection.close();
  128.          System.out.println("Closed statement and connection.");
  129.  
  130.  
  131.         } catch (SQLException sqlException) {
  132.                 System.out.println(sqlException.getMessage());
  133.           }          
  134. }
  135.  
Nov 13 '08 #1
3 2428
itsraghz
127 100+
First of all, writing such a big chunk of tasks in one method is NOT a good OO practice.

If I am right, ejbCreate() NEVER creates a database , table and row. All it just does is "creating/instantiating a bean instance which close tied to and represents a row of database table which should have already been existing".

Hope this helps!
Nov 13 '08 #2
oll3i
679 512MB
i read somewhere that i can connect to the db and insert rows in ejbCreate() ?
Nov 13 '08 #3
itsraghz
127 100+
Not so sure of it. Shall check and get back to you!
Nov 13 '08 #4

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

Similar topics

1
by: Martin Zeigler | last post by:
I have been using Weblogic 5.1 for a couple of years. I currently pass an object as a parameter to my ejbCreate method. The data in the object could be modified by my bean and the caller needs to...
6
by: star111792 | last post by:
helo all i am working with JSP and EJBs. i have written a simple code for verifying username and password of user by using session beans. my problem is that i am getting the following error: ...
0
by: star111792 | last post by:
helo all i am working with JSP and EJBs. i have written a simple code for verifying username and password of user by using session beans. my problem is that i am getting the following exception: ...
2
by: star111792 | last post by:
helo all, i am working with JSP and EJBs. i have written a simple code for verifying username and password of user by using session beans. my problem is that i m getting exception named...
4
oll3i
by: oll3i | last post by:
In my component class i implemeneted ejbCreate method which connects to the cloudscape database and returns connection. My question is Is it the right thing to do or should it be done in some other...
7
oll3i
by: oll3i | last post by:
in deploytool when i go to "cmp database" i get the message "cannot find accessor getFirstName for CMP field firstName"?
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.