Advertisement
Guest User

Untitled

a guest
May 31st, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1.  
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.FlowLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.sql.DriverManager;
  12. import java.util.Vector;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.JProgressBar;
  19. import javax.swing.JScrollPane;
  20. import javax.swing.JSpinner;
  21. import javax.swing.JTabbedPane;
  22. import javax.swing.JTable;
  23. import javax.swing.SpinnerNumberModel;
  24. import javax.swing.SwingUtilities;
  25. import javax.swing.border.EmptyBorder;
  26. import javax.swing.table.DefaultTableModel;
  27. import java.sql.Connection;
  28. import java.sql.ResultSet;
  29. import java.sql.SQLException;
  30. import java.sql.Statement;
  31. import java.util.logging.Level;
  32. import java.util.logging.Logger;
  33.  
  34. /*
  35. * To change this license header, choose License Headers in Project Properties.
  36. * To change this template file, choose Tools | Templates
  37. * and open the template in the editor.
  38. */
  39. /**
  40. *
  41. * @author user
  42. */
  43. public class Szorzat extends JFrame implements ActionListener {
  44.  
  45. SpinnerNumberModel model1 = new SpinnerNumberModel(5, 0, 40, 1);
  46. SpinnerNumberModel model2 = new SpinnerNumberModel(5, 0, 40, 1);
  47. private JSpinner spinner = new JSpinner(model1);
  48. private JSpinner spinner2 = new JSpinner(model2);
  49. private JLabel lbl1 = new JLabel("<html>Biros Andras<br/>andras@biros.hu</html>");
  50. private JButton btnszamitas = new JButton("Szamolas");
  51. private JPanel panel = new JPanel();
  52. private JPanel panel2 = new JPanel();
  53. JTabbedPane tabbedpane = new JTabbedPane();
  54. JProgressBar progressbar = new JProgressBar();
  55. DefaultTableModel model = new DefaultTableModel();
  56. JTable table = new JTable(model);
  57. JScrollPane jsp = new JScrollPane(table);
  58. private JButton btnbetoltes = new JButton("Betöltés állományból");
  59. private JButton btnkimentes = new JButton("Mentés állományba");
  60. private JButton btndbbetoltes = new JButton("DB-bő' ");
  61.  
  62. private JButton btndbkimentes = new JButton("DB-be nyom");
  63.  
  64. public Szorzat() {
  65.  
  66. panel2.add(lbl1);
  67. tabbedpane.add("About", panel2);
  68. tabbedpane.setBorder(new EmptyBorder(10, 10, 10, 10));
  69.  
  70. add(tabbedpane, BorderLayout.NORTH);
  71. setSize(450, 450);
  72. panel.setLayout(new FlowLayout());
  73. setDefaultCloseOperation(EXIT_ON_CLOSE);
  74. setLocationRelativeTo(this);
  75. panel.add(spinner);
  76. panel.add(spinner2);
  77. panel.add(btnszamitas);
  78. btnbetoltes.setBackground(Color.GREEN);
  79. btnbetoltes.addActionListener(this);
  80. btnkimentes.addActionListener(this);
  81. btnkimentes.setBackground(Color.GREEN);
  82. btndbbetoltes.addActionListener(this);
  83. btndbkimentes.addActionListener(this);
  84. btndbbetoltes.setBackground(Color.yellow);
  85. btndbkimentes.setBackground(Color.yellow);
  86.  
  87. panel.add(btnbetoltes);
  88. panel.add(btnkimentes);
  89. panel.add(btndbbetoltes);
  90. panel.add(btndbkimentes);
  91. panel.add(progressbar);
  92.  
  93. model.addColumn("Első szám:");
  94. model.addColumn("Második szám: ");
  95. model.addColumn("Eredmény: ");
  96. table.setModel(model);
  97. panel.add(jsp);
  98. btnszamitas.addActionListener(this);
  99. add(panel);
  100. setVisible(true);
  101. }
  102.  
  103. public static int multiplyNaturals(int First, int Second) {
  104.  
  105. return 0;
  106. }
  107.  
  108. public static Connection getConnection() throws Exception {
  109. try {
  110. String driver = "com.mysql.jdbc.Driver";
  111. String url = "jdbc:mysql://localhost:3306";
  112. String username = "root";
  113. String password = "root";
  114. Class.forName(driver);
  115.  
  116. Connection conn = DriverManager.getConnection(url, username, password);
  117. System.out.println("Connected");
  118. return conn;
  119. } catch (Exception e) {
  120. System.out.println(e);
  121. }
  122. return null;
  123. }
  124.  
  125. private Vector<String> getColumnNames() {
  126. Vector<String> columnNames = new Vector<String>();
  127. for (int i = 0; i < table.getColumnCount(); i++) {
  128. columnNames.add(table.getColumnName(i));
  129. }
  130. return columnNames;
  131. }
  132.  
  133. public static void main(String[] args) {
  134. Szorzat szorzat = new Szorzat();
  135. }
  136.  
  137. @Override
  138. public void actionPerformed(ActionEvent e) {
  139. if (e.getSource() == btnszamitas) {
  140. int szam = (int) spinner.getValue();
  141. int szam2 = (int) spinner2.getValue();
  142. int eredmeny = szam * szam2;
  143.  
  144. Thread t = new Thread(new Runnable() {
  145. @Override
  146. public void run() {
  147. try {
  148. for (int i = 0; i < 10; i++) {
  149. Thread.sleep(100);
  150. final int i20 = i * 20;
  151. SwingUtilities.invokeAndWait(new Runnable() {
  152. public void run() {
  153. progressbar.setValue(i20);
  154.  
  155. }
  156. });
  157. }
  158.  
  159. SwingUtilities.invokeAndWait(new Runnable() {
  160.  
  161. public void run() {
  162. JOptionPane.showMessageDialog(null, "Az eredmény: " + eredmeny);
  163. model.addRow(new Object[]{szam, szam2, eredmeny});
  164. }
  165.  
  166. }
  167. );
  168. } catch (Exception e) {
  169. e.printStackTrace();
  170. }
  171. }
  172.  
  173. });
  174. t.start();
  175.  
  176. }
  177. if (e.getSource() == btnbetoltes) {
  178. try {
  179. ObjectInputStream in = new ObjectInputStream(
  180. new FileInputStream("test.txt"));
  181. Vector rowData = (Vector) in.readObject();
  182.  
  183. Vector columnNames = (Vector) in.readObject();
  184. model.setDataVector(rowData, columnNames);
  185.  
  186. in.close();
  187. } catch (Exception ex) {
  188. ex.printStackTrace();
  189.  
  190. }
  191. }
  192.  
  193. if (e.getSource() == btnkimentes) {
  194.  
  195. try {
  196. ObjectOutputStream out = new ObjectOutputStream(
  197. new FileOutputStream("test.txt"));
  198. out.writeObject(model.getDataVector());
  199. out.writeObject(getColumnNames());
  200.  
  201. out.close();
  202. } catch (Exception ex) {
  203. ex.printStackTrace();
  204. }
  205.  
  206. }
  207. if (e.getSource() == btndbbetoltes) {
  208.  
  209. Statement stmt = null;
  210. String dbBoTotQuery = "select * from szorzas.dump;";
  211.  
  212. try {
  213. try {
  214. stmt = getConnection().createStatement();
  215. } catch (Exception ex) {
  216. Logger.getLogger(Szorzat.class.getName()).log(Level.SEVERE, null, ex);
  217. }
  218. ResultSet rs = stmt.executeQuery(dbBoTotQuery);
  219. while (rs.next()) {
  220. model.addRow(new Object[]{rs.getInt("F"), rs.getInt("S"), rs.getInt("P")});
  221. }
  222. } catch (SQLException ex) {
  223. Logger.getLogger(Szorzat.class.getName()).log(Level.SEVERE, null, ex);
  224. }
  225. }
  226.  
  227. if (e.getSource() == btndbkimentes) {
  228.  
  229. Statement stmt = null;
  230. try {
  231. stmt = getConnection().createStatement();
  232. String dbTorolQuery = "delete from szorzas.dump;";
  233. stmt.executeUpdate(dbTorolQuery);
  234. for (int i = 0; i < model.getRowCount(); i++) {
  235.  
  236. String dbBeNyomQuery = "insert into szorzas.dump (F, S, P) values (" + model.getValueAt(i, 0) + "," + model.getValueAt(i, 1) + "," + model.getValueAt(i, 2) + ");";
  237. stmt.executeUpdate(dbBeNyomQuery);
  238. }
  239. } catch (Exception ex) {
  240. Logger.getLogger(Szorzat.class.getName()).log(Level.SEVERE, null, ex);
  241. }
  242.  
  243. }
  244.  
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement