Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//In JAVA Please There is 4 Java classes below to build the program Create a 3 layer application in Java, where the top layer is

//In JAVA Please

There is 4 Java classes below to build the program

Create a 3 layer application in Java, where the top layer is the User Interface, the middle layer is the business layer, and the bottom layer is the database connectivity.

The user interface will present a GUI to the user, asking them to enter some information, and to interact with the stored data. Refer to your previous assignments. You can use your code or start from scratch. (The exact functionality provided to the user is deliberately vague here.)

Create a connection to a relational database using SQLite.

Create a single database table to hold information. Go back to the previous assignments, and re-use your classes, or modify them as you like. Recall that you will create the DB only once. You can just comment out the code that creates it if you like. Some people find a way to detect if the DB is already there. Thats fine, but not required.

These requirements are taken from the previous assignment. This time the user is to be presented with a GUI that allows these things to be done. So the data is not hard coded, the user will fill in a GUI click a button to interact with the DB. When you retrieve the data from the database it should populate the GUI to demonstrate that it is working.

Demonstrate the insertion of a record into the database. Create a method in the Data layer that takes a Person as a parameter, and puts that data in the database. Insert several records.

Demonstrate the retrieval of information from the database. Use SQL/SQLite Select statements.

Write a method called getPerson 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 thats 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. Note that this is the data-exchange between the relational database and the business layer.

Write a method called findAllPeople that returns an ArrayList of objects containing all the people in the database.

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?

//AppData.java

package businesslayer;

import java.sql.Connection;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.List;

import datalayer.DatabaseConnection;

// This is an example of using the Singleton pattern to make the application's data available throughout the

// application while guaranteeing that there is only one copy of it.

public class AppData {

private List people = new ArrayList();

// this is the reference to the single instance of the AppData class

private static AppData appData = null;

// A private constructor that is only called one time

private AppData() {

}

// A public method to make the app Data available throughout the application.

// The first time the method is called, the Single instance of AppData is created,

// each subsequent time, the one data object created is returned.

public static AppData getAppData(){

if(appData == null){

appData = new AppData();

}

return appData;

}

// example of a method to change the appData from throughout the project

// there might be lots of there to add / remove data.

public void addPerson(Person person){

people.add(person); // this adds the object to the datastructures in RAM

try {

Connection con = DatabaseConnection.getConnection();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Statement stmt;

// make the insert into the database.

}

}

//Person.java

package businesslayer;

public class Person {

private String name;

public Person(String name) {

super();

this.name = name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return "Person [name=" + name + "]";

}

}

//DatabaseConnection.java

package datalayer;

import java.sql.Connection;

import java.sql.SQLException;

public class DatabaseConnection {

private static Connection conn;

public static Connection getConnection() throws SQLException {

if (conn == null || conn.isClosed()) {

conn = magicallyCreateNewConnection();

}

return conn;

}

private static Connection magicallyCreateNewConnection() {

// TODO Auto-generated method stub

return null;

}

}

//MainGUI.java

package userinterface;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import businesslayer.AppData;

import businesslayer.Person;

public class MainGUI extends JFrame {

public static void main(String[] args) {

JFrame myMainFrame = new MainGUI();

myMainFrame.setTitle("Hey I'm the Title");

myMainFrame.setSize(400, 300);

myMainFrame.setLocation(100, 100);

myMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

myMainFrame.setVisible(true);

JButton myButton = new JButton("Click Here");

myMainFrame.setLayout(new FlowLayout());

myMainFrame.add(myButton);

MyButtonHandler mbh = new MyButtonHandler();

myButton.addActionListener(mbh);

JButton otherButton = new JButton("Other Button");

myMainFrame.add(otherButton);

otherButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

System.out.println("I hear the other button!!!!!");

// maybe it's time to add to the application's data

AppData.getAppData().addPerson( new Person("Bob") );

}

});

}

static class MyButtonHandler implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

System.out.println("I hear the button!");

System.out.println(e);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle PL/SQL Programming Database Management Systems

Authors: Steven Feuerstein

1st Edition

978-1565921429

More Books

Students also viewed these Databases questions