|
Free
Java
Tutorials
|
![]() |
home |
Stay
at Home and Learn
|
|||||
Creating a Database with Java |
||||||
|
Now that your server has been started, you can go ahead and create a database. To create a new database, right click on Java DB again. From the menu that appears, select Create Database:
When you click on Create Database, you'll see a dialogue box appear:
Type a name for your database in the first box. Call it Employees. Type any User Name and Password (something a bit harder to crack than ours below!):
Click OK to create your database. It should then appear on the list:
Creating a Table in the DatabaseNow that the database has been created, you need to create a table in the database. To do so, right click on your database. From the menu that appears select Connect:
When a connection is made, you'll see some default folders for Tables, Views, and Procedures:
To create a new table in your database, right click the Tables folder. From the menu that appears, select Create Table:
When you click on Create Table, a dialogue box appears:
From here, you not only type a name for your table, but you also set up the columns for the table. In the Table Name at the top, delete the default name of Untitled. Type a new name for your table. Call it Workers. You'll then have a table called Workers, which is in the Employees database. But you can't click OK just yet as the table has no columns in it. We want to create columns with the following names: ID The ID column will hold a unique identifying number. This will identify a row in the table. A column with unique data in it is known as a Primary Key. Because it's the Primary Key, the column has to hold data: It can't hold a null value. (A null value just means there's no information there.) So put a tick in the box for Key. When you tick the Key box, check marks will also appear for Index and Unique. Now enter a title in the Column Name area. Type ID:
You now need to specify what kind of data is going in to the column. For our ID column, we'll have Integers. So scroll along until you come to Data Type. Click on Data Type and a drop down list will appear. From the drop down list, select Integers:
We now have enough for the ID column in the table. Click the Add Column button on the right to add a new column to the table. Enter the following values for this column (VARCHAR means a variable number of characters): Key: Unchecked For the third column in your table, enter the following values: Key: Unchecked For the final column, here are the values to enter: Key: Unchecked When you're finished, your Table dialogue box should look like this:
Click OK when you've entered all the information. Your table and table columns will then be created:
The next thing to do is to add some records to the database table. We'll do that next.
<-- Java and Databases | Adding Records to a Database Table --> <--Back to the Java Contents Page View all our Home Study Computer Courses
|