Advertisement
Guest User

Untitled

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