Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.82 KB | None | 0 0
  1. //coded by hackme
  2. //hackme-java-programs©
  3. //opensource
  4. //finito il 13-marzo-2011 alle 8.30
  5.  
  6. import java.awt.Font;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.Vector;
  15. import javax.swing.*;
  16. import javax.swing.table.DefaultTableModel;
  17.  
  18. public class rubrica{
  19.  
  20.     public Statement cmd = null;
  21.     public Vector colonne,righe = null;
  22.     public ResultSet rs = null;
  23.     public Font f = null;
  24.     public JFrame finestra = null;
  25.     public ImageIcon icona = null,img1,img2,img3,img4,img5,img6;
  26.     public JTable table = null;
  27.     public JLabel labelinfo = null;
  28.     public DefaultTableModel model = null;
  29.     public JScrollPane table1= null;
  30.     public JTextField text1= null;
  31.     public JComboBox box1;
  32.     public String [] campibox1;
  33.     public JButton button1 = null, button2 =null, button3 = null, button4 = null;
  34.     public JMenuBar menubar = null;
  35.     public JMenu menu = null,menu1 = null;
  36.     public JMenuItem item1,item2,item3,item4;
  37.    
  38.     @SuppressWarnings("unchecked")
  39.     rubrica(){
  40.        
  41.     try {
  42.             Class.forName("org.sqlite.JDBC");
  43.             Connection con = DriverManager.getConnection("jdbc:sqlite:src/db/db" );
  44.             cmd = con.createStatement();
  45.             String query = "SELECT * FROM `tabella`" ;
  46.             rs = cmd.executeQuery(query);  
  47.             colonne = new Vector();
  48.             colonne.addElement("nome");
  49.             colonne.addElement("cognome");
  50.             colonne.addElement("numero");
  51.             righe = new Vector();
  52.             while (rs.next()) {
  53.             Vector vett = new Vector();
  54.             vett.addElement(rs.getString("nome"));
  55.             vett.addElement(rs.getString("cognome"));
  56.             vett.addElement(rs.getString("numero"));
  57.             righe.addElement(vett);
  58.             }
  59.             } catch (ClassNotFoundException e) {
  60.             } catch (SQLException e) {
  61.             }
  62.    
  63.         img1 = new ImageIcon("src/img/toolbar_02.png");
  64.         img2 = new ImageIcon("src/img/toolbar_09.png");
  65.         img3 = new ImageIcon("src/img/toolbar_11.png");
  66.         img4 = new ImageIcon("src/img/toolbar_18.png");
  67.         img5 = new ImageIcon("src/img/toolbar_20.png");
  68.         img6 = new ImageIcon("src/img/toolbar_21.png");
  69.        
  70.         f = new Font("arial", Font.BOLD, 10);
  71.        
  72.         item1 = new JMenuItem("aggiungi");
  73.         item1.setIcon(img1);
  74.         item1.setFont(f);
  75.         item1.addActionListener(new aggiungi());
  76.         item1.setAccelerator(KeyStroke.getKeyStroke(java.a  wt.event.KeyEvent.VK_A, java.awt.Event.CTRL_MASK));
  77.        
  78.         item4 = new JMenuItem("about");
  79.         item4.setIcon(img5);
  80.         item4.setFont(f);
  81.         item4.addActionListener(new about());
  82.        
  83.         item2 = new JMenuItem("Aggiorna");
  84.         item2.setIcon(img2);
  85.         item2.setFont(f);
  86.         item2.addActionListener(new aggiorna());
  87.         item2.setAccelerator(KeyStroke.getKeyStroke(java.a  wt.event.KeyEvent.VK_F5,java.awt.Event.CTRL_MASK))  ;
  88.        
  89.         item3 = new JMenuItem("Esci");
  90.         item3.setFont(f);
  91.         item3.setIcon(img3);
  92.         item3.addActionListener(new esci());
  93.         item3.setAccelerator(KeyStroke.getKeyStroke(java.a  wt.event.KeyEvent.VK_Q, java.awt.Event.CTRL_MASK));
  94.        
  95.         menu = new JMenu("File");
  96.         menu.setFont(f);
  97.         menu.setIcon(img4);
  98.         menu.add(item1);    
  99.         menu.add(item2);
  100.         menu.add(item3);
  101.  
  102.         menu1 = new JMenu("Info");
  103.         menu1.setIcon(img5);
  104.         menu1.setFont(f);
  105.         menu1.add(item4);
  106.        
  107.         menubar = new JMenuBar();
  108.         menubar.add(menu);
  109.         menubar.add(menu1);
  110.        
  111.         campibox1 = new String[] {"nome","cognome","numero"};
  112.        
  113.         box1 = new JComboBox(campibox1);
  114.         box1.setFont(f);
  115.         box1.setBounds(120, 40, 100, 20);
  116.        
  117.         text1 = new JTextField();
  118.         text1.setFont(f);
  119.         text1.setBounds(10, 40, 100, 20);
  120.        
  121.         model = new DefaultTableModel(righe,colonne);
  122.  
  123.        
  124.         button1 = new JButton();
  125.         button1.setText("aggiungi");
  126.         button1.setFont(f);
  127.         button1.setBounds(10, 10, 100, 20);
  128.         button1.addActionListener(new aggiungi());
  129.        
  130.         button2 = new JButton("Rimuovi");
  131.         button2.setFont(f);
  132.         button2.addActionListener(new rimuovi());
  133.         button2.setBounds(120, 10, 100, 20);
  134.        
  135.         button3 = new JButton("Aggiorna");
  136.         button3.setFont(f);
  137.         button3.addActionListener(new aggiorna());
  138.         button3.setBounds(230, 10, 100, 20);
  139.        
  140.         button4 = new JButton(img6);
  141.         button4.setFont(f);
  142.         button4.addActionListener(new cerca());
  143.         button4.setBounds(230, 40, 100, 20);
  144.        
  145.         labelinfo = new JLabel("jRubri© hackme-java-programs©");
  146.         labelinfo.setFont(f);
  147.         labelinfo.setBounds(160, 400, 300, 25);
  148.        
  149.        
  150.         table = new JTable(model);
  151.         table1 = new JScrollPane(table);
  152.         table1.setBounds(10, 70, 320, 320);
  153.        
  154.         icona = new ImageIcon("src/img/save.gif");
  155.        
  156.         finestra = new JFrame("jRubri hackme-java-programs©");
  157.         finestra.setIconImage(icona.getImage());
  158.         finestra.setLocationRelativeTo(null);
  159.         finestra.setResizable(false);
  160.         finestra.setLayout(null);
  161.         finestra.setDefaultCloseOperation(JFrame.EXIT_ON_C  LOSE);
  162.         finestra.setSize(350, 490);
  163.         finestra.setJMenuBar(menubar);
  164.         finestra.add(table1);
  165.         finestra.add(labelinfo);
  166.         finestra.add(button1);
  167.         finestra.add(button2);
  168.         finestra.add(button3);
  169.         finestra.add(button4);
  170.         finestra.add(box1);
  171.         finestra.add(text1);
  172.         finestra.setVisible(true);
  173.     }
  174.  
  175.     public static void main(String[] args) {
  176.         @SuppressWarnings("unused")
  177.         rubrica form1 = new rubrica();
  178.        
  179.     }
  180.    
  181.     public class esci implements ActionListener{
  182.        
  183.         public void actionPerformed(ActionEvent arg0) {
  184.             System.exit(0);
  185.         }
  186.     }
  187.    
  188.     public class about implements ActionListener{
  189.  
  190.         public void actionPerformed(ActionEvent arg0) {
  191.             JOptionPane.showMessageDialog(null, "jRubri hackme-java-programs©\nProgramma libero e OpenSource\nhackme-java-programs©", "jRubri - info", JOptionPane.INFORMATION_MESSAGE);
  192.         }
  193.        
  194.     }
  195.    
  196.     public class rimuovi implements ActionListener{
  197.  
  198.         @SuppressWarnings("unchecked")
  199.         public void actionPerformed(ActionEvent e) {
  200.             try {
  201.                 Class.forName("org.sqlite.JDBC");
  202.                 Connection con = DriverManager.getConnection("jdbc:sqlite:src/db/db" );
  203.                     cmd = con.createStatement();
  204.                     String query = "DELETE FROM `tabella`";
  205.                     cmd.execute(query);
  206.                     rs = cmd.executeQuery("SELECT * FROM `tabella`");
  207.                     colonne = new Vector();
  208.                     colonne.addElement("nome");
  209.                     colonne.addElement("cognome");
  210.                     colonne.addElement("numero");
  211.                     righe = new Vector();
  212.                    
  213.                     while (rs.next()) {
  214.                     Vector vett = new Vector();
  215.                     vett.addElement(rs.getString("nome"));
  216.                     vett.addElement(rs.getString("cognome"));
  217.                     vett.addElement(rs.getString("numero"));
  218.                     righe.addElement(vett);
  219.                     }
  220.                     model = new DefaultTableModel(righe, colonne);
  221.                     table.setModel(model);
  222.                     table1.repaint();
  223.  
  224.                 } catch (ClassNotFoundException e3) {
  225.                     System.out.println(e3);
  226.                 } catch (SQLException e3) {
  227.                     System.out.println(e3);
  228.                 }
  229.         }
  230.        
  231.     }
  232.    
  233.     public class aggiorna implements ActionListener{
  234.  
  235.         @SuppressWarnings("unchecked")
  236.         public void actionPerformed(ActionEvent e) {
  237.               try {
  238.                   Class.forName("org.sqlite.JDBC");
  239.                     Connection con = DriverManager.getConnection("jdbc:sqlite:src/db/db" );
  240.                     cmd = con.createStatement();
  241.                     String query = "SELECT * FROM `tabella`";
  242.                     rs = cmd.executeQuery(query);  
  243.                     colonne = new Vector();
  244.                     colonne.addElement("nome");
  245.                     colonne.addElement("cognome");
  246.                     colonne.addElement("numero");
  247.                     righe = new Vector();
  248.                     while (rs.next()) {
  249.                     Vector vett = new Vector();
  250.                     vett.addElement(rs.getString("nome"));
  251.                     vett.addElement(rs.getString("cognome"));
  252.                     vett.addElement(rs.getString("numero"));
  253.                     righe.addElement(vett);
  254.                     }
  255.                
  256.                     model = new DefaultTableModel(righe, colonne);
  257.                     table.setModel(model);
  258.                     table1.repaint();
  259.  
  260.                 } catch (ClassNotFoundException e2) {
  261.            
  262.                 } catch (SQLException e2) {
  263.                    
  264.                 }          
  265.         }
  266.        
  267.     }
  268.    
  269.     public class cerca implements ActionListener {
  270.  
  271.  
  272.         @SuppressWarnings("unchecked")
  273.         public void actionPerformed(ActionEvent arg0) {
  274.               try {
  275.                   Class.forName("org.sqlite.JDBC");
  276.                     Connection con = DriverManager.getConnection("jdbc:sqlite:src/db/db" );
  277.                     cmd = con.createStatement();
  278.                     String query = "SELECT * FROM `tabella` where `"+ box1.getSelectedItem() +"` like '%" + text1.getText() + "%'";
  279.                     rs = cmd.executeQuery(query);  
  280.                     colonne = new Vector();
  281.                     colonne.addElement("nome");
  282.                     colonne.addElement("cognome");
  283.                     colonne.addElement("numero");
  284.                     righe = new Vector();
  285.                     while (rs.next()) {
  286.                     Vector vett = new Vector();
  287.                     vett.addElement(rs.getString("nome"));
  288.                     vett.addElement(rs.getString("cognome"));
  289.                     vett.addElement(rs.getString("numero"));
  290.                     righe.addElement(vett);
  291.                     }
  292.                
  293.                     model = new DefaultTableModel(righe, colonne);
  294.                     table.setModel(model);
  295.                     table1.repaint();
  296.  
  297.                 } catch (ClassNotFoundException e) {
  298.            
  299.                 } catch (SQLException e) {
  300.                    
  301.                 }          
  302.         }
  303.        
  304.     }
  305.    
  306.    
  307.     public class aggiungi implements ActionListener{
  308.  
  309.         @SuppressWarnings("unchecked")
  310.         public void actionPerformed(ActionEvent arg0) {
  311.        
  312.             String insNome = null;
  313.             String insCognome = null;
  314.             String insNumero = null;
  315.    
  316.                 try {
  317.                    
  318.                 insNome = JOptionPane.showInputDialog(null, "Inserisci Nome:");
  319.                 insCognome = JOptionPane.showInputDialog(null, "Inserisci Cognome:");
  320.                 insNumero = JOptionPane.showInputDialog(null, "Inserisci Numero:");
  321.                 if (insNome.equals("") & insCognome.equals("") & insNumero.equals("")){
  322.                     JOptionPane.showMessageDialog(null, "Inserisci almeno un campo per aggiungere\nuna persona a jRubri", "jRubri - Errore:", JOptionPane.ERROR_MESSAGE);
  323.                     }else if(insNome.equals(null) & insCognome.equals(null) & insNumero.equals(null)){
  324.                         JOptionPane.showMessageDialog(null, "Inserisci almeno un campo per aggiungere\nuna persona a jRubri", "jRubri - Errore:", JOptionPane.ERROR_MESSAGE);
  325.                     }else{
  326.                         Class.forName("org.sqlite.JDBC");
  327.                         Connection con = DriverManager.getConnection("jdbc:sqlite:src/db/db" );
  328.                         cmd = con.createStatement();
  329.                         String query = "INSERT into `tabella` (`nome`,`cognome`,`numero`) values('" + insNome + "','" + insCognome + "','"+ insNumero + "')";
  330.                         cmd.execute(query);
  331.                         rs = cmd.executeQuery("SELECT * FROM `tabella`");
  332.                         colonne = new Vector();
  333.                         colonne.addElement("nome");
  334.                         colonne.addElement("cognome");
  335.                         colonne.addElement("numero");
  336.                         righe = new Vector();
  337.                        
  338.                         while (rs.next()) {
  339.                         Vector vett = new Vector();
  340.                         vett.addElement(rs.getString("nome"));
  341.                         vett.addElement(rs.getString("cognome"));
  342.                         vett.addElement(rs.getString("numero"));
  343.                         righe.addElement(vett);
  344.                         }
  345.                         model = new DefaultTableModel(righe, colonne);
  346.                         table.setModel(model);
  347.                         table1.repaint();
  348.                     }
  349.                     } catch (ClassNotFoundException e) {
  350.                         System.out.println(e);
  351.                     } catch (SQLException e) {
  352.                         System.out.println(e);
  353.                     }
  354.                
  355.     }
  356.     }
  357.  
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement