Solved

Checking before doing creation table in the database "JAVA"

Posted on 2006-07-12
8
232 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 80 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java / Linux and Regular Expressions 11 71
Apps blocked by Java 9 64
java stored proc example 9 24
hibernate example for saving data 19 14
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now