Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Model;
- public class Employee {
- int id,income,members;
- String familyName,address;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getIncome() {
- return income;
- }
- public void setIncome(int income) {
- this.income = income;
- }
- public int getMembers() {
- return members;
- }
- public void setMembers(int members) {
- this.members = members;
- }
- 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 Employee() {
- super();
- }
- }
- package Controller;
- import java.util.*;
- import java.sql.*;
- import Model.*;
- public class Controller {
- public List <Employee> findAll(){
- try{
- List <Employee> listEmployee = new ArrayList<>();
- PreparedStatement pst = Driver.getConnection().prepareStatement ("Select * from addFamily");
- ResultSet rs = pst.executeQuery();
- while (rs.next())
- {
- Employee e= new Employee();
- e.setId(rs.getInt("id"));
- e.setFamilyName(rs.getString("familyName"));
- e.setAddress(rs.getString("address"));
- e.setIncome(rs.getInt("income"));
- e.setMembers(rs.getInt("members"));
- listEmployee.add(e);
- }
- return listEmployee;
- }
- catch(Exception b){
- return null;
- }
- }
- public boolean create(Employee e){
- try{
- PreparedStatement pst = Driver.getConnection().prepareStatement("insert into addFamily(id,familyName,address,income,members)values(?,?,?,?,?)");
- pst.setInt(1,e.getId());
- pst.setString(2,e.getFamilyName());
- pst.setString(3,e.getAddress());
- pst.setInt(4,e.getIncome());
- pst.setInt(5,e.getMembers());
- System.out.println("RECORDED");
- return pst.executeUpdate()>0;
- }
- catch(Exception c){
- System.out.println("NOT RECORDED");
- return false;
- }
- }
- }
- package Controller;
- import java.sql.*;
- public class Driver {
- public static Connection getConnection(){
- Connection conn = null;
- try{
- conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/user2","root","");
- System.out.println("CONNECTED");
- }
- catch(Exception a){
- System.out.println("NOT CONNECTED");
- }
- return conn;
- }
- }
- 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.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;
- /**
- * 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 (Employee e: c.findAll()){
- TableItem tableItem = new TableItem (table,SWT.NONE);
- tableItem.setText("TABLE");
- tableItem.setText(new String []
- {String.valueOf (e.getId()) ,(e.getFamilyName()),(e.getAddress()),String.valueOf(e.getIncome()),String.valueOf(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, 10, 516, 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_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.NONE);
- tblclmnNewColumn_4.setWidth(100);
- tblclmnNewColumn_4.setText("MEMBERS");
- 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(10, 292, 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(94, 289, 165, 21);
- textAddress = new Text(shell, SWT.BORDER);
- textAddress.setBounds(94, 310, 165, 21);
- Button btnNewButton = new Button(shell, SWT.NONE);
- btnNewButton.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- Employee r = new Employee();
- r.setFamilyName(textFamilyName.getText());
- r.setAddress(textAddress.getText());
- r.setIncome(Integer.parseInt(textIncome.getText()));
- r.setMembers(Integer.parseInt(textMembers.getText()));
- if (c.create(r)){
- JOptionPane.showMessageDialog(null, "ADD NEW FAMILY SUCCESSFUL");
- fillData();
- }else
- JOptionPane.showMessageDialog(null, "ADD NEW FAMILY FAILED");
- }
- });
- btnNewButton.setBounds(323, 308, 75, 25);
- btnNewButton.setText("ADD");
- Label lblNewLabel = new Label(shell, SWT.NONE);
- lblNewLabel.setBounds(10, 337, 55, 15);
- lblNewLabel.setText("Income");
- Label lblNewLabel_1 = new Label(shell, SWT.NONE);
- lblNewLabel_1.setBounds(10, 362, 55, 15);
- lblNewLabel_1.setText("Members");
- textIncome = new Text(shell, SWT.BORDER);
- textIncome.setBounds(94, 331, 165, 21);
- textMembers = new Text(shell, SWT.BORDER);
- textMembers.setBounds(94, 356, 165, 21);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement