Database Miniproject
Database Miniproject
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 EmbeddedSQLConnection()
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();
try
con.close();
con = null;
catch (SQLException e)
e.printStackTrace();
{
PreparedStatement s = null;
try
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));
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
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();
e1.printStackTrace();
return true;
PreparedStatement s = null;
try
s = con.prepareStatement(query);
s.setInt(1, customerID);
s.execute();
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
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();
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;
// Declare User
/***********************************************************************
**********************************************************************/
/***********************************************************************
**********************************************************************/
/***********************************************************************
**********************************************************************/
public MainFrame()
super();
/***********************************************************************
* NORTH PANEL
**********************************************************************/
northPanel.add(new JLabel("Name:"));
northPanel.add(name);
northPanel.add(search);
search.addActionListener(new ActionListener() {
Search();
});
add(northPanel, BorderLayout.NORTH);
/***********************************************************************
* SOUTH PANEL
**********************************************************************/
southPanel.add(customerInformation);
customerInformation.setEnabled(false);
southPanel.add(add);
customerInformation.addActionListener(new ActionListener() {
CustomerInformation();
});
add.addActionListener(new ActionListener() {
Add();
});
add(southPanel, BorderLayout.SOUTH);
x.getConnection();
x.getCustomerInformation(u, name.getText());
x.closeConnection();
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();
customerInformationPanel.removeAll();
customerInformationPanel.setBorder(BorderFactory
.createTitledBorder("Customer Information"));
customerInformationPanel.add(customerID);
customerID.setEnabled(true);
customerInformationPanel.add(eName);
customerInformationPanel.add(address);
customerInformationPanel.add(city);
customerInformationPanel.add(postal_code);
customerInformationPanel.add(phone_number);
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() {
Delete();
});
update.addActionListener(new ActionListener() {
Update();
});
JOptionPane.showMessageDialog(null, customerInformationPanel);
customerID.setText("");
eName.setText("");
address.setText("");
city.setText("");
postal_code.setText("");
phone_number.setText("");
email.setText("");
addCustomerPanel.removeAll();
addCustomerPanel.setBorder(BorderFactory
.createTitledBorder("Customer Information"));
// db2
addCustomerPanel.add(eName);
addCustomerPanel.add(address);
addCustomerPanel.add(city);
addCustomerPanel.add(postal_code);
addCustomerPanel.add(phone_number);
addCustomerPanel.add(email);
addCustomerPanel.add(addToDB);
addToDB.addActionListener(new ActionListener() {
AddToDB();
});
JOptionPane.showMessageDialog(null, addCustomerPanel);
String stringText;
int intText = 0;
intText = Integer.parseInt(stringText);
} catch (NumberFormatException e)
if (intText != 0)
x.getConnection();
x.closeConnection();
//db2
x.getConnection();
x.removeCustomer(u.getCustomerID());
x.closeConnection();
u.removeCustomer();
delete.setEnabled(false);
update.setEnabled(false);
}
public void Update()
//db2
x.getConnection();
x.closeConnection();
u.removeCustomer();
delete.setEnabled(false);
update.setEnabled(false);
/**
*/
}
Main.java
package gui;
import javax.swing.JFrame;
mainPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPage.setSize(500, 100);
mainPage.setVisible(true);
}
SQLCUSTOMER.java
package objects;
public SQLCUSTOMER()
this.customer_ID = customerID;
this.name = name;
this.address = address;
this.city = city;
}
public void changePostalCode(String postalCode)
this.postal_code = postalCode;
this.phone_number = phoneNumber;
this.email = email;
return customer_ID;
return name;
return address;
return city;
}
public String getPostalCode()
return postal_code;
return phone_number;
return email;
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