Solved

Checking before doing creation table in the database "JAVA"

Posted on 2006-07-12
8
220 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
Comment Utility
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
Comment Utility
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
Comment Utility
Can I have the size of the columns that is already defined in the DB?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:javamate06
Comment Utility
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
Comment Utility
Try

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

Author Comment

by:javamate06
Comment Utility
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
Comment Utility
:-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

8 Experts available now in Live!

Get 1:1 Help Now