Guest User

Untitled

a guest
Apr 2nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.08 KB | None | 0 0
  1. package Application;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. import java.awt.BorderLayout;
  7. import java.awt.Font;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16.  
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JPanel;
  21. import javax.swing.JScrollPane;
  22. import javax.swing.JTable;
  23. import javax.swing.JTextField;
  24. import javax.swing.table.DefaultTableModel;
  25.  
  26. public class Work extends JFrame implements ActionListener {
  27.  
  28. private JLabel JTitle;
  29. private JButton JAdd, JUpdate, JDelete, JPremium, JAccept, jDelete2, jPremium,jUpdate;
  30. private JTextField textId, textFname, textLname, textE_mail, textAddress, textSalary;
  31.  
  32. public Work() {
  33.  
  34. super("Application");
  35. // setLocationRelativeTo(null);
  36. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37. this.setLayout(null);
  38. this.setExtendedState(JFrame.MAXIMIZED_BOTH);
  39. setSize(800, 600);
  40. setVisible(true);
  41.  
  42. // Title Label
  43. JTitle = new JLabel("Business application");
  44. JTitle.setBounds(600, 20, 200, 20);
  45. JTitle.setFont(new Font("SansSerif", Font.BOLD, 18));
  46. add(JTitle);
  47.  
  48. // Buttons
  49. JAdd = new JButton("Add new");
  50. JAdd.setBounds(900, 100, 100, 20);
  51. JAdd.addActionListener(this);
  52. add(JAdd);
  53.  
  54. JUpdate = new JButton("Update");
  55. JUpdate.setBounds(1000, 100, 100, 20);
  56. JUpdate.addActionListener(this);
  57. add(JUpdate);
  58.  
  59. JDelete = new JButton("Delete");
  60. JDelete.setBounds(1100, 100, 100, 20);
  61. JDelete.addActionListener(this);
  62. add(JDelete);
  63.  
  64. JPremium = new JButton("Earnings");
  65. JPremium.setBounds(1200, 100, 100, 20);
  66. JPremium.addActionListener(this);
  67. add(JPremium);
  68.  
  69. JAccept = new JButton("Accept");
  70. JAccept.setBounds(1050, 200, 100, 20);
  71. JAccept.addActionListener(this);
  72. JAccept.setVisible(false);
  73. add(JAccept);
  74.  
  75. jDelete2 = new JButton("Delete");
  76. jDelete2.setBounds(1050, 200, 100, 20);
  77. jDelete2.addActionListener(this);
  78. jDelete2.setVisible(false);
  79. add(jDelete2);
  80.  
  81. jPremium = new JButton("Change");
  82. jPremium.setBounds(1050, 200, 100, 20);
  83. jPremium.addActionListener(this);
  84. jPremium.setVisible(false);
  85. add(jPremium);
  86.  
  87. jUpdate = new JButton("Update");
  88. jUpdate.setBounds(1050, 200, 100, 20);
  89. jUpdate.addActionListener(this);
  90. jUpdate.setVisible(false);
  91. add(jUpdate);
  92.  
  93.  
  94.  
  95.  
  96. // textId,, textLname, textE_mail, textAddress, textSalary;
  97. // textfield
  98. textFname = new JTextField();
  99. textFname.setBounds(800, 150, 100, 20);
  100. add(textFname);
  101. textFname.setVisible(false);
  102.  
  103. textLname = new JTextField();
  104. textLname.setBounds(900, 150, 100, 20);
  105. add(textLname);
  106. textLname.setVisible(false);
  107.  
  108. textE_mail = new JTextField();
  109. textE_mail.setBounds(1000, 150, 100, 20);
  110. add(textE_mail);
  111. textE_mail.setVisible(false);
  112.  
  113. textAddress = new JTextField();
  114. textAddress.setBounds(1100, 150, 100, 20);
  115. add(textAddress);
  116. textAddress.setVisible(false);
  117.  
  118. textSalary = new JTextField();
  119. textSalary.setBounds(1200, 150, 100, 20);
  120. add(textSalary);
  121. textSalary.setVisible(false);
  122.  
  123. }
  124.  
  125. @Override
  126. public void actionPerformed(ActionEvent e) {
  127.  
  128. Object source = e.getSource();
  129.  
  130. if (source == JAdd) {
  131. textFname.setVisible(true);
  132. textFname.setText("Fname");
  133.  
  134. textLname.setVisible(true);
  135. textLname.setText("Lname");
  136.  
  137. textE_mail.setVisible(true);
  138. textE_mail.setText("e-mail");
  139.  
  140. textAddress.setVisible(true);
  141. textAddress.setText("Address");
  142.  
  143. textSalary.setVisible(true);
  144. textSalary.setText("Salary");
  145.  
  146. JAccept.setVisible(true);
  147.  
  148. jDelete2.setVisible(false);
  149. jPremium.setVisible(false);
  150. jUpdate.setVisible(false);
  151.  
  152.  
  153. }
  154. if (source == JAccept) {
  155. AddEmployees();
  156. validate();
  157. repaint();
  158. }
  159.  
  160. if(source == JDelete){
  161. textFname.setVisible(false);
  162. textLname.setVisible(false);
  163. textAddress.setVisible(false);
  164. textSalary.setVisible(false);
  165. JAccept.setVisible(false);
  166. jPremium.setVisible(false);
  167. jUpdate.setVisible(false);
  168.  
  169.  
  170. jDelete2.setVisible(true);
  171. textE_mail.setVisible(true);
  172. textE_mail.setText("id");
  173. }
  174. if (source == jDelete2) {
  175. DeleteEmployees();
  176. validate();
  177. repaint();
  178. }
  179.  
  180. if(source == JPremium){
  181. textFname.setVisible(false);
  182. textLname.setVisible(false);
  183. textAddress.setVisible(false);
  184. JAccept.setVisible(false);
  185. jDelete2.setVisible(false);
  186. jUpdate.setVisible(false);
  187.  
  188. textE_mail.setVisible(true);
  189. textE_mail.setText("id");
  190. textSalary.setVisible(true);
  191. textSalary.setText("Change");
  192. jPremium.setVisible(true);
  193. }
  194.  
  195. if(source == jPremium){
  196. PremiumEmployees();
  197. }
  198. if(source == JUpdate){
  199. textFname.setVisible(true);
  200. textFname.setText("Id");
  201.  
  202. textLname.setVisible(true);
  203. textLname.setText("Fname");
  204.  
  205. textE_mail.setVisible(true);
  206. textE_mail.setText("Lname");
  207.  
  208. textAddress.setVisible(true);
  209. textAddress.setText("e_mail");
  210.  
  211. textSalary.setVisible(true);
  212. textSalary.setText("Address");
  213.  
  214. jUpdate.setVisible(true);
  215.  
  216. jPremium.setVisible(false);
  217. JAccept.setVisible(false);
  218. jDelete2.setVisible(false);
  219. }
  220. if(source == jUpdate){
  221. Update();
  222. }
  223.  
  224.  
  225. }
  226.  
  227. // connection
  228. static Connection getConnection() {
  229. Connection con = null;
  230. try {
  231. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees", "pma", "");
  232. } catch (SQLException e) {
  233. e.printStackTrace();
  234. }
  235. return con;
  236. }
  237.  
  238. static ArrayList<Users> getUsers() {
  239. ArrayList<Users> users = new ArrayList<Users>();
  240. Connection con = getConnection();
  241. Statement st;
  242. ResultSet rs;
  243. Users u;
  244. try {
  245. st = con.createStatement();
  246. rs = st.executeQuery("Select * from employees");
  247. while (rs.next()) {
  248. u = new Users(rs.getInt("id"), rs.getString("fname"), rs.getString("lname"), rs.getString("e_mail"),
  249. rs.getString("address"), rs.getInt("salary"));
  250. users.add(u);
  251. }
  252. } catch (SQLException ex) {
  253. Logger.getLogger(Work.class.getName()).log(Level.SEVERE, null, ex);
  254. }
  255. return users;
  256. }
  257.  
  258. private void AddEmployees() {
  259.  
  260.  
  261. String pName = textFname.getText();
  262. String pLname2 = textLname.getText();
  263. String pE_mail = textE_mail.getText();
  264. String pAddres = textAddress.getText();
  265. int pSalary = Integer.parseInt(textSalary.getText());
  266.  
  267.  
  268. try {
  269. // connection to database
  270. Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees", "pma", "");
  271. // create a statement
  272. Statement myStmt = myConn.createStatement();
  273. // execute sql query
  274. String sql2 = "insert into employees (fname,lname,e_mail,address,salary) values ('" + pName + "','"
  275. + pLname2 + "','" + pE_mail + "','" + pAddres + "'," + pSalary + ")";
  276. myStmt.executeUpdate(sql2);
  277.  
  278. } catch (Exception exc) {
  279. exc.printStackTrace();
  280. }
  281.  
  282. }
  283.  
  284. private void DeleteEmployees(){
  285.  
  286. int D = Integer.parseInt(textE_mail.getText());
  287. try {
  288. // connection to database
  289. Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees", "pma", "");
  290. // create a statement
  291. Statement myStmt = myConn.createStatement();
  292. // execute sql query
  293.  
  294. String sql ="DELETE from employees where id="+D;
  295. myStmt.executeUpdate(sql);
  296.  
  297. } catch (Exception exc) {
  298. exc.printStackTrace();
  299. }
  300.  
  301. }
  302.  
  303. private void PremiumEmployees(){
  304.  
  305. int id = Integer.parseInt(textE_mail.getText());
  306. int rise = Integer.parseInt(textSalary.getText());
  307. try {
  308. // connection to database
  309. Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees", "pma", "");
  310. // create a statement
  311. Statement myStmt = myConn.createStatement();
  312. // execute sql query
  313.  
  314. String sql ="update employees set salary="+rise+" where id ="+id ;
  315. myStmt.executeUpdate(sql);
  316. System.out.println("aktualizacja");
  317.  
  318. } catch (Exception exc) {
  319. exc.printStackTrace();
  320. }
  321.  
  322. }
  323.  
  324. private void Update(){
  325.  
  326. int pId = Integer.parseInt(textFname.getText());
  327. String pName = textLname.getText();
  328. String pLname2 = textE_mail.getText();
  329. String pE_mail = textAddress.getText();
  330. String pAddress = textSalary.getText();
  331.  
  332. try {
  333. // connection to database
  334. Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees", "pma", "");
  335. // create a statement
  336. Statement myStmt = myConn.createStatement();
  337. // execute sql query
  338.  
  339. String sql ="update employees set fname='"+pName+"', lname='"+pLname2+"', e_mail='"+pE_mail+"', address='"+pAddress+"'"+"where id="+pId;
  340.  
  341. myStmt.executeUpdate(sql);
  342. System.out.println("aktualizacja");
  343.  
  344. } catch (Exception exc) {
  345. exc.printStackTrace();
  346. }
  347. }
  348. public static void main(String[] args) {
  349.  
  350. JTable table = new JTable();
  351. DefaultTableModel model = new DefaultTableModel();
  352.  
  353. Object[] columnsName = new Object[6];
  354. columnsName[0] = "Id";
  355. columnsName[1] = "Fname";
  356. columnsName[2] = "Lname";
  357. columnsName[3] = "E_mail";
  358. columnsName[4] = "Address";
  359. columnsName[5] = "Salary";
  360.  
  361. model.setColumnIdentifiers(columnsName);
  362.  
  363. Object[] rowData = new Object[6];
  364. for (int i = 0; i < getUsers().size(); i++) {
  365. rowData[0] = getUsers().get(i).getId();
  366. rowData[1] = getUsers().get(i).getFname();
  367. rowData[2] = getUsers().get(i).getLname();
  368. rowData[3] = getUsers().get(i).getE_mail();
  369. rowData[4] = getUsers().get(i).getAddress();
  370. rowData[5] = getUsers().get(i).getSalary();
  371. model.addRow(rowData);
  372. }
  373.  
  374. table.setModel(model);
  375. System.out.println(getUsers().size());
  376.  
  377. JPanel panel = new JPanel();
  378. // panel.setLayout(new BorderLayout());// responsywne
  379. JScrollPane pane = new JScrollPane(table);
  380. // panel.add(pane, BorderLayout.CENTER);
  381. panel.add(pane);
  382. panel.setBounds(100, 100, 500, 500);
  383. // window.setContentPane(panel);
  384.  
  385. Work window = new Work();
  386. window.add(panel);
  387. window.setVisible(true);
  388.  
  389. }
  390. }
Add Comment
Please, Sign In to add comment