jimbo123
jimbo123

Reputation: 289

Java and databases - initializing tables

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.

Upvotes: 0

Views: 76

Answers (2)

roehrijn
roehrijn

Reputation: 1427

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.

Upvotes: 2

Prabhaker A
Prabhaker A

Reputation: 8473

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

Upvotes: 0

Related Questions