0

Hi I'm trying to create an address book type program in Java using SQLite. For now, all it should do is ask the user for a contact name and telephone number to add to the table.

I haven't started the coding but the thing I cannot get my head around is the table creations. Suppose I write a program that

  1. Connects to a database
  2. Creates a table
  3. Asks the user for details about adding contact data (to the table).

I compile it once without errors. But then every time I run the program it will create a new table. The only thing I can think of is that database and table creation is done outside of the Java program.

2
  • 2
    The program could simply 1. conect to the DB, 2. Check if the table exists, 3. create it if it doesn't exist yet, 4. ask the user for details. Commented Aug 31, 2013 at 20:36
  • Okay, I did not know there was a function to check if table exists. Commented Aug 31, 2013 at 21:34

2 Answers 2

2

In many enterprise environments your application's database user will propably not have the rights to execute DDL (CREATEs, ALTERs, and so on...) statements. So managing the database layout outside the application is usually done in these cases.

There are some tools availiable which try to simplify this job. A very sophisticated one for example is Liquibase.

But if you want to manage your database inside your application it is a best practise that you define an extra table where you maintain a database schema version number. On application startup you check this schema version and then - if applicable - execute your DDL statements to create or alter the schema to get the desired shema version.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay I was not aware of that. I am just writing this java code in a text editor and compiling it at the terminal.
0

Yes it is always good to create tables outside of java program.Just use that tables in java application.
So you design could be now.
1.Connect to database.
2.Ask user for details about adding contact data (to the table).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.