Java Programming tutorial with officialhord - Connecting Java program to Sqlite Database

in #utopian-io8 years ago (edited)

sample.jpg

Hi steemians thanks for following, from the last tutorial we created example1, today we will be working on creating a database and connecting it to our program.

Straight to work

Stage 1 – Creating the Database

Before we create the database, let me tell you what a database is.

Database:

A database is an organized collection of data in tables, rows and columns, this allows search for data faster and more organized.

To create our database we follow the steps below;

STEP 1

Open Sqlite Studio

database1.jpg

  1. Click on the database menu.

  2. Click add a database

  3. Click on the plus sign icon in the popup menu and select where you want the file to be stored, I suggest you go to documents, netbeans projects, tutorial.

  4. Set the name you want for the database.

  5. Click save, click ok.

We have successfully created a database.
Basically, after doing this our database is always empty without any data until we start adding data to the database, like I said in earlier tutorial, a database is a storage location for data in an organized manner (tables) in the computer system.
A database uses tables to store data, when a database is created we would have to add tables to it before it starts storing data, without this the database is useless.

Step 2 – Creating tables in the database.

  1. Right click on tables.
  2. Click create a table.
  3. Input the name of the table as in 3 above.

Step 3 – Adding Columns to the database.

  1. Click on the table icon that has a pencil on it.
  2. Enter the name of the column.
  3. Choose the variable type (the type of data you want it to store)
  4. Click ok.
    If you followed the two stages above you should see this as you go along.
    database2.jpg

Go ahead and add one more column to your table, it should be called password and the Data type should be VARCHAR. If you’ve done this click the commit sign close to the add column sign in your SQlite studio and click ok so the columns in the database can be saved.
database3.jpg

Step 4 – Add Data to database.

database4.jpg

Adding data to database.

  1. Click on the data tab.
  2. Click the plus sign.
  3. Double click the area where I typed admin and do same.
  4. Do same for the password column also.
  5. the commit sign and we are done.
    We have successfully added data to our database.

STAGE 2 – CONNECTING OUR DATABASE TO THE PROGRAM

Step 1 – Create a java class for the connection

database5.jpg

Open your Pre-installed Netbeans ide, the program we worked on previously should be open, if not open the application folder and double click on source packages. Once this is done and you can see the default packages, follow these steps:

  1. Right click on default packages.
  2. Go to new.
  3. Click Java class
  4. Enter the class name, which should be “javaconnect” for reference reasons.
  5. Click ok.
    Congratulations, you have successfully created a java class.

Step 2 – Download the Java-Sqlite Connection library.

We will be needing a java library to enable our connecting the program to the database work out as planned, without the library any code we type in the javaconnect class will not work so click here to download the library required,
database6.jpg

download version 3.21.0 once download is complete, copy the downloaded file to the location (documents/netbeans projects/tutorialclass) where we saved the files for our program.

Step 3 - Import the Library to our Program.

Once the file has been copied to our source file, we now need to import it into our libraries in the program so it is available for the program to access whenever it is required.
database7.jpg

Importing Sqlite-jdbc library.

  1. Right click on libraries
  2. Click add Jar/Folder.
  3. In the location chooser click documents, netbeansprojects, tutorial class and click the sqlite-jdbc .3.21.0
  4. Click Open.
    If you did it correctly sqlite-jdbc-3.21.0.jar should be in the libraries. YIPEE !! we are done.

Step 5 – Adding our Connection Code

If you have followed steps 1 – 4 above, then you’re ready to input some coding therefore lets import some java libraries more by inputting these codes.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;

database8.jpg

When you have input these codes, you will be able to work with functions from the libraries and the jar file we downloaded also adding our own code to make things work.
Will explain everything we imported in the next post tomorrow.
That is done so no we add the other section of code.

public static void connect() {
        Connection conn = null;
        try {
            // db parameters
            String url = "jdbc:sqlite:C:\\Users\\mojisola.akinola\\Documents\\NetBeansProjects\\tutorialclass\\tutorialdatabase.db";
            // create a connection to the database
            conn = DriverManager.getConnection(url);
            JOptionPane.showMessageDialog(null, "Connection to SQLite has been established !!!");     
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }

Your code should look like this.
database9.jpg

Once this is done, right click inside the code section and click run, you should see this confirmation is you followed correctly.
databasedone.jpg

Note:

All screenshots in this post were taken by me, the video was shot by me using bandicam free version, this has not been posted anywhere else.

Also check out my Youtube Channel for more tutorial videos.



Posted on Utopian.io - Rewarding Open Source Contributors

Post Updated...

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

  • Hello @official-hord, The repository you given are belongs to utopian but your lesson is about JAVA programming.
  • In the video tutorials we expect from authors to give their lessons by their voices.
  • Please do not feel that you are not a good teacher but you need some changes in order to follow utopian rules. Thanks in advance!

You can contact us on Discord.
[utopian-moderator]

Alright thanks will work on that...

This is really a good Java tutorial.

@abuja-steem just upvoted and resteemed this post. Help us to do more by upvoting this comment

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.036
BTC 109587.97
ETH 4474.98
USDT 1.00
SBD 0.84