Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Checking before doing creation table in the database "JAVA"

Posted on 2006-07-12
8
Medium Priority
?
263 Views
Last Modified: 2010-03-31
I want to write a method to create table (CREATE TABLE …) in the database.
I wrote a method doCreation() to generate SQL create statement.
But, I want to check if tableArrayList.get(1) = “name of table” is already in the database or do the creation.
0
Comment
Question by:javamate06
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17090347
You could do

CREATE OR REPLACE TABLE

if the Sql of your db allows it. Otherwise you will have to query system tables
0
 

Author Comment

by:javamate06
ID: 17113648
If I use:

CREATE OR REPLACE TABLE

I may face a problem in the size of column, I may create table with minmum size and it will not allowed this.  Becuse of I have for every create statment differenet size of the same column.

If there is a way to check the size and take the maximum.
0
 

Author Comment

by:javamate06
ID: 17113726
Can I have the size of the columns that is already defined in the DB?
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 86

Expert Comment

by:CEHJ
ID: 17113742
>>Can I have the size of the columns that is already defined in the DB?

That would be db-specific. Check for your db. You could build your own mapping between column type and size
0
 

Author Comment

by:javamate06
ID: 17113960
I use:

 java.sql

and I want to retrieve the size of the column from “desc tableName“ query because the second column has the type and the size ,
I tried with this code
 
        ResultSet rs = stmt.executeQuery("desc TABLE2");
            ResultSetMetaData rsmd = rs.getMetaData();
           int numberOfColumns = rsmd.getColumnCount();
          
                while(numberOfColumns !=  0)
                {
                 System.out.println(rsmd.getColumDisplaySize(numberOfColumns));
                 numberOfColumns--;
                 }

It gave me
 
ORA-00900 Invalid SQL statement
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 160 total points
ID: 17114130
Try

 ResultSet rs = stmt.executeQuery("describe TABLE2");
0
 

Author Comment

by:javamate06
ID: 17114531
For you information use

select count(1) from tab where tname like 'table name';


if count(1) > 0
then table exit;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17120368
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question