Skip to main content
Updated date:

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 48

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 47 | Lesson 49 >>

Lesson 48: How to create databases and tables in Java NetBeans

The previous lesson was important because we did an introduction to Java and databases and we also covered on how to set up Apache Derby Network server. The Server must be on always when working with databases else you won’t be able to connect to the database.

We need to create a database on Java DB and then create a table and insert some data. We’ll then manipulate these records using Java codes. Manipulation of data involves operations such as adding new records, updating, deleting, searching, navigation etc.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

The better way to work with tables is to house them in a database. This way we can associate tables with a single database name and it will be easier to manipulate data contained in the tables.

Moreover, there are users' restrictions and privileges that we need to deal with. These user rights are better handled when data is organized in a database.

We’ll start by creating a database in Java NetBeans, make sure the Server is up and running. If it is not, right click on Java DB and select Start Server. To create a database, right click on Java DB and select Create Database as shown below.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

We are going to create a database called School, so on the dialog box that appears, enter School as the Database Name. For now, we can enter User Name as admin and Password as admin for the database login credentials, don’t forget to confirm the password.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

Click OK and the database will be created and listed among the other databases.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

How to create tables in Java NetBeans

Now that we have the School database, we’ll go ahead and create a table in the database. To create a table in Java NetBeans, we have to connect the database to the Server. Right click on the School database and select Connect.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

Once the connection is made, expand on the School database and navigate further down to APP. Expand APP to reveal three folders; Tables, Views and Procedures. Right click on Tables and select Create Table. Note: This path might be slightly different depending on the version of Java NetBeans you are using.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

When you click on Create Table option, a dialog box like the one shown below will appear.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

We need to create a table called Students with column names; StdID, StdName, Faculty, Course, City, Address, Telephone and Remarks. For the Table name, enter Students. Click on the Add column button to add the first column. Our first column should be StdID which will be used to store student's identification number. StdID will act as the Primary key and Index so we should specify that it is Unique and cannot contain Null values.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

The image above shows how you will go about creating the columns. I have set the Data type for StdID column to be NUMERIC of size to 8 but you can put any other size larger than 0. Click OK and you should be able to create the first table column. Follow the same procedure again to create all the other columns with the following specifications:

Table Structure

Primary Key: Unchecked
Index: Unchecked
Null: Unchecked
Unique: Unchecked
Name: StdName
Type: VARCHAR
Size: 40

Primary Key: Unchecked
Index: Unchecked
Null: Unchecked
Unique: Unchecked
Name: Faculty
Type: VARCHAR
Size: 30

Primary Key: Unchecked
Index: Unchecked
Null: Unchecked
Unique: Unchecked
Name: Course
Type: VARCHAR
Size: 40

Primary Key: Unchecked
Index: Unchecked
Null: Unchecked
Unique: Unchecked
Name: City
Type: VARCHAR
Size: 30

Primary Key: Unchecked
Index: Unchecked
Null: Unchecked
Unique: Unchecked
Name: Address
Type: VARCHAR
Size: 40

Primary Key: Unchecked
Index: Unchecked
Null: Checked
Unique: Unchecked
Name: Remarks
Type: VARCHAR
Size: 100

Table Structure Cont'

Primary Key: Unchecked
Index: Unchecked
Null: Checked
Unique: Unchecked
Name: Telephone
Type: VARCHAR
Size: 20

When you are through, the complete table structure should look as shown:

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

Click OK and the table will be created and placed under the Tables folder with the specified columns.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

Now, let us add a few records to work with. To add records into the table, right click on table Students and select View Data.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

Click on the Insert Records button on the top left side of the table to start inserting new records. Alternatively you can press Alt+I keys

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

When you click on Insert Records button or press on Alt+I you will be taken to record insertion dialog box where you can add records. Every time you are through with entering a record, click on Add Row button to insert a new record.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

Click OK when you are through entering all records, this will take you back to the Table view mode. Below is the table with three records.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-48

For us to be able to manipulate data in databases or tables, we need to have some skills in working with Structured Query Language (SQL) commands. In the next lesson, we shall learn a few SQL commands before we can learn how to connect to a database in Java NetBeans.

<< Lesson 47 | Lesson 49 >>

  • Arrays of string type in Java NetBeans
    In some cases in Java programming, you would need to store values of string type in an array like days of the week or Boolean (True or False) values. In this article, we shall learn how we can implement and work with arrays of string type in Java.
  • How to implement User Defined Methods in Java NetBeans
    Having programming ability to create User Defined Methods (UDMs) is one of the most sought skills in programming and software development. In this article, I will discuss how to create user defined methods in Java programming language ...
  • How to handle errors in Java NetBeans programming
    Ask any programmer what is the most daunting task in programming and you will get a response to the effect of detecting, isolating and correcting errors. While some programming errors such as syntax errors are easy to detect and correct ...
  • Working with GUI controls: Label and Text field
    Java NetBeans provides different Graphical User Interface (GUI) controls which can visually simplify programming and enhance the functioning and user-friendliness of the final application. This article explores different ways of manipulating Java...

Want to make money online?

Comments

Danson Wachira (author) from Nairobi, Kenya on April 23, 2013:

Hi ahain,

You can delete a database by right clicking on the database on the Projects tab and selecting Delete.

ahain on April 23, 2013:

how to delete a database..

?

Danson Wachira (author) from Nairobi, Kenya on April 06, 2013:

Hi ComfortB,

Both you and i know how much we love writing, i gives me pleasure to see such tutorial being helpful. With interest i know you can make it as a programmer only that programmers dream about programs :-) Thanks for the visit and comment.

Comfort Babatola from Bonaire, GA, USA on April 06, 2013:

dwachira , you must be a very patient individual! 'Cause I know it's not easy putting a tutorial like this together.

I see there's a little bit of similarity between creating tables in MS Access and Java Net Beans. Which caused me to think I might just do well following your tutorials.

This has been very interesting. Voted up and useful, and I'm sure many will find it useful.

Related Articles