Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.sql.DriverManager;
- import java.util.Vector;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JProgressBar;
- import javax.swing.JScrollPane;
- import javax.swing.JSpinner;
- import javax.swing.JTabbedPane;
- import javax.swing.JTable;
- import javax.swing.SpinnerNumberModel;
- import javax.swing.SwingUtilities;
- import javax.swing.border.EmptyBorder;
- import javax.swing.table.DefaultTableModel;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author user
- */
- public class Szorzat extends JFrame implements ActionListener {
- SpinnerNumberModel model1 = new SpinnerNumberModel(5, 0, 40, 1);
- SpinnerNumberModel model2 = new SpinnerNumberModel(5, 0, 40, 1);
- private JSpinner spinner = new JSpinner(model1);
- private JSpinner spinner2 = new JSpinner(model2);
- private JLabel lbl1 = new JLabel("<html>Biros Andras<br/>andras@biros.hu</html>");
- private JButton btnszamitas = new JButton("Szamolas");
- private JPanel panel = new JPanel();
- private JPanel panel2 = new JPanel();
- JTabbedPane tabbedpane = new JTabbedPane();
- JProgressBar progressbar = new JProgressBar();
- DefaultTableModel model = new DefaultTableModel();
- JTable table = new JTable(model);
- JScrollPane jsp = new JScrollPane(table);
- private JButton btnbetoltes = new JButton("Betöltés állományból");
- private JButton btnkimentes = new JButton("Mentés állományba");
- private JButton btndbbetoltes = new JButton("DB-bő' ");
- private JButton btndbkimentes = new JButton("DB-be nyom");
- int eredmeny;
- public Szorzat() {
- panel2.add(lbl1);
- tabbedpane.add("About", panel2);
- tabbedpane.setBorder(new EmptyBorder(10, 10, 10, 10));
- add(tabbedpane, BorderLayout.NORTH);
- setSize(450, 450);
- panel.setLayout(new FlowLayout());
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setLocationRelativeTo(this);
- panel.add(spinner);
- panel.add(spinner2);
- panel.add(btnszamitas);
- btnbetoltes.setBackground(Color.GREEN);
- btnbetoltes.addActionListener(this);
- btnkimentes.addActionListener(this);
- btnkimentes.setBackground(Color.GREEN);
- btndbbetoltes.addActionListener(this);
- btndbkimentes.addActionListener(this);
- btndbbetoltes.setBackground(Color.yellow);
- btndbkimentes.setBackground(Color.yellow);
- panel.add(btnbetoltes);
- panel.add(btnkimentes);
- panel.add(btndbbetoltes);
- panel.add(btndbkimentes);
- panel.add(progressbar);
- model.addColumn("Első szám:");
- model.addColumn("Második szám: ");
- model.addColumn("Eredmény: ");
- table.setModel(model);
- panel.add(jsp);
- btnszamitas.addActionListener(this);
- add(panel);
- setVisible(true);
- }
- public static Connection getConnection() throws Exception {
- try {
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306";
- String username = "root";
- String password = "root";
- Class.forName(driver);
- Connection conn = DriverManager.getConnection(url, username, password);
- System.out.println("Connected");
- return conn;
- } catch (Exception e) {
- System.out.println(e);
- }
- return null;
- }
- private Vector<String> getColumnNames() {
- Vector<String> columnNames = new Vector<String>();
- for (int i = 0; i < table.getColumnCount(); i++) {
- columnNames.add(table.getColumnName(i));
- }
- return columnNames;
- }
- public static void main(String[] args) {
- Szorzat Szorzat = new Szorzat();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnszamitas) {
- int szam = (int) spinner.getValue();
- int szam2 = (int) spinner2.getValue();
- eredmeny = 1;
- for (int i = 0; i < szam2; i++) {
- eredmeny *= szam;
- }
- Thread t = new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- for (int i = 0; i < 10; i++) {
- Thread.sleep(100);
- final int i20 = i * 20;
- SwingUtilities.invokeAndWait(new Runnable() {
- public void run() {
- progressbar.setValue(i20);
- }
- });
- }
- SwingUtilities.invokeAndWait(new Runnable() {
- public void run() {
- JOptionPane.showMessageDialog(null, "Az eredmény: " + eredmeny);
- model.addRow(new Object[]{szam, szam2, eredmeny});
- }
- }
- );
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- t.start();
- }
- if (e.getSource() == btnbetoltes) {
- try {
- ObjectInputStream in = new ObjectInputStream(
- new FileInputStream("test.txt"));
- Vector rowData = (Vector) in.readObject();
- Vector columnNames = (Vector) in.readObject();
- model.setDataVector(rowData, columnNames);
- in.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- if (e.getSource() == btnkimentes) {
- try {
- ObjectOutputStream out = new ObjectOutputStream(
- new FileOutputStream("test.txt"));
- out.writeObject(model.getDataVector());
- out.writeObject(getColumnNames());
- out.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- if (e.getSource() == btndbbetoltes) {
- Statement stmt = null;
- String dbBoTotQuery = "select * from szorzas.dump;";
- try {
- try {
- stmt = getConnection().createStatement();
- } catch (Exception ex) {
- Logger.getLogger(Szorzat.class.getName()).log(Level.SEVERE, null, ex);
- }
- ResultSet rs = stmt.executeQuery(dbBoTotQuery);
- while (rs.next()) {
- model.addRow(new Object[]{rs.getInt("F"), rs.getInt("S"), rs.getInt("P")});
- }
- } catch (SQLException ex) {
- Logger.getLogger(Szorzat.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- if (e.getSource() == btndbkimentes) {
- Statement stmt = null;
- try {
- stmt = getConnection().createStatement();
- String dbTorolQuery = "delete from szorzas.dump;";
- stmt.executeUpdate(dbTorolQuery);
- for (int i = 0; i < model.getRowCount(); i++) {
- String dbBeNyomQuery = "insert into szorzas.dump (F, S, P) values (" + model.getValueAt(i, 0) + "," + model.getValueAt(i, 1) + "," + model.getValueAt(i, 2) + ");";
- stmt.executeUpdate(dbBeNyomQuery);
- }
- } catch (Exception ex) {
- Logger.getLogger(Szorzat.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement