Advertisement
Guest User

Untitled

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