Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.93 KB | None | 0 0
  1. package org;
  2.  
  3. import java.awt.Container;
  4. import java.awt.event.*;
  5. import java.sql.*;
  6. import java.text.DateFormat;
  7. import java.text.SimpleDateFormat;
  8. import javax.swing.*;
  9. import javax.swing.table.DefaultTableModel;
  10.  
  11. public class Graphical extends JFrame {
  12. static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
  13. static String connect = "jdbc:derby:DB;create=true";
  14. private static final long serialVersionUID = 1L;
  15. DefaultTableModel model1 = new DefaultTableModel();
  16. DefaultTableModel model2 = new DefaultTableModel();
  17. DefaultTableModel model3 = new DefaultTableModel();
  18. private JTable table1=new JTable(model1) ;
  19. private JTable table2=new JTable(model2) ;
  20. private JTable table3=new JTable(model3) ;
  21. private JScrollPane scrollPane1 = new JScrollPane(table1);
  22. private JScrollPane scrollPane2 = new JScrollPane(table2);
  23. private JScrollPane scrollPane3 = new JScrollPane(table3);
  24. private JButton addButton = new JButton("Добавить");
  25. private JButton deleteButton = new JButton("Удалить");
  26. private JButton createButton = new JButton("Создать");
  27. private JButton lookButton = new JButton("Отобразить");
  28.  
  29. public Graphical (){
  30. super("Отдел кадров");
  31. init();
  32. }
  33. public void look(){
  34. DefaultTableModel m1 = (DefaultTableModel) table1.getModel();
  35. DefaultTableModel m2 = (DefaultTableModel) table2.getModel();
  36. DefaultTableModel m3 = (DefaultTableModel) table3.getModel();
  37. m1.setRowCount(0);
  38. m1.addRow(new Object[]{"Код работника:", "ФИО:", "Должность:", "Стаж:", "Дата заключ договора:"});
  39. m3.setRowCount(0);
  40. m3.addRow(new Object[]{"Код отдела:", "Должность:", "Оклад:","Премия:"});
  41. m2.setRowCount(0);
  42. m2.addRow(new Object[]{"ФИО:", "Дата рождения", "Пол", "Образование", "Телефон", "Номер паспорта"});
  43. System.setProperty("derby.system.home", "C:\\MyDB");
  44. try {
  45. Class.forName( driver );
  46. Connection conn = DriverManager.getConnection(connect);
  47. Statement st = conn.createStatement();
  48. ResultSet rec = st.executeQuery("SELECT * FROM EmployeeСard ORDER BY fio");
  49. while (rec.next()) {
  50. int kod = rec.getInt("kod");
  51. String name = rec.getString("fio");
  52. String position = rec.getString("position");
  53. int experience = rec.getInt("experience");
  54. Date date = rec.getDate("date");
  55. DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
  56. String d=df.format(date);
  57. DefaultTableModel model = (DefaultTableModel) table1.getModel();
  58. model.addRow(new Object[]{Integer.toString(kod), name, position,experience,d});
  59.  
  60. }
  61. rec.close();
  62. st.close();
  63. } catch (Exception e1) {
  64. System.err.println("Run-time error1: " + e1 );
  65. }
  66. try {
  67. Class.forName( driver );
  68. Connection conn = DriverManager.getConnection(connect);
  69. Statement st = conn.createStatement();
  70. ResultSet rec = st.executeQuery("SELECT * FROM PersonInformation ORDER BY fio");
  71. while (rec.next()) {
  72. String fio = rec.getString("fio");
  73. int sex = rec.getInt("sex");
  74. String education = rec.getString("education");
  75. int pasportNumber = rec.getInt("pasportNumber");
  76. int number=rec.getInt("number");
  77. Date date = rec.getDate("date");
  78. DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
  79. String d=df.format(date);
  80. DefaultTableModel model2 = (DefaultTableModel) table2.getModel();
  81. model2.addRow(new Object[]{fio, d, Integer.toString(sex),education,number,pasportNumber});
  82.  
  83. }
  84. rec.close();
  85. st.close();
  86. } catch (Exception e1) {
  87. System.err.println("Run-time error2: " + e1 );
  88. }
  89. try {
  90. Class.forName( driver );
  91. Connection conn = DriverManager.getConnection(connect);
  92. Statement st = conn.createStatement();
  93. ResultSet rec = st.executeQuery("SELECT * FROM StafInfo ORDER BY kod");
  94. while (rec.next()) {
  95. int kod = rec.getInt("kod");
  96. String position = rec.getString("position");
  97. int premium = rec.getInt("premium");
  98. int oklad=rec.getInt("oklad");
  99. DefaultTableModel model3 = (DefaultTableModel) table3.getModel();
  100. model3.addRow(new Object[]{Integer.toString(kod), position,oklad,premium});
  101.  
  102. }
  103. rec.close();
  104. st.close();
  105. } catch (Exception e1) {
  106. System.err.println("Run-time error3: " + e1 );
  107. }
  108. }
  109. public void init() {
  110. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  111. Container c = getContentPane();
  112. setBounds (20,80,980,900);
  113. c.setLayout(null);
  114. DefaultTableModel model1 = (DefaultTableModel) table1.getModel();
  115. model1.addColumn("Код");
  116. model1.addColumn("ФИО");
  117. model1.addColumn("Должность");
  118. model1.addColumn("Стаж");
  119. model1.addColumn("Дата принятия");
  120. table1.setBounds(20, 20, 900, 200);
  121. c.add(table1);
  122.  
  123. DefaultTableModel model2 = (DefaultTableModel) table2.getModel();
  124. model2.addColumn("Код");
  125. model2.addColumn("Дата");
  126. model2.addColumn("Пол");
  127. model2.addColumn("Образование");
  128. model2.addColumn("Телефон");
  129. model2.addColumn("Номер паспорта");
  130. table2.setBounds(20, 260, 900, 200);
  131. c.add(table2);
  132.  
  133.  
  134. DefaultTableModel model3 = (DefaultTableModel) table3.getModel();
  135. model3.addColumn("Код отдела");
  136. model3.addColumn("Должность");
  137. model3.addColumn("Оклад");
  138. model3.addColumn("Премия");
  139. table3.setBounds(20, 500, 900, 200);
  140. c.add(table3);
  141.  
  142. addButton.setBounds(40,750,100,40);
  143. c.add(addButton);
  144. addButton.addActionListener(new addListener());
  145.  
  146. createButton.setBounds(350,750,100,40);
  147. c.add(createButton);
  148. createButton.addActionListener(new createListener());
  149.  
  150. lookButton.setBounds(600,750,200,40);
  151. c.add(lookButton);
  152. lookButton.addActionListener(new lookListener());
  153.  
  154. deleteButton.setBounds(190,750,100,40);
  155. c.add(deleteButton);
  156. deleteButton.addActionListener(new deleteListener());
  157. }
  158. private class addListener implements ActionListener{
  159. public void actionPerformed(ActionEvent e){
  160. new DialogAddItem(Graphical.this, "Добавить нового клиента");
  161. look();
  162. }
  163. }
  164. private class createListener implements ActionListener{
  165. public void actionPerformed(ActionEvent e){
  166.  
  167.  
  168. String driver = "org.apache.derby.jdbc.EmbeddedDriver";
  169. String connect = "jdbc:derby:DB;create=true";
  170. System.setProperty("derby.system.home", "C:\\MyDB" );
  171. try {
  172. Class.forName( driver );
  173. Connection conn = DriverManager.getConnection(connect);
  174. Statement st = conn.createStatement();
  175. st.executeUpdate("CREATE TABLE PersonInformation " +
  176. "(fio VARCHAR(32) PRIMARY KEY, date DATE, sex INT, education VARCHAR(32), number INT, pasportNumber INT )");
  177. st.executeUpdate("CREATE TABLE StafInfo " +
  178. "(kod INT, position VARCHAR(32) PRIMARY KEY, oklad INT, premium INT)");
  179.  
  180. st.executeUpdate("CREATE TABLE EmployeeСard " +
  181. "(kod INT PRIMARY KEY, fio VARCHAR (32) REFERENCES PersonInformation (fio), position VARCHAR(32) REFERENCES StafInfo (position), " +
  182. "experience INT, date DATE)");
  183. st.close();
  184.  
  185. } catch (Exception e1) {
  186. System.err.println("Run-time error00: " + e1 );
  187. }
  188. String Cards = "INSERT INTO EmployeeСard(kod, fio, position, experience, date) VALUES(?,?,?,?,?)";
  189. String RersonsInfo = "INSERT INTO PersonInformation(fio, date, sex, education, number, pasportNumber) VALUES(?,?,?,?,?,?)";
  190. String stf = "INSERT INTO StafInfo(kod, position, oklad, premium) VALUES(?,?,?,?)";
  191.  
  192. StafInfo [] arrSt = {
  193. new StafInfo(1, "Логист", 1000, 125 ),
  194. new StafInfo(2, "Экономист" ,1500, 200 ),
  195. new StafInfo(2,"Финансист", 1200, 300 ),
  196. new StafInfo(3, "Программист", 3000, 500 ),
  197. new StafInfo(3, "Системный администратор", 2000, 1000 ),
  198. new StafInfo(3,"Тестировщик", 1000, 100 ),
  199. new StafInfo(3, "Директор", 500, 100 )
  200. };
  201.  
  202. PersonInformation [] arrInfo = {
  203. new PersonInformation( "Макаревич Евгения Александровна", new Date( 80, 1, 1 ), 0, "БГУ", 124574, 124585),
  204. new PersonInformation( "Коренчук Анна Олеговна", new Date( 77, 4, 6 ), 0, "БНТУ", 785444, 325612),
  205. new PersonInformation( "Бузун Елизавета Анатольевна", new Date( 89, 8, 21 ), 0, "БГУИР", 451256, 458965),
  206. new PersonInformation("Tochilo Anna", new Date( 93, 10, 13 ), 0, "БрГУ", 785695, 963125),
  207. new PersonInformation( "Тулейко Максим Юрьевич", new Date( 90, 5, 9 ), 1, "Академия", 632985, 125633),
  208. };
  209.  
  210. EmployeeСard [] arrCards = {
  211. new EmployeeСard( 1,"Макаревич Евгения Александровна", "Экономист", 1,new Date( 16, 10, 1 )),
  212. new EmployeeСard( 2,"Коренчук Анна Олеговна", "Логист", 5,new Date( 9, 8, 14 )),
  213. new EmployeeСard( 3,"Бузун Елизавета Анатольевна", "Программист", 2, new Date( 98, 11, 1 )),
  214. new EmployeeСard( 4,"Tochilo Anna", "Директор", 2, new Date( 14, 10, 1 )),
  215. new EmployeeСard( 5,"Тулейко Максим Юрьевич", "Финансист", 2, new Date( 15, 9, 13 )),
  216. };
  217. System.setProperty("derby.system.Вhome", "C:\\MyDB" );
  218.  
  219. try {
  220. Class.forName( driver );
  221. Connection conn = DriverManager.getConnection(connect);
  222. PreparedStatement st = conn.prepareStatement(stf);
  223.  
  224. for ( StafInfo si : arrSt ) {
  225.  
  226. try {
  227. st.setInt( 1, si.kod );
  228. st.setString( 2, si.position );
  229. st.setInt(3, si.oklad);
  230. st.setInt(4, si.premium);
  231. st.executeUpdate();
  232. }
  233. catch (SQLException e1) {
  234. System.err.println("Insert error: " + e1 );
  235. }
  236. }
  237.  
  238. st = conn.prepareStatement(RersonsInfo);
  239. for ( PersonInformation pi : arrInfo ) {
  240. try {
  241. st.setString( 1, pi.fio );
  242. st.setDate(2, pi.date);
  243. st.setInt( 3, pi.sex );
  244. st.setString( 4, pi.education );
  245. st.setInt( 5, pi.number );
  246. st.setInt( 6, pi.pasportNamber );
  247. st.executeUpdate();
  248. }
  249. catch (SQLException e1) {
  250. System.err.println("Insert error: " + e1 );
  251. }
  252. }
  253. st.close();
  254.  
  255. st = conn.prepareStatement(Cards);
  256. for ( EmployeeСard hc : arrCards ) {
  257. try {
  258. st.setInt( 1, hc.kod );
  259. st.setString( 2, hc.fio );
  260. st.setString( 3, hc.position );
  261. st.setInt( 4, hc.experience );
  262. st.setDate( 5, hc.date );
  263. st.executeUpdate();
  264. }
  265. catch (SQLException e1) {
  266. System.err.println("Insert error: " + e1);
  267. }
  268. }
  269. st.close();
  270. conn.close();
  271.  
  272. } catch (Exception e1) {
  273. System.err.println("Run-time error: " + e1 );
  274. }
  275. }
  276. }
  277. private class deleteListener implements ActionListener{
  278. public void actionPerformed(ActionEvent e){
  279. int t1 = table1.getSelectedRow();
  280. int t2 = table2.getSelectedRow();
  281. int t3 = table3.getSelectedRow();
  282.  
  283. String connect = "jdbc:derby:DB;create=true";
  284. System.setProperty("derby.system.home", "C:\\MyDB" );
  285. try{
  286. Connection conn = DriverManager.getConnection(connect);
  287.  
  288. if(t1 != -1) {
  289. String fio = table1.getValueAt(t1, 1).toString();
  290. String booksIns = "DELETE FROM EmployeeСard WHERE fio = '"+fio+"'" ;
  291. try {
  292. PreparedStatement st = conn.prepareStatement(booksIns);
  293. st.executeUpdate();
  294. st.close();
  295.  
  296. } catch (SQLException e1) {
  297. System.err.println("Insert error: " + e1 );
  298. }
  299. }
  300. if(t2 != -1) {
  301. String fio = table2.getValueAt(t2, 0).toString();
  302. String booksIns = "DELETE FROM EmployeeСard WHERE fio = '"+fio+"'" ;
  303. String booksI = "DELETE FROM PersonInformation WHERE fio = '"+fio+"'" ;
  304. try {
  305. PreparedStatement st = conn.prepareStatement(booksIns);
  306. st.executeUpdate();
  307. st = conn.prepareStatement(booksI);
  308. st.executeUpdate();
  309. st.close();
  310.  
  311. } catch (SQLException e1) {
  312. System.err.println("Insert error: " + e1 );
  313. }
  314. }
  315. if(t3 != -1) {
  316. String position = table3.getValueAt(t3, 1).toString();
  317. String booksIns = "DELETE FROM EmployeeСard WHERE position = '"+position+"'" ;
  318. String booksI = "DELETE FROM StafInfo WHERE position = '"+position+"'" ;
  319. try {
  320. PreparedStatement st = conn.prepareStatement(booksIns);
  321. st.executeUpdate();
  322. st = conn.prepareStatement(booksI);
  323. st.executeUpdate();
  324. st.close();
  325.  
  326. } catch (SQLException e1) {
  327. System.err.println("Insert error: " + e1 );
  328. }
  329. }
  330. }
  331. catch (Exception e3) {
  332. System.err.println("Insert error: " + e3 );
  333. }
  334. look();
  335. //new DialogDelete(Graphical.this, "Удаление данных в базе");
  336. }
  337. }
  338. private class lookListener implements ActionListener{
  339. public void actionPerformed(ActionEvent e){
  340. look();
  341. }
  342. }
  343.  
  344. public static void main(String[] args) {
  345. Graphical app = new Graphical();
  346. app.setVisible(true);
  347. app.setResizable(false);
  348. }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement