Advertisement
Guest User

Untitled

a guest
May 29th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. package Model;
  2.  
  3. public class Employee {
  4.  
  5. int id,income,members;
  6. String familyName,address;
  7.  
  8.  
  9.  
  10. public int getId() {
  11. return id;
  12. }
  13.  
  14.  
  15. public void setId(int id) {
  16. this.id = id;
  17. }
  18.  
  19.  
  20. public int getIncome() {
  21. return income;
  22. }
  23.  
  24.  
  25. public void setIncome(int income) {
  26. this.income = income;
  27. }
  28.  
  29.  
  30. public int getMembers() {
  31. return members;
  32. }
  33.  
  34.  
  35. public void setMembers(int members) {
  36. this.members = members;
  37. }
  38.  
  39.  
  40. public String getFamilyName() {
  41. return familyName;
  42. }
  43.  
  44.  
  45. public void setFamilyName(String familyName) {
  46. this.familyName = familyName;
  47. }
  48.  
  49.  
  50. public String getAddress() {
  51. return address;
  52. }
  53.  
  54.  
  55. public void setAddress(String address) {
  56. this.address = address;
  57. }
  58.  
  59.  
  60. public Employee() {
  61. super();
  62.  
  63. }
  64.  
  65. }
  66.  
  67. package Controller;
  68.  
  69. import java.util.*;
  70. import java.sql.*;
  71. import Model.*;
  72.  
  73.  
  74.  
  75.  
  76.  
  77. public class Controller {
  78.  
  79. public List <Employee> findAll(){
  80. try{
  81. List <Employee> listEmployee = new ArrayList<>();
  82.  
  83. PreparedStatement pst = Driver.getConnection().prepareStatement ("Select * from addFamily");
  84.  
  85. ResultSet rs = pst.executeQuery();
  86. while (rs.next())
  87. {
  88. Employee e= new Employee();
  89. e.setId(rs.getInt("id"));
  90. e.setFamilyName(rs.getString("familyName"));
  91. e.setAddress(rs.getString("address"));
  92. e.setIncome(rs.getInt("income"));
  93. e.setMembers(rs.getInt("members"));
  94.  
  95. listEmployee.add(e);
  96.  
  97.  
  98. }
  99.  
  100. return listEmployee;
  101.  
  102. }
  103.  
  104. catch(Exception b){
  105. return null;
  106. }
  107. }
  108.  
  109.  
  110. public boolean create(Employee e){
  111. try{
  112. PreparedStatement pst = Driver.getConnection().prepareStatement("insert into addFamily(id,familyName,address,income,members)values(?,?,?,?,?)");
  113. pst.setInt(1,e.getId());
  114. pst.setString(2,e.getFamilyName());
  115. pst.setString(3,e.getAddress());
  116. pst.setInt(4,e.getIncome());
  117. pst.setInt(5,e.getMembers());
  118.  
  119. System.out.println("RECORDED");
  120. return pst.executeUpdate()>0;
  121. }
  122. catch(Exception c){
  123. System.out.println("NOT RECORDED");
  124. return false;
  125. }
  126. }
  127. }
  128.  
  129.  
  130. package Controller;
  131. import java.sql.*;
  132.  
  133.  
  134. public class Driver {
  135.  
  136. public static Connection getConnection(){
  137.  
  138. Connection conn = null;
  139.  
  140.  
  141.  
  142. try{
  143. conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/user2","root","");
  144.  
  145. System.out.println("CONNECTED");
  146. }
  147.  
  148. catch(Exception a){
  149. System.out.println("NOT CONNECTED");
  150. }
  151. return conn;
  152. }
  153. }
  154.  
  155.  
  156. package View;
  157.  
  158. import org.eclipse.swt.SWT;
  159. import org.eclipse.swt.widgets.Display;
  160. import org.eclipse.swt.widgets.Shell;
  161. import org.eclipse.swt.widgets.Table;
  162. import org.eclipse.swt.widgets.TableColumn;
  163.  
  164. import java.util.*;
  165. import javax.swing.JOptionPane;
  166. import Controller.*;
  167. import Model.*;
  168. import org.eclipse.swt.widgets.TableItem;
  169. import org.eclipse.swt.widgets.Label;
  170. import org.eclipse.swt.widgets.Text;
  171. import org.eclipse.swt.widgets.Button;
  172. import org.eclipse.swt.events.SelectionAdapter;
  173. import org.eclipse.swt.events.SelectionEvent;
  174. public class View {
  175.  
  176. Controller c = new Controller();
  177. protected Shell shell;
  178. private Table table;
  179. private Text textID;
  180. private Text textFamilyName;
  181. private Text textAddress;
  182. private Text textIncome;
  183. private Text textMembers;
  184.  
  185. /**
  186. * Launch the application.
  187. * @param args
  188. */
  189. public static void main(String[] args) {
  190. try {
  191. View window = new View();
  192. window.open();
  193. } catch (Exception e) {
  194. }
  195. }
  196.  
  197. private void fillData(){
  198. Controller c = new Controller();
  199. for (Employee e: c.findAll()){
  200. TableItem tableItem = new TableItem (table,SWT.NONE);
  201. tableItem.setText("TABLE");
  202. tableItem.setText(new String []
  203. {String.valueOf (e.getId()) ,(e.getFamilyName()),(e.getAddress()),String.valueOf(e.getIncome()),String.valueOf(e.getMembers())});
  204. }
  205.  
  206. }
  207. /**
  208. * Open the window.
  209. */
  210. public void open() {
  211. Display display = Display.getDefault();
  212. createContents();
  213. shell.open();
  214. shell.layout();
  215. while (!shell.isDisposed()) {
  216. if (!display.readAndDispatch()) {
  217. display.sleep();
  218. }
  219. }
  220. }
  221.  
  222. /**
  223. * Create contents of the window.
  224. */
  225. protected void createContents() {
  226. shell = new Shell();
  227. shell.setSize(704, 442);
  228. shell.setText("SWT Application");
  229.  
  230. table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
  231. table.setBounds(10, 10, 516, 242);
  232. table.setHeaderVisible(true);
  233. table.setLinesVisible(true);
  234.  
  235. TableColumn tblclmnNewColumn = new TableColumn(table, SWT.CENTER);
  236. tblclmnNewColumn.setWidth(48);
  237. tblclmnNewColumn.setText("ID");
  238.  
  239. TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.CENTER);
  240. tblclmnNewColumn_1.setWidth(142);
  241. tblclmnNewColumn_1.setText("FAMILY NAME");
  242.  
  243. TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.CENTER);
  244. tblclmnNewColumn_2.setWidth(126);
  245. tblclmnNewColumn_2.setText("ADDRESS");
  246.  
  247. TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.CENTER);
  248. tblclmnNewColumn_3.setWidth(100);
  249. tblclmnNewColumn_3.setText("INCOME");
  250.  
  251. TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.NONE);
  252. tblclmnNewColumn_4.setWidth(100);
  253. tblclmnNewColumn_4.setText("MEMBERS");
  254.  
  255. Label lblId = new Label(shell, SWT.NONE);
  256. lblId.setBounds(10, 271, 55, 15);
  257. lblId.setText("Family ID");
  258.  
  259. textID = new Text(shell, SWT.BORDER);
  260. textID.setBounds(94, 268, 165, 21);
  261.  
  262. Label lblLastName = new Label(shell, SWT.NONE);
  263. lblLastName.setBounds(10, 292, 76, 15);
  264. lblLastName.setText("Family Name");
  265.  
  266. Label lblFirstName = new Label(shell, SWT.NONE);
  267. lblFirstName.setBounds(10, 313, 66, 15);
  268. lblFirstName.setText("Address");
  269.  
  270. textFamilyName = new Text(shell, SWT.BORDER);
  271. textFamilyName.setBounds(94, 289, 165, 21);
  272.  
  273. textAddress = new Text(shell, SWT.BORDER);
  274. textAddress.setBounds(94, 310, 165, 21);
  275.  
  276. Button btnNewButton = new Button(shell, SWT.NONE);
  277. btnNewButton.addSelectionListener(new SelectionAdapter() {
  278. @Override
  279. public void widgetSelected(SelectionEvent e) {
  280. Employee r = new Employee();
  281. r.setFamilyName(textFamilyName.getText());
  282. r.setAddress(textAddress.getText());
  283. r.setIncome(Integer.parseInt(textIncome.getText()));
  284. r.setMembers(Integer.parseInt(textMembers.getText()));
  285.  
  286. if (c.create(r)){
  287. JOptionPane.showMessageDialog(null, "ADD NEW FAMILY SUCCESSFUL");
  288. fillData();
  289. }else
  290. JOptionPane.showMessageDialog(null, "ADD NEW FAMILY FAILED");
  291. }
  292. });
  293. btnNewButton.setBounds(323, 308, 75, 25);
  294. btnNewButton.setText("ADD");
  295.  
  296. Label lblNewLabel = new Label(shell, SWT.NONE);
  297. lblNewLabel.setBounds(10, 337, 55, 15);
  298. lblNewLabel.setText("Income");
  299.  
  300. Label lblNewLabel_1 = new Label(shell, SWT.NONE);
  301. lblNewLabel_1.setBounds(10, 362, 55, 15);
  302. lblNewLabel_1.setText("Members");
  303.  
  304. textIncome = new Text(shell, SWT.BORDER);
  305. textIncome.setBounds(94, 331, 165, 21);
  306.  
  307. textMembers = new Text(shell, SWT.BORDER);
  308. textMembers.setBounds(94, 356, 165, 21);
  309.  
  310. }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement