Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Create table statement from DatabaseMetaData

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create table statement from DatabaseMetaData

    Hi Experts,

    Do you know if it's possible to retrieve a create table statement based on a DatabaseMetaData ? (without looping through columnName / Type)

    The idea is to get the DatabaseMetaData from one db server, and execute the Create Table statements on a different db server.

    One obvious solution would be to loop through each table's column name/type and construct the create table statement manually, but I'd like to know whether this can be automated.

    Thanks in advnace,

    Sid


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Create table statement from DatabaseMetaData

    Yes, but it depends upon how you wish to create the new tables. Can you provide a simple example that demonstrates what you wish to achieve?

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create table statement from DatabaseMetaData

    Sure.

    We have two databases: Database A and B.

    On database A, there is a table called Table T.

    Using JDBC, we get the DatabaseMetaData for database A , and locate Table T in the meta data.

    Now at this point, I'd like to know how we can get a ready made Create statement for Table T (simply a string) and execute it on database B.

    Hope that was clear.

    Thanks.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Create table statement from DatabaseMetaData

    I meant more of a precise example showing the schema for Table T, and how you wish to create the table in Database B...for example, do you just wish to Copy the table to database B? Or restructure its data into database B.

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create table statement from DatabaseMetaData

    No it's simply creating the table with no data manipulation. I don't really care about table contetns.

    example:

    Table T (on database A)
    Column A int
    Column B int

    conn = getConnection(database A);

    DatabaseMetaData dmd = conn.getMetaData();

    String createStatement = dmd.getCreateStatement ( String tableName ); // This is what I am looking for

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Create table statement from DatabaseMetaData

    Ah, I see...take a look at this link. It provides code to copy a database, but can be trimmed to copy a single table schema:How To Copy Database Data Using JDBC. You should note that things like foreign keys, indexes, triggers, views, etc...are not copied in this code.

  7. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create table statement from DatabaseMetaData

    Thanks for that - appreciate your help.

    Sid

Similar Threads

  1. Building Table of Specified Rows and Columns
    By wale89 in forum Java Servlet
    Replies: 1
    Last Post: August 3rd, 2010, 08:57 AM
  2. Replies: 0
    Last Post: April 15th, 2010, 05:13 AM
  3. Adding Marathi words to MySQL table
    By vaishali in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: July 8th, 2009, 06:43 AM
  4. Replies: 2
    Last Post: July 8th, 2009, 06:35 AM
  5. Getting table height using JSTL
    By jsnx7 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: March 19th, 2009, 12:03 PM