Solved

Checking before doing creation table in the database "JAVA"

Posted on 2006-07-12
8
261 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

628 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