0% found this document useful (0 votes)
82 views25 pages

Database Miniproject

The document describes an embedded SQL connection class for connecting to a database and performing CRUD operations on customer records. The class includes methods for getting a database connection, closing the connection, retrieving customer information based on name, adding a new customer, removing a customer, and updating a customer. It also describes a main frame GUI class with panels and buttons for searching for customers, displaying customer information, and allowing the user to add, delete, or update a customer record.

Uploaded by

Ruthara Devan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views25 pages

Database Miniproject

The document describes an embedded SQL connection class for connecting to a database and performing CRUD operations on customer records. The class includes methods for getting a database connection, closing the connection, retrieving customer information based on name, adding a new customer, removing a customer, and updating a customer. It also describes a main frame GUI class with panels and buttons for searching for customers, displaying customer information, and allowing the user to add, delete, or update a customer record.

Uploaded by

Ruthara Devan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

SOURCE CODE

EmbeddedSQLConnection

package database;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.*;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import objects.SQLCUSTOMER;

public class EmbeddedSQLConnection

private Connection con;

public EmbeddedSQLConnection()

public void getConnection()

try

if(con==null)

Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();

con=DriverManager.getConnection("jdbc:db2://localhost:50001/SAMPLE","db2inst1","password");

}
System.out.println(con);

catch (SQLException e)

System.out.println(e.getMessage());

System.out.println(e.getErrorCode());

e.printStackTrace();

catch (Exception e)

e.printStackTrace();

public void closeConnection()

try

con.close();

con = null;

catch (SQLException e)

// TODO Auto-generated catch block

e.printStackTrace();

public boolean getCustomerInformation(SQLCUSTOMER u, String name)

{
PreparedStatement s = null;

try

String query = "SELECT customerID, name, address, city, postal_code, phone_number,


email FROM SQLCUSTOMER WHERE name = ?";

s = con.prepareStatement(query);

s.setString(1, name);

ResultSet rs=s.executeQuery();

while(rs.next())

u.changeCustomerID(rs.getInt(1));

u.changeName(rs.getString(2));

u.changeAddress(rs.getString(3));

u.changeCity(rs.getString(4));

u.changePostalCode(rs.getString(5));

u.changePnoneNumber(rs.getString(6));

u.changeEmail(rs.getString(7));

catch (SQLException e1)

// TODO Auto-generated catch block

e1.printStackTrace();

return true;

public boolean addCustomer(int customerID, String name, String address, String city, String
postal_code, String phone_number, String email)

{
PreparedStatement s = null;

try

String query = "INSERT INTO SQLCUSTOMER (customerID, name, address, city,


postal_code, phone_number, email) VALUES (?,?,?,?,?,?,?)";

System.out.println(name);

s = con.prepareStatement(query);

s.setInt(1, customerID);

s.setString(2, name);

s.setString(3, address);

s.setString(4, city);

s.setString(5, postal_code);

s.setString(6, phone_number);

s.setString(7, email);

s.execute();

catch (SQLException e1)

// TODO Auto-generated catch block

e1.printStackTrace();

return true;

public boolean removeCustomer(int customerID)


{

PreparedStatement s = null;

try

String query = "DELETE FROM SQLCUSTOMER WHERE customerID = ?";

s = con.prepareStatement(query);

s.setInt(1, customerID);

s.execute();

catch (SQLException e1)

// TODO Auto-generated catch block

e1.printStackTrace();

return true;

public boolean updateCustomer(int customerID, String name, String address, String city, String
postal_code, String phone_number, String email)

PreparedStatement s = null;

try

String query = "Update SQLCUSTOMER SET (name, address, city, postal_code,


phone_number, email) = (?,?,?,?,?,?) WHERE customerID = ?";

s = con.prepareStatement(query);

s.setString(1, name);

s.setString(2, address);

s.setString(3, city);
s.setString(4, postal_code);

s.setString(5, phone_number);

s.setString(6, email);

s.setInt(7, customerID);

s.execute();

catch (SQLException e1)

// TODO Auto-generated catch block

e1.printStackTrace();

return true;

}
MainFrame.jave

package gui;

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import database.EmbeddedSQLConnection;

import objects.SQLCUSTOMER;

public class MainFrame extends JFrame {

// Declare User

SQLCUSTOMER u = new SQLCUSTOMER();

EmbeddedSQLConnection x = new EmbeddedSQLConnection();

/***********************************************************************

* NORTH PANEL DECLARATIONS

**********************************************************************/

private JPanel northPanel = new JPanel();

JTextField name = new JTextField(20);

JButton search = new JButton("Search");

/***********************************************************************

* SOUTH PANEL DECLARATIONS

**********************************************************************/

private JPanel southPanel = new JPanel();

JButton customerInformation = new JButton("Customer Information");

/***********************************************************************

* CUSTOMER PANEL DECLARATIONS

**********************************************************************/

JPanel customerInformationPanel = new JPanel();

JTextField customerID = new JTextField(20);


JTextField eName = new JTextField(20);

JTextField address = new JTextField(20);

JTextField city = new JTextField(20);

JTextField postal_code = new JTextField(20);

JTextField phone_number = new JTextField(20);

JTextField email = new JTextField(20);

JButton add = new JButton("Add");

JButton delete = new JButton("Delete");

JButton update = new JButton("Update");

JPanel addCustomerPanel = new JPanel();

JButton addToDB = new JButton("Add");

public MainFrame()

super();

/***********************************************************************

* NORTH PANEL

**********************************************************************/

// Add contents to northPanel

northPanel.add(new JLabel("Name:"));

northPanel.add(name);

northPanel.add(search);

search.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

Search();

});

add(northPanel, BorderLayout.NORTH);
/***********************************************************************

* SOUTH PANEL

**********************************************************************/

// Add contents to southPanel

southPanel.setLayout(new GridLayout(1, 1));

southPanel.add(customerInformation);

customerInformation.setEnabled(false);

southPanel.add(add);

customerInformation.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

CustomerInformation();

});

add.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

Add();

});

add(southPanel, BorderLayout.SOUTH);

public void Search()

JPanel searchPanel = new JPanel();

searchPanel.setLayout(new GridLayout(0, 1));

//5.1 Incorporating SELECT with the Application

x.getConnection();
x.getCustomerInformation(u, name.getText());

x.closeConnection();

System.out.println("text = " + name.getText() + " name = " );

if (name.getText().equals (u.getName()))

customerID.setText("");

eName.setText("");

address.setText("");

city.setText("");

postal_code.setText("");

phone_number.setText("");

email.setText("");

customerInformation.setEnabled(true);

CustomerInformation();

else

InvalidNameMessage();

public void CustomerInformation()

customerInformationPanel.removeAll();

customerInformationPanel.setBorder(BorderFactory

.createTitledBorder("Customer Information"));

customerInformationPanel.setLayout(new GridLayout(11, 2));

customerInformationPanel.add(new JLabel(new String("Customer ID: " )));

customerInformationPanel.add(customerID);
customerID.setEnabled(true);

customerInformationPanel.add(new JLabel(new String("Name: " )));

customerInformationPanel.add(eName);

customerInformationPanel.add(new JLabel(new String("Address: " )));

customerInformationPanel.add(address);

customerInformationPanel.add(new JLabel(new String("City: " )));

customerInformationPanel.add(city);

customerInformationPanel.add(new JLabel(new String("Postal Code: " )));

customerInformationPanel.add(postal_code);

customerInformationPanel.add(new JLabel(new String("Telephone Number: " )));

customerInformationPanel.add(phone_number);

customerInformationPanel.add(new JLabel(new String("Email: " )));

customerInformationPanel.add(email);

customerID.setText(String.valueOf(u.getCustomerID()));

eName.setText(u.getName());

address.setText(u.getAddress());

city.setText(u.getCity());

postal_code.setText(u.getPostalCode());

phone_number.setText(u.getPhoneNumber());

email.setText(u.getEmail());

customerInformationPanel.add(delete);

customerInformationPanel.add(update);

delete.setEnabled(true);

update.setEnabled(true);

delete.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)


{

Delete();

});

update.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

Update();

});

JOptionPane.showMessageDialog(null, customerInformationPanel);

public void Add()

customerID.setText("");

eName.setText("");

address.setText("");

city.setText("");

postal_code.setText("");

phone_number.setText("");

email.setText("");

addCustomerPanel.removeAll();

addCustomerPanel.setBorder(BorderFactory

.createTitledBorder("Customer Information"));

addCustomerPanel.setLayout(new GridLayout(12, 2));

// db2

addCustomerPanel.add(new JLabel(new String("Customer ID: ")));


addCustomerPanel.add(customerID);

addCustomerPanel.add(new JLabel(new String("Name: " )));

addCustomerPanel.add(eName);

addCustomerPanel.add(new JLabel(new String("Address: " )));

addCustomerPanel.add(address);

addCustomerPanel.add(new JLabel(new String("City: " )));

addCustomerPanel.add(city);

addCustomerPanel.add(new JLabel(new String("Postal Code: " )));

addCustomerPanel.add(postal_code);

addCustomerPanel.add(new JLabel(new String("Telephone Number: " )));

addCustomerPanel.add(phone_number);

addCustomerPanel.add(new JLabel(new String("Email: " )));

addCustomerPanel.add(email);

addCustomerPanel.add(addToDB);

addToDB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

AddToDB();

});

JOptionPane.showMessageDialog(null, addCustomerPanel);

public void AddToDB()

String stringText;

int intText = 0;

stringText = new String(customerID.getText());


try

intText = Integer.parseInt(stringText);

} catch (NumberFormatException e)

if (intText != 0)

//6.1 Incorporating INSERT with the Application

x.getConnection();

x.addCustomer(intText, eName.getText(), address.getText(), city.getText(),


postal_code.getText(), phone_number.getText(), email.getText());

x.closeConnection();

JOptionPane.showMessageDialog(null, eName.getText() + " has been added to the


Database");

public void Delete()

//db2

x.getConnection();

x.removeCustomer(u.getCustomerID());

x.closeConnection();

u.removeCustomer();

JOptionPane.showMessageDialog(null, "Customer has been deleted from the Database");

delete.setEnabled(false);

update.setEnabled(false);

}
public void Update()

//db2

x.getConnection();

x.updateCustomer(u.getCustomerID(), eName.getText(), address.getText(), city.getText(),


postal_code.getText(), phone_number.getText(), email.getText());

x.closeConnection();

u.removeCustomer();

JOptionPane.showMessageDialog(null, "Customer has been updated in the Database");

delete.setEnabled(false);

update.setEnabled(false);

/**

* Task: Notify User of incorrect name compared to the SQLCUSTOMER Table.

*/

public void InvalidNameMessage()

JOptionPane.showMessageDialog(null, "This Customer does not exist in the Database");

}
Main.java

package gui;

import javax.swing.JFrame;

public class Main {

public static void main(String[] args)

MainFrame mainPage = new MainFrame();

mainPage.setTitle("Embedded SQL in an Application");

mainPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainPage.setSize(500, 100);

mainPage.setVisible(true);

}
SQLCUSTOMER.java

package objects;

public class SQLCUSTOMER {

private int customer_ID = 0;

private String name = null;

private String address = null;

private String city = null;

private String postal_code = null;

private String phone_number = null;

private String email = null;

public SQLCUSTOMER()

public void changeCustomerID(int customerID)

this.customer_ID = customerID;

public void changeName(String name)

this.name = name;

public void changeAddress(String address)

this.address = address;

public void changeCity(String city)

this.city = city;

}
public void changePostalCode(String postalCode)

this.postal_code = postalCode;

public void changePnoneNumber(String phoneNumber)

this.phone_number = phoneNumber;

public void changeEmail(String email)

this.email = email;

public int getCustomerID()

return customer_ID;

public String getName()

return name;

public String getAddress()

return address;

public String getCity()

return city;

}
public String getPostalCode()

return postal_code;

public String getPhoneNumber()

return phone_number;

public String getEmail()

return email;

public Boolean removeCustomer()

customer_ID = 0;

name = null;

address = null;

city = null;

postal_code = null;

phone_number = null;

email = null;

return true;

}
USER MANUAL

Add Customer
Search Customer
Update Customer
Search after Update
Delete after Customer
Search after Delete

You might also like