Part I – Build a Customer Business Object. This class will have 6 properties, CustId, CustPassword, CustFirstName, CustLastName, CustAddress and CustEmail. Build an empty args Constructor in the Customer class that initializes all properties to 0 or “”. Also build a constructor that takes all 6 args and set the appropriate properties. Then build the following 3 methods:
selectDB(custID); //to find a customer’s info in the DB
insertDB(custId, custPassword, custFirstName, custLastName, custAddress, custEmail) // to insert a new Customer in the DB
deleteDB(); //this method will delete the Customer from the DB
//so to check login execute the following code à
Customer c1 = new Customer(); //creates empty object
c1.selectDB(id); //does the DB lookup to find Customer
String pwdb = c1.getPassword();
if (pw.equals(pwdb)) { //this compares pw(from gui to the
//password from the database
//login correct
}
else {
//login incorrect
}
Part II – Now build an Account Business Object. This class will have all of the database access code to lookup an Account in the database. Build a selectDB(acctNo) method in the Account class that accepts an integer acctNo. This method will then go to the database and find all information about the Account requested. Then, in the next lab, when our Servlet wants to find an Account in the DB, all we have to do is à
Account a1 = new Account();
a1.selectDB(acctNo);
abalance = a1.getBalance();
Expert Answer
Part 1:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class Customer_Business_Object {
/*
* Below are the variables initiated
*/
private String CustId;
private String CustPassword;
private String CustFirstName;
private String CustLastName;
private String CustAddress;
private String CustEmail;
/*
* Constructor without arguments. This will also set the values of variables to “0” when called
*/
public Customer_Business_Object(){
super();
CustId = “0”;
CustPassword = “0”;
CustFirstName = “0”;
CustLastName = “0”;
CustAddress = “0”;
CustEmail = “0”;
}
/*
* constructor with arguments
*/
public Customer_Business_Object(String custId, String custPassword, String custFirstName, String custLastName,
String custAddress, String custEmail) {
super();
CustId = custId;
CustPassword = custPassword;
CustFirstName = custFirstName;
CustLastName = custLastName;
CustAddress = custAddress;
CustEmail = custEmail;
}
/*
* selectDB method. returns true if CustId exists else returns false
*/
static boolean selectDB(String custID)
{
boolean flag = false;
try
{
/*
* Below is the code required to create a connection with the database.This is for MySql database
*/
String myDriver = “org.gjt.mm.mysql.Driver”;
/*
* one thing you must take care is check the username and password of your database
*/
String myUrl = “jdbc:mysql://localhost/test”;
Class.forName(myDriver);
/*
* below root is the username, and the empty string is password. change as per your credentials. Check for the other methods too
*/
Connection conn = DriverManager.getConnection(myUrl, “root”, “”);
// our SQL SELECT query.
// if you only need a few columns, specify them by name
PreparedStatement statement = conn.prepareStatement(“SELECT CustId FROM customer_table”);
// execute the query, and get a java resultset
ResultSet rs = statement.executeQuery();
while(rs.next())
{
/*
* comparing CustId with the value in result set if exists flag is made true
*/
if(custID.equalsIgnoreCase(rs.getString(1)))
{
flag = true;
}
}
/*
* closing the connection with the database
*/
conn.close();
}catch(Exception e){
System.out.print(e);
}
return flag;
}
static void insertDB(String custId,String custPassword,String custFirstName,String custLastName,String custAddress,String custEmail)
{
try{
// create our mysql database connection
String myDriver = “org.gjt.mm.mysql.Driver”;
String myUrl = “jdbc:mysql://localhost/test”;
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, “root”, “”);
// our SQL SELECT query.
PreparedStatement statement = conn.prepareStatement(“Insert into customer_table(custId, custPassword, custFirstName, custLastName, custAddress, custEmail) values(?,?,?,?,?,?)”);
/*
* Here we are setting the values that must be entered into the database
* The order of below elements must be same as customer_table(custId, custPassword, custFirstName, custLastName, custAddress, custEmail)
*/
statement.setString(1, custId);
statement.setString(2, custPassword);
statement.setString(3, custFirstName);
statement.setString(4, custLastName);
statement.setString(5, custAddress);
statement.setString(6, custEmail);
/*
* executing the query
*/
statement.executeUpdate();
/*
* closing the connection
*/
conn.close();
}catch(Exception e){
System.out.print(e);
}
}
static void deleteDB(String custId)
{
try{
// create our mysql database connection
String myDriver = “org.gjt.mm.mysql.Driver”;
String myUrl = “jdbc:mysql://localhost/test”;
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, “root”, “”);
PreparedStatement statement = conn.prepareStatement(“DELETE FROM customer_table WHERE name = ?”);
statement.setString(1,custId);
statement.executeUpdate();
conn.close();
}catch(Exception e){
System.out.print(e);
}
}
}
Part 2:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Account_Business_Object
{
private int acctNo;
static void selectDB(int acctNo){
try{
String myDriver = “org.gjt.mm.mysql.Driver”;
String myUrl = “jdbc:mysql://localhost/test”;
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, “root”, “”);
// our SQL SELECT query.
PreparedStatement statement = conn.prepareStatement(“SELECT acctNo FROM Account_table”);
// execute the query, and get a java resultset
ResultSet rs = statement.executeQuery();
while(rs.next())
{
acctNo = rs.getInt(1);
}
conn.close();
}catch(Exception e){
System.out.print(e);
}
}
}
The only concern I have with the above code is, As per your requirement in the question there is no main method to call the other methods. If there is no main method program will not run, As by default compiler search for a main method. Kindly comment if you need any more help. Will be glad to help.