Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Model;
- public class Family {
- public String familyId,income,members, contactNumber;
- public String familyName,address;
- public String getFamilyId() {
- return familyId;
- }
- public void setFamilyId(String familyID) {
- this.familyId = familyID;
- }
- public String getIncome() {
- return income;
- }
- public void setIncome(String income) {
- this.income = income;
- }
- public String getMembers() {
- return members;
- }
- public void setMembers(String members) {
- this.members = members;
- }
- public String getContactNumber() {
- return contactNumber;
- }
- public void setContactNumber(String contactNumber) {
- this.contactNumber = contactNumber;
- }
- public String getFamilyName() {
- return familyName;
- }
- public void setFamilyName(String familyName) {
- this.familyName = familyName;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public Family() {
- super();
- // TODO Auto-generated constructor stub
- }
- }
- package Controller;
- import java.sql.*;
- public class Driver {
- public static Connection getConnection(){
- Connection conn = null;
- try{
- conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/oop","root","");
- System.out.println("CONNECTED");
- }
- catch(Exception a){
- System.out.println("NOT CONNECTED");
- }
- return conn;
- }
- }
- package Controller;
- import java.util.*;
- import java.sql.*;
- import Model.*;
- public class Controller {
- public List <Family> findAll(){
- try{
- List <Family> listFamily = new ArrayList<>();
- PreparedStatement pst = Driver.getConnection().prepareStatement ("Select * from addFamily");
- ResultSet rs = pst.executeQuery();
- while (rs.next())
- {
- Family e= new Family();
- e.setFamilyId(rs.getString("familyId"));
- e.setFamilyName(rs.getString("familyName"));
- e.setAddress(rs.getString("address"));
- e.setContactNumber(rs.getString("contactNumber"));
- e.setIncome(rs.getString("income"));
- e.setMembers(rs.getString("members"));
- listFamily.add(e);
- }
- return listFamily;
- }
- catch(Exception b){
- return null;
- }
- }
- public boolean create(Family e){
- try{
- PreparedStatement pst = Driver.getConnection().prepareStatement("insert into summer(familyId,familyName,address,contactNumber,income,members)values(?,?,?,?,?,?)");
- pst.setString(1,e.getFamilyId());
- pst.setString(2,e.getFamilyName());
- pst.setString(3,e.getAddress());
- pst.setString(4,e.getContactNumber());
- pst.setString(5,e.getIncome());
- pst.setString(6,e.getMembers());
- System.out.println("RECORDED");
- return pst.executeUpdate()>0;
- }
- catch(Exception c){
- System.out.println("NOT RECORDED");
- return false;
- }
- }
- public boolean update(Family f){
- try{
- PreparedStatement pst = Driver.getConnection().prepareStatement("update summer set FamilyName=? , Address=? , ContactNumber=? , Income=? , Members=? where FamilyID=?");
- pst.setString(1, f.getFamilyName());
- pst.setString(2, f.getAddress());
- pst.setString(3, f.getContactNumber());
- pst.setString(4, f.getIncome());
- pst.setString(5, f.getMembers());
- pst.setString(6, f.getFamilyId());
- System.out.println("Data Updated");
- return pst.executeUpdate()>0;
- }catch(Exception e){
- System.out.println("Data not Updated");
- return false;
- }
- }
- }
- package View;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.Table;
- import org.eclipse.swt.widgets.TableColumn;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.*;
- import javax.swing.JOptionPane;
- import Controller.*;
- import Model.*;
- import org.eclipse.swt.widgets.TableItem;
- import org.eclipse.swt.widgets.Label;
- import org.eclipse.swt.widgets.Text;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.events.SelectionAdapter;
- import org.eclipse.swt.events.SelectionEvent;
- public class View {
- Controller c = new Controller();
- protected Shell shell;
- private Table table;
- private Text textID;
- private Text textFamilyName;
- private Text textAddress;
- private Text textIncome;
- private Text textMembers;
- private Text textContactNumber;
- /**
- * Launch the application.
- * @param args
- */
- public static void main(String[] args) {
- try {
- View window = new View();
- window.open();
- } catch (Exception e) {
- }
- }
- private void fillData(){
- Controller c = new Controller();
- for (Family e: c.findAll()){
- TableItem tableItem = new TableItem (table,SWT.NONE);
- tableItem.setText("TABLE");
- tableItem.setText(new String [] {String.valueOf(e.getFamilyId()) ,(e.getFamilyName()),(e.getAddress()),String.valueOf(e.getContactNumber()),String.valueOf(e.getIncome()),String.valueOf(e.getMembers())});
- }
- }
- private void updateData(){
- Controller c = new Controller();
- for (Family e: c.findAll()){
- TableItem tableItem = new TableItem (table,SWT.NONE);
- tableItem.setText("TABLE");
- tableItem.setText(new String [] {(e.getFamilyId()) ,(e.getFamilyName()),(e.getAddress()),(e.getContactNumber()),(e.getIncome()),(e.getMembers())});
- }
- }
- /**
- * Open the window.
- */
- public void open() {
- Display display = Display.getDefault();
- createContents();
- shell.open();
- shell.layout();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch()) {
- display.sleep();
- }
- }
- }
- /**
- * Create contents of the window.
- */
- protected void createContents() {
- shell = new Shell();
- shell.setSize(704, 442);
- shell.setText("SWT Application");
- table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
- table.setBounds(10, 23, 648, 242);
- table.setHeaderVisible(true);
- table.setLinesVisible(true);
- TableColumn tblclmnNewColumn = new TableColumn(table, SWT.CENTER);
- tblclmnNewColumn.setWidth(48);
- tblclmnNewColumn.setText("ID");
- TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.CENTER);
- tblclmnNewColumn_1.setWidth(142);
- tblclmnNewColumn_1.setText("FAMILY NAME");
- TableColumn tblclmnNewColumn_5 = new TableColumn(table, SWT.CENTER);
- tblclmnNewColumn_5.setWidth(127);
- tblclmnNewColumn_5.setText("CONTACT NUMBER");
- TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.CENTER);
- tblclmnNewColumn_2.setWidth(126);
- tblclmnNewColumn_2.setText("ADDRESS");
- TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.CENTER);
- tblclmnNewColumn_3.setWidth(100);
- tblclmnNewColumn_3.setText("INCOME");
- TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.CENTER);
- tblclmnNewColumn_4.setWidth(100);
- tblclmnNewColumn_4.setText("MEMBERS");
- TableItem tableItem = new TableItem(table, SWT.NONE);
- Label lblId = new Label(shell, SWT.NONE);
- lblId.setBounds(10, 271, 55, 15);
- lblId.setText("Family ID");
- textID = new Text(shell, SWT.BORDER);
- textID.setBounds(94, 268, 165, 21);
- Label lblLastName = new Label(shell, SWT.NONE);
- lblLastName.setBounds(306, 271, 76, 15);
- lblLastName.setText("Family Name");
- Label lblFirstName = new Label(shell, SWT.NONE);
- lblFirstName.setBounds(10, 313, 66, 15);
- lblFirstName.setText("Address");
- textFamilyName = new Text(shell, SWT.BORDER);
- textFamilyName.setBounds(388, 271, 165, 21);
- textAddress = new Text(shell, SWT.BORDER);
- textAddress.setBounds(94, 310, 165, 21);
- Button btnAdd = new Button(shell, SWT.NONE);
- btnAdd.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- Family r = new Family();
- r.setFamilyId(textID.getText());
- r.setFamilyName(textFamilyName.getText());
- r.setAddress(textAddress.getText());
- r.setContactNumber(textContactNumber.getText());
- r.setIncome(textIncome.getText());
- r.setMembers(textMembers.getText());
- if (c.create(r)){
- JOptionPane.showMessageDialog(null, "ADD NEW FAMILY SUCCESSFUL");
- fillData();
- }else
- JOptionPane.showMessageDialog(null, "ADD NEW FAMILY FAILED");
- }
- });
- btnAdd.setBounds(559, 271, 75, 21);
- btnAdd.setText("Add");
- Label lblNewLabel = new Label(shell, SWT.NONE);
- lblNewLabel.setBounds(306, 313, 55, 15);
- lblNewLabel.setText("Income");
- Label lblNewLabel_1 = new Label(shell, SWT.NONE);
- lblNewLabel_1.setBounds(10, 355, 55, 15);
- lblNewLabel_1.setText("Members");
- textIncome = new Text(shell, SWT.BORDER);
- textIncome.setBounds(388, 310, 165, 21);
- textMembers = new Text(shell, SWT.BORDER);
- textMembers.setBounds(94, 352, 165, 21);
- Label lblContact = new Label(shell, SWT.NONE);
- lblContact.setBounds(306, 355, 55, 15);
- lblContact.setText("Contact #");
- textContactNumber = new Text(shell, SWT.BORDER);
- textContactNumber.setBounds(388, 352, 165, 21);
- Button btnUpdate = new Button(shell, SWT.NONE);
- btnUpdate.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent f) {
- Family f1 = new Family();
- f1.setFamilyId(textID.getText());
- f1.setFamilyName(textFamilyName.getText());
- f1.setAddress(textAddress.getText());
- f1.setContactNumber(textContactNumber.getText());
- f1.setIncome(textIncome.getText());
- f1.setMembers(textMembers.getText());
- if(c.update(f1)){
- JOptionPane.showMessageDialog(null, "UPDATE FAMILY SUCCESSFUL");
- updateData();
- }else
- JOptionPane.showMessageDialog(null, "UPDATE FAMILY FAILED");
- }});
- btnUpdate.setBounds(559, 308, 75, 25);
- btnUpdate.setText("Update");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement