We will use a database in this programming project. Here is a good tutorial for getting started with SQLite. http://www.tutorialspoint.com/sqlite/sqlite_java.htm(Links to an external site.) For this...

1 answer below »

We will use a database in this programming project. Here is a good tutorial for getting started with SQLite.



http://www.tutorialspoint.com/sqlite/sqlite_java.htm(Links to an external site.)


For this assignment you can use SQLite OR MySQL.


There are numerous videos on YouTube and other resources around the web that demonstrate how to do this. Some are better than others. Do some research and find one that is helpful to you.Once you have done so, please post a link to it on the discussion board and discuss what you found helpful.


We have been working with the front-end (GUI), and the middle (creating and manipulating collections of objects), and now we will add on the back end. The persistent storage of data in your applications. This exercise is to get you comfortable with connecting to a DB, adding, deleting, retrieving data. I encourage you to play with this one; do more than the minimum.


SQLite is a very small database. It is included by default in Android and iOS. It is surprisingly powerful for such a small footprint. It can be frustrating to see what’s going on – what is in the DB, did the query work correctly? MySQL is often called a community database. It belongs to Oracle, but they allow anyone to use it for free. The recent versions of the MySQL workbench that allows you to see what’s going on in your database are really very nice – starting to look like the Access front end.



  1. Create a connection to a relational database using SQLite or MySQL.

  2. Create a single database table to hold information.


Let’s make a simple class called Person for this exercise.


Person



  • firstName (String)

  • lastName(String)

  • age (int)

  • ssn (long)

  • creditCard (long)


Note that once you have the DB created, you don’t want to do this again every time you run your test program. The easiest way to deal with this – for this assignment - is to comment out the code that creates the DB creation and the table creation while you experiment with the following.
(Aside: I choose ssn and credit card as fields here so that you might think about the persistent storage of sensitive data. There are some pretty strict laws governing the storage of some data. Please don’t use any actual social security numbers or credit card numbers in this exercise.)




  1. Demonstrate the insertion of a record into the database. Insert several records.




  1. Write a method called insertPerson(Person person) that adds a person object to your database. Create another object of type Person, and demonstrate calling your method, passing the object to the method.




  1. Demonstrate the retrieval of information from the database. Use SQL Select statements, to retrieve a particular Person from the database.




  1. Write a method called selectPerson that returns a Person object. This method retrieves the data for a Person from the database. We also need to pass a parameter to identify what person. You can use ‘name’ if you like, or if you find it easier to use the database generated ID that’s fine too.
    This method returns the object that represents that person. This will require that you extract the data that is returned from the database, and call the Person constructor. (This is the data-exchange between the relational database and the business layer. )




  1. Write a method called findAllPeople that returns an ArrayList of objects containing all the people in the database. Demonstrate that it is working correctly.




  1. Write a method called deletePerson that removes a person from the database. The parameters will be first name and last name. Print out on the console the data from the record that is being deleted. Use your findAllPeople method to verify that that person has been removed from the database. Consider what this method should return. Suppose the person is not found, should the method return that information somehow?



Learning Outcomes



  • The student will be able to write code that creates and modifies a relational database.



Submission Instructions



  1. Please submit one or more .java files as required.

  2. Please submit screenshot(s) of the program running on your computer. Screenshot(s) should include all output.


Your source code must produce the output in your screenshots!

Answered 1 days AfterApr 20, 2021

Answer To: We will use a database in this programming project. Here is a good tutorial for getting started with...

Kshitij answered on Apr 22 2021
150 Votes
DatabaseConnection.java
DatabaseConnection.java
/*   Created by IntelliJ IDEA.
 *   Author: Kshitij Varshney (kshitijvarshne1)
 *   Date: 22-Apr-21
 *   Time: 6:34 PM
 *   File: DatabaseConnection.java
 */
package April.api22_21;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQL
Exception;
public class DatabaseConnection {
    private static  final String URL ="jdbc:mysql://localhost:3306/greynodes";
    private static final String USERNAME="root";
    private static final String PASSWORD="";

    private static Connection connection = null;
    public static Connection getConnection() {
        return connection;
    }
    public void connectToDatabase() {
        try {
            connection= DriverManager.getConnection(
                    URL,USERNAME,PASSWORD
            );
        } catch (SQLException exception) {
            System.err.println("Could not coonect to database please check your URL, USERNAME or PASSWORD ");
        }
        finally {
            printConnectionStatus();
        }
    }
    private void printConnectionStatus() {
        System.out.println(
                connection==null?"The connection to the databse is inaction":
                        "The connection to the database is active"
        );
    }
}
Main.java
Main.java
/*   Created by IntelliJ IDEA.
 *   Author: Kshitij Varshney (kshitijvarshne1)
 *   Date: 22-Apr-21
 *   Time: 6:53 PM
 *   File: Main.java
 */
package April.api22_21;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        DatabaseConnection connection = new DatabaseConnection();
        connection.connectToDatabase();
        PersonHelper personHelper = new PersonHelper();
        Scanner sc = new Scanner(System.in);
        System.out.println("==== HELLO ====");
        show();
        int key = sc.nextInt();
        while(true){
           if(key>=1 && key<= 5){
                switch (key){
                    case 1:
                        System.out.println("Enter the first name");
                        String firstName= sc.next();
                        System.out.println("Enter the last name");
                        String lastName = sc.next();
                        System.out.println("Enter the age");
                        int age = sc.nextInt();
                        System.out.println("Enter the ssn number");
                        long ssn= sc.nextLong();
                        System.out.println("Enter the credit card number");
                        long creditCard = sc.nextLong();
                        Person newPerson = new Person(firstName,lastName,age,ssn,creditCard);
                        try {
                            personHelper.insertPerson(DatabaseConnection.getConnection(),newPerson);
                            System.out.println("Person is added successfully");
                        }
                        catch (SQLException ex){
                            System.out.println("Could not add the pe...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here