Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.83 KB | None | 0 0
  1. ArrayList columnNames = new ArrayList();
  2. ArrayList data = new ArrayList();
  3.  
  4. data_connector getdata1 = new data_connector();
  5. host = getdata1.getHost();
  6. username = getdata1.getUsername();
  7. password1 = getdata1.getPassword();
  8. mysql_command = getdata1.getMysql_command();
  9. command_name = getdata1.getCommand_name();
  10. setTitle(command_name);
  11.  
  12. // Connect to an MySQL Database, run query, get result set
  13. String url = "jdbc:mysql://"+host+":3306/xxxxx";
  14. String userid = username;
  15. String password = password1;
  16. String sql = mysql_command;
  17.  
  18.  
  19. // Java SE 7 has try-with-resources
  20. // This will ensure that the sql objects are closed when the program
  21. // is finished with them
  22. try (Connection connection = DriverManager.getConnection( url, userid, password );
  23. Statement stmt = connection.createStatement();
  24. ResultSet rs = stmt.executeQuery( sql ))
  25. {
  26. ResultSetMetaData md = rs.getMetaData();
  27. int columns = md.getColumnCount();
  28.  
  29. // Get column names
  30. for (int i = 1; i <= columns; i++)
  31. columnNames.add(md.getColumnName(i));
  32.  
  33. // Get row data
  34. while (rs.next())
  35. {
  36. ArrayList row = new ArrayList(columns);
  37. for (int i = 1; i <= columns; i++)
  38. row.add(rs.getObject(i));
  39.  
  40. data.add(row);
  41. }
  42. }
  43. catch (SQLException e)
  44. {
  45. System.out.println(e.getMessage());
  46. JOptionPane.showMessageDialog(null, e.getMessage());
  47. mysql_fail_flag = 1;
  48. }
  49.  
  50. // Create Vectors and copy over elements from ArrayLists to them
  51. // Vector is deprecated but I am using them in this example to keep
  52. // things simple - the best practice would be to create a custom defined
  53. // class which inherits from the AbstractTableModel class
  54. Vector columnNamesVector = new Vector();
  55. Vector dataVector = new Vector();
  56.  
  57. for (int i = 0; i < data.size(); i++)
  58. {
  59. ArrayList subArray = (ArrayList)data.get(i);
  60. Vector subVector = new Vector();
  61. for (int j = 0; j < subArray.size(); j++)
  62. subVector.add(subArray.get(j));
  63.  
  64. dataVector.add(subVector);
  65. }
  66.  
  67. for (int i = 0; i < columnNames.size(); i++ )
  68. columnNamesVector.add(columnNames.get(i));
  69. contentPane.setLayout(null);
  70.  
  71. // Create table with database data
  72. table = new JTable(dataVector, columnNamesVector)
  73. {
  74. public Class getColumnClass(int column)
  75. {
  76. for (int row = 0; row < getRowCount(); row++)
  77. {
  78. Object o = getValueAt(row, column);
  79.  
  80. if (o != null)
  81. return o.getClass();
  82. }
  83.  
  84. return Object.class;
  85. }
  86. };
  87.  
  88. // table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  89. JScrollPane scrollPane = new JScrollPane(table);
  90. scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  91.  
  92. scrollPane.setBounds(5, 5, xframeWidth-20, yframeHeight-70);
  93. getContentPane().add(scrollPane);
  94.  
  95. JPanel buttonPanel = new JPanel();
  96. buttonPanel.setBounds(5, 856, 1574, 1);
  97. getContentPane().add(buttonPanel);
  98. buttonPanel.setLayout(null);
  99.  
  100. class MyModel extends AbstractTableModel
  101. {
  102. private ResultSet result;
  103. private ResultSetMetaData metadata;
  104. public MyModel (ResultSet rs)
  105. {
  106. super();
  107. result = rs; // mustn't be null, maybe check and throw NPE
  108. metadata = result.getMetaData();
  109. }
  110.  
  111. public int getRowCount ()
  112. {
  113. result.last();
  114. return result.getRow(); // See http://stackoverflow.com/questions/8292256/get-number-of-rows-returned-by-resultset-in-java
  115. }
  116. public int getColumnCount ()
  117. {
  118. return metadata.getColumnCount();
  119. }
  120.  
  121. public Object getValueAt (int row, int col)
  122. {
  123. result.absolute(row);
  124. return result.getString(col);
  125. }
  126. public String getColumnName (int col)
  127. {
  128. return metadata.getColumnName(col);
  129. }
  130.  
  131. public void setValueAt (Object value, int row, int col)
  132. {
  133. result.absolute(row);
  134. result.updateObject(col, value);
  135. }
  136. }
  137.  
  138. private class RowColumnListSelectionListener implements ListSelectionListener {
  139. public void valueChanged(ListSelectionEvent e) {
  140.  
  141. rowIndexStart = table.getSelectedRow();
  142. rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();
  143. colIndexStart = table.getSelectedColumn();
  144. colIndexEnd = table.getColumnModel().getSelectionModel().getMaxSelectionIndex();
  145.  
  146. for ( i = rowIndexStart; i <= rowIndexEnd; i++) {
  147. for ( j = colIndexStart; j <= colIndexEnd; j++) {
  148.  
  149. Object cell_value = table.getValueAt(i,j);
  150. Cell_value_string_before = (String) cell_value;
  151. }
  152. }
  153.  
  154. }
  155.  
  156. }
  157.  
  158. public test_table() {
  159. setIconImage(Toolkit.getDefaultToolkit().getImage(test_table.class.getResource("/com/sun/java/swing/plaf/windows/icons/Computer.gif")));
  160.  
  161.  
  162. //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  163. //setBounds(100, 100, 688, 589);
  164. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  165. xframeWidth = screenSize.width; //dynamic size for frame x-axes
  166. yframeHeight = screenSize.height; //dynamic size for frame y-axes
  167. int xlocation = xframeWidth*2; //dynamic location x-axes
  168. int ylocation = yframeHeight*2; //dynamic location y-axes
  169. setBounds(0,0, xframeWidth, yframeHeight);
  170. setResizable(false);
  171.  
  172. JMenuBar menuBar = new JMenuBar();
  173. setJMenuBar(menuBar);
  174.  
  175. JMenu mnNewMenu = new JMenu("Αρχείο");
  176. menuBar.add(mnNewMenu);
  177.  
  178. JMenuItem mntmNewMenuItem_1 = new JMenuItem("Εξαγωγή σε .txt αρχείο");
  179. mntmNewMenuItem_1.addActionListener(new ActionListener() {
  180. public void actionPerformed(ActionEvent e) {
  181.  
  182. JFileChooser fileChooser =new JFileChooser();
  183. fileChooser.setDialogTitle("Δημιουργία αρχείου .txt");
  184. FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt", "text");
  185. fileChooser.setFileFilter(filter);
  186.  
  187. int returnVal = fileChooser.showSaveDialog(null);
  188. if (returnVal == JFileChooser.APPROVE_OPTION) {
  189.  
  190.  
  191. try {
  192. File file = fileChooser.getSelectedFile();
  193. File newfile = new File(file.getPath()+".txt");
  194. // PrintWriter os = new PrintWriter(file);
  195. FileWriter fw = new FileWriter(newfile,true); //filewriter
  196. BufferedWriter bw = new BufferedWriter(fw); //buffered writer
  197. PrintWriter os = new PrintWriter(bw, true);
  198.  
  199. os.print("");
  200. for (int col = 0; col < table.getColumnCount(); col++) {
  201. os.print(table.getColumnName(col) + "t");
  202. os.print(";");
  203. }
  204.  
  205. os.println("");
  206. os.println("");
  207.  
  208. for (int row = 0; row < table.getRowCount(); row++) {
  209. for (int col = 0; col < table.getColumnCount(); col++) {
  210. //os.print(table.getColumnName(col) + "t");
  211. // os.print(": ");
  212. os.print(table.getValueAt(row, col) + "t");
  213. os.print(";");
  214. // os.print(table.getRowCount() + "t");
  215. }
  216. os.println("");
  217. }
  218. os.close();
  219. System.out.println("Done!");
  220. }
  221. catch (IOException e1) {
  222. // TODO Auto-generated catch block
  223. e1.printStackTrace();
  224.  
  225.  
  226.  
  227.  
  228. }
  229. }
  230.  
  231.  
  232. }
  233. });
  234. mnNewMenu.add(mntmNewMenuItem_1);
  235.  
  236. JMenuItem mntmNewMenuItem_2 = new JMenuItem("Εκτύπωση");
  237. mntmNewMenuItem_2.addActionListener(new ActionListener() {
  238. public void actionPerformed(ActionEvent e) {
  239.  
  240. try {
  241. if (! table.print()) {
  242. System.err.println("User cancelled printing");
  243. }
  244. } catch (java.awt.print.PrinterException e1) {
  245. System.err.format("Cannot print %s%n", e1.getMessage());
  246. }
  247. }
  248. });
  249. mnNewMenu.add(mntmNewMenuItem_2);
  250. contentPane = new JPanel();
  251. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  252. setContentPane(contentPane);
  253.  
  254.  
  255. ArrayList columnNames = new ArrayList();
  256. ArrayList data = new ArrayList();
  257.  
  258. // data_connector getdata1 = new data_connector();
  259. // host = getdata1.getHost();
  260. // username = getdata1.getUsername();
  261. // password1 = getdata1.getPassword();
  262. // mysql_command = getdata1.getMysql_command();
  263. // command_name = getdata1.getCommand_name();
  264. //setTitle(command_name);
  265. // Connect to an MySQL Database, run query, get result set
  266. String url = "jdbc:mysql://localhost:3306/υπαλληλοι απε-μπε";
  267. String userid = "ziorange";
  268. String password = "120736";
  269. String sql = "SELECT * FROM `ΥΠΑΛΛΗΛΟΙ 2 test`";
  270. //String sql = "SELECT `ΚΩΔΙΚΟΣ`,`ΕΠΩΝΥΜΟ`,`ΟΝΟΜΑ`,`ΟΝΟΜΑ ΠΑΤΡΟΣ`,`ΑΜΚΑ`,`ΑΡΙΘΜΟΣ ΜΗΤΡΩΟΥ ΙΚΑ (αν υπάρχει)` FROM `ΥΠΑΛΛΗΛΟΙ 2` WHERE `ΚΩΔΙΚΟΣ`>'0' AND `ΗΜΕΡΟΜΗΝΙΑ ΑΠΟΧΩΡΗΣΗΣ`>'2009-12-31 00:00:00' OR `ΕΙΔΙΚΟΤΗΤΑ` !='ΑΝΤΑΠΟΚΡΙΤΗΣ ΕΞ' AND `ΛΟΓΟΣ ΑΠΟΧΩΡΗΣΗΣ`='-' ORDER BY `ΕΠΩΝΥΜΟ`";
  271.  
  272. // Java SE 7 has try-with-resources
  273. // This will ensure that the sql objects are closed when the program
  274. // is finished with them
  275. try
  276. {
  277. connection = DriverManager.getConnection( url, userid, password );
  278. Statement stmt = connection.createStatement();
  279. ResultSet rs = stmt.executeQuery( sql );
  280.  
  281. ResultSetMetaData md = rs.getMetaData();
  282. int columns = md.getColumnCount();
  283.  
  284. // Get column names
  285. for (int i = 1; i <= columns; i++)
  286. {
  287. columnNames.add( md.getColumnName(i) );
  288. }
  289.  
  290. // Get row data
  291. while (rs.next())
  292. {
  293. ArrayList row = new ArrayList(columns);
  294.  
  295. for (int i = 1; i <= columns; i++)
  296. {
  297. row.add( rs.getObject(i) );
  298. }
  299.  
  300. data.add( row );
  301. }
  302. }
  303. catch (SQLException e)
  304. {
  305. //
  306. System.out.println( e.getMessage() );
  307. JOptionPane.showMessageDialog(null, e.getMessage() );
  308. mysql_fail_flag=1;
  309. }
  310.  
  311. // Create Vectors and copy over elements from ArrayLists to them
  312. // Vector is deprecated but I am using them in this example to keep
  313. // things simple - the best practice would be to create a custom defined
  314. // class which inherits from the AbstractTableModel class
  315. Vector columnNamesVector = new Vector();
  316. Vector dataVector = new Vector();
  317.  
  318. for (int i = 0; i < data.size(); i++)
  319. {
  320. ArrayList subArray = (ArrayList)data.get(i);
  321. Vector subVector = new Vector();
  322. for (int j = 0; j < subArray.size(); j++)
  323. {
  324. subVector.add(subArray.get(j));
  325. }
  326. dataVector.add(subVector);
  327. }
  328.  
  329. for (int i = 0; i < columnNames.size(); i++ )
  330. columnNamesVector.add(columnNames.get(i));
  331. contentPane.setLayout(null);
  332.  
  333. // Create table with database data
  334. table = new JTable(dataVector, columnNamesVector)
  335.  
  336.  
  337.  
  338. {
  339. public Class getColumnClass(int column)
  340. {
  341. for (int row = 0; row < getRowCount(); row++)
  342. {
  343. Object o = getValueAt(row, column);
  344.  
  345. if (o != null)
  346. {
  347. return o.getClass();
  348. }
  349. }
  350.  
  351. return Object.class;
  352. }
  353. };
  354.  
  355. table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  356. table.setColumnSelectionAllowed(true); //epilegei to kathe keli ksexwrista
  357. table.getSelectionModel().addListSelectionListener(
  358. new RowColumnListSelectionListener());
  359.  
  360. table.getDefaultEditor(String.class).addCellEditorListener(
  361. new CellEditorListener() {
  362. public void editingCanceled(ChangeEvent e) {
  363. System.out.println("editingCanceled");
  364. }
  365.  
  366. public void editingStopped(ChangeEvent e) {
  367.  
  368. System.out.println("editingStopped: apply additional action");
  369.  
  370. rowIndexStart = table.getSelectedRow();
  371. rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();
  372. colIndexStart = table.getSelectedColumn();
  373. colIndexEnd = table.getColumnModel().getSelectionModel().getMaxSelectionIndex();
  374.  
  375. for ( i = rowIndexStart; i <= rowIndexEnd; i++) {
  376. for ( j = colIndexStart; j <= colIndexEnd; j++) {
  377.  
  378. Object cell_value = table.getValueAt(i,j);
  379.  
  380. Cell_value_string_after = (String) cell_value;
  381.  
  382. ia=i+1;
  383. ja=j+1;
  384.  
  385. column_name_selected2 = table.getColumnName(ja-1);
  386.  
  387.  
  388. }
  389.  
  390. }
  391.  
  392. if(Cell_value_string_before.equals(Cell_value_string_after)){
  393. System.out.println("Do nothing");
  394. }
  395. else{
  396. System.out.println("UPDATE DATABASE");
  397. update_database();
  398. }
  399. }
  400. });
  401.  
  402.  
  403.  
  404.  
  405.  
  406. JScrollPane scrollPane = new JScrollPane( table );
  407. scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  408.  
  409. scrollPane.setBounds(5, 5, xframeWidth-20, yframeHeight-70);
  410. getContentPane().add( scrollPane );
  411.  
  412.  
  413.  
  414. JPanel buttonPanel = new JPanel();
  415. buttonPanel.setBounds(5, 856, 1574, 1);
  416. getContentPane().add( buttonPanel );
  417. buttonPanel.setLayout(null);
  418.  
  419.  
  420.  
  421. }
  422.  
  423. public void update_database(){
  424.  
  425.  
  426. Object get_lastname = table.getValueAt(ia-1, 5);
  427. String get_lastname_string = (String) get_lastname;
  428. Object get_name = table.getValueAt(ia-1, 6);
  429. String get_name_string=(String) get_name;
  430.  
  431. System.out.println("RESULT= "+ column_name_selected2 + " - "+Cell_value_string_after+ " - " + get_lastname_string+ " - " + get_name_string );
  432.  
  433. if(column_name_selected2.equals("ΕΠΩΝΥΜΟ") || column_name_selected2.equals("ΟΝΟΜΑ")){
  434. JOptionPane.showMessageDialog(null, "To επώνυμο και το όνομα δεν μπορεί να αλλάξει","Μήνυμα:",JOptionPane.WARNING_MESSAGE);
  435. }
  436.  
  437. else{
  438. try {
  439. PreparedStatement update = (PreparedStatement) connection.prepareStatement
  440.  
  441. ("UPDATE `ΥΠΑΛΛΗΛΟΙ 2 TEST` SET `" +column_name_selected2+"` = ? WHERE ΕΠΩΝΥΜΟ= ? AND ΟΝΟΜΑ =? ");
  442.  
  443.  
  444. update.setString(1,Cell_value_string_after);
  445. update.setString(2,get_lastname_string);
  446. update.setString(3,get_name_string);
  447.  
  448. int all_edit_query_status=update.executeUpdate();
  449.  
  450. } catch (SQLException e) {
  451. // TODO Auto-generated catch block
  452. e.printStackTrace();
  453. }
  454.  
  455. }
  456.  
  457. }
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement