Question
existing code:-> Class Person: import java.math.BigInteger; public class Person { private long ssn; private String firstName; private String lastName; private int age; private long creditCard;
existing code:->
Class Person:
import java.math.BigInteger;
public class Person {
private long ssn;
private String firstName;
private String lastName;
private int age;
private long creditCard;
public Person(String firstName, String lastName, int age, long creditCard, long ssn) {
this.ssn = ssn;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.creditCard = creditCard;
}
public Person() {
}
public long getSsn() {
return ssn;
}
public void setSsn(long ssn) {
this.ssn = ssn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getCreditCard() {
return creditCard;
}
public void setCreditCard(long creditCard) {
this.creditCard = creditCard;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Person))
return false;
Person person = (Person) o;
return getSsn() == person.getSsn();
}
@Override
public int hashCode() {
return (int) (getSsn() ^ (getSsn() >>> 32));
}
@Override
public String toString() {
return "Person{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", age=" + age
+ ", creditCard=" + creditCard + ", ssn=" + ssn + '}';
}
}
Class Test:
import java.sql.*;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
Person person1 = new Person("Deep", "Shah", 13, 5136113, 12346);
Person person2 = new Person("Ravi", "Patel", 14, 5136114, 12347);
Person person3 = new Person("Anup", "Jain", 15, 5136115, 12348);
Person person4 = new Person("Chirag", "Patel", 16, 5136116, 12349);
Person person5 = new Person("Mitul", "Shah", 17, 5136117, 12350);
createTable();
insertPerson(person1);
insertPerson(person2);
insertPerson(person3);
insertPerson(person4);
insertPerson(person5);
System.out.println(selectPerson(4));
for (Person p : findAllPeople()) {
System.out.println(p);
}
deletePerson(4);
for (Person p : findAllPeople()) {
System.out.println(p);
}
}
public static Connection deletePerson(int id) {
Connection connection = null;
Statement stmt = null;
try {
connection = getConnection();
connection.setAutoCommit(false);
stmt = connection.createStatement();
String sql = "DELETE from MARK where ID=" + id + ";";
stmt.executeUpdate(sql);
connection.commit();
connection.close();
System.out.println("(Deleted Person " + id + ") done successfully");
} catch (Exception e) {
System.out.println(e);
connection = null;
}
return connection;
}
public static ArrayList
Connection connection = null;
Statement statement = null;
ArrayList
try {
connection = getConnection();
connection.setAutoCommit(false);
statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM MARK;");
while (rs.next()) {
person.add(new Person(rs.getString("firstname"), rs.getString("lastname"), rs.getInt("age"),
rs.getInt("creditcard"), rs.getInt("ssn")));
}
rs.close();
statement.close();
connection.close();
System.out.println("(Find All People) done successfully");
} catch (Exception e) {
System.out.println(e);
person = null;
}
return person;
}
public static Person selectPerson(int id) {
Connection connection = null;
Statement statement = null;
Person person = new Person();
try {
connection = getConnection();
connection.setAutoCommit(false);
statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM MARK where ID=" + id + ";");
while (rs.next()) {
person.setFirstName(rs.getString("firstname"));
person.setLastName(rs.getString("lastname"));
person.setAge(rs.getInt("age"));
person.setCreditCard(rs.getInt("creditcard"));
person.setSsn(rs.getInt("ssn"));
}
rs.close();
statement.close();
connection.close();
System.out.println("(Select Person " + id + ") done successfully");
} catch (Exception e) {
System.out.println(e);
person = null;
}
return person;
}
public static Connection insertPerson(Person person) {
Connection connection = null;
Statement statement = null;
try {
connection = getConnection();
connection.setAutoCommit(false);
statement = connection.createStatement();
String sql = "INSERT INTO MARK (FIRSTNAME,LASTNAME,AGE,CREDITCARD,SSN) " + "VALUES (" + "'"
+ person.getFirstName() + "'," + "'" + person.getLastName() + "'," + person.getAge() + ","
+ person.getCreditCard() + "," + person.getSsn() + " );";
statement.executeUpdate(sql);
statement.close();
connection.commit();
connection.close();
System.out.println(
"(Insert Person " + person.getFirstName() + " " + person.getLastName() + ") done successfully");
} catch (Exception e) {
System.out.println(e);
connection = null;
}
return connection;
}
public static Connection createTable() {
Connection connection = null;
Statement statement = null;
try {
connection = getConnection();
statement = connection.createStatement();
String sql = "CREATE TABLE MARK " + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "FIRSTNAME CHAR(30) NOT NULL, " + "LASTNAME CHAR(30) NOT NULL, "
+ "AGE INT , " + "CREDITCARD BIGINT , "
+ "SSN BIGINT ) ";
statement.executeUpdate(sql);
statement.close();
connection.close();
System.out.println("Table created successfully");
} catch (Exception e) {
System.out.println(e);
connection = null;
}
return connection;
}
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:MyDatabase.db");
System.out.println("Opened database successfully");
} catch (Exception e) {
System.out.println(e);
connection = null;
}
return connection;
}
}
In this project we want to pull together many of the things we learned in the semester, and create a small Java system that includes a GUI, custom business classes, and a database.
This is a typical 3 layer architecture. Now that we know about all the pieces, its good to think about where they go. Its important to have an orgainziation. We would like to have all of the GUI in one place, and the DB code all in another place. Its not a difficult concept.
<< < My hope with this project is that you will decide what the project does. Look at the specification below. Its not too interesting, and describes a minimal project for which you can receive a perfect grade. If you prefer to design a project that demonstartes a 3 layer architecture, but the subject doesnt look like the description below, I encourage you to do that. Dont be too ambitious. There is a very limited amount of time. >>>
Here is a demonstration for getting started:
https://www.youtube.com/watch?feature=player_embedded&v=VbuLXze6xf8
The code in that video is attached as a zip file.
1. 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.
2. 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.)
3. Create a connection to a relational database using SQLite or MySQL.
4. 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.
5. 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.
6. 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.
7. Demonstrate the retrieval of information from the database. Use SQL Select statements.
8. 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.
9. Write a method called findAllPeople that returns an ArrayList of objects containing all the people in the database.
10. 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?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started