Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //coded by hackme
- //hackme-java-programs©
- //opensource
- //finito il 13-marzo-2011 alle 8.30
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Vector;
- import javax.swing.*;
- import javax.swing.table.DefaultTableModel;
- public class rubrica{
- public Statement cmd = null;
- public Vector colonne,righe = null;
- public ResultSet rs = null;
- public Font f = null;
- public JFrame finestra = null;
- public ImageIcon icona = null,img1,img2,img3,img4,img5,img6;
- public JTable table = null;
- public JLabel labelinfo = null;
- public DefaultTableModel model = null;
- public JScrollPane table1= null;
- public JTextField text1= null;
- public JComboBox box1;
- public String [] campibox1;
- public JButton button1 = null, button2 =null, button3 = null, button4 = null;
- public JMenuBar menubar = null;
- public JMenu menu = null,menu1 = null;
- public JMenuItem item1,item2,item3,item4;
- @SuppressWarnings("unchecked")
- rubrica(){
- try {
- Class.forName("org.sqlite.JDBC");
- Connection con = DriverManager.getConnection("jdbc:sqlite:src/db/db" );
- cmd = con.createStatement();
- String query = "SELECT * FROM `tabella`" ;
- rs = cmd.executeQuery(query);
- colonne = new Vector();
- colonne.addElement("nome");
- colonne.addElement("cognome");
- colonne.addElement("numero");
- righe = new Vector();
- while (rs.next()) {
- Vector vett = new Vector();
- vett.addElement(rs.getString("nome"));
- vett.addElement(rs.getString("cognome"));
- vett.addElement(rs.getString("numero"));
- righe.addElement(vett);
- }
- } catch (ClassNotFoundException e) {
- } catch (SQLException e) {
- }
- img1 = new ImageIcon("src/img/toolbar_02.png");
- img2 = new ImageIcon("src/img/toolbar_09.png");
- img3 = new ImageIcon("src/img/toolbar_11.png");
- img4 = new ImageIcon("src/img/toolbar_18.png");
- img5 = new ImageIcon("src/img/toolbar_20.png");
- img6 = new ImageIcon("src/img/toolbar_21.png");
- f = new Font("arial", Font.BOLD, 10);
- item1 = new JMenuItem("aggiungi");
- item1.setIcon(img1);
- item1.setFont(f);
- item1.addActionListener(new aggiungi());
- item1.setAccelerator(KeyStroke.getKeyStroke(java.a wt.event.KeyEvent.VK_A, java.awt.Event.CTRL_MASK));
- item4 = new JMenuItem("about");
- item4.setIcon(img5);
- item4.setFont(f);
- item4.addActionListener(new about());
- item2 = new JMenuItem("Aggiorna");
- item2.setIcon(img2);
- item2.setFont(f);
- item2.addActionListener(new aggiorna());
- item2.setAccelerator(KeyStroke.getKeyStroke(java.a wt.event.KeyEvent.VK_F5,java.awt.Event.CTRL_MASK)) ;
- item3 = new JMenuItem("Esci");
- item3.setFont(f);
- item3.setIcon(img3);
- item3.addActionListener(new esci());
- item3.setAccelerator(KeyStroke.getKeyStroke(java.a wt.event.KeyEvent.VK_Q, java.awt.Event.CTRL_MASK));
- menu = new JMenu("File");
- menu.setFont(f);
- menu.setIcon(img4);
- menu.add(item1);
- menu.add(item2);
- menu.add(item3);
- menu1 = new JMenu("Info");
- menu1.setIcon(img5);
- menu1.setFont(f);
- menu1.add(item4);
- menubar = new JMenuBar();
- menubar.add(menu);
- menubar.add(menu1);
- campibox1 = new String[] {"nome","cognome","numero"};
- box1 = new JComboBox(campibox1);
- box1.setFont(f);
- box1.setBounds(120, 40, 100, 20);
- text1 = new JTextField();
- text1.setFont(f);
- text1.setBounds(10, 40, 100, 20);
- model = new DefaultTableModel(righe,colonne);
- button1 = new JButton();
- button1.setText("aggiungi");
- button1.setFont(f);
- button1.setBounds(10, 10, 100, 20);
- button1.addActionListener(new aggiungi());
- button2 = new JButton("Rimuovi");
- button2.setFont(f);
- button2.addActionListener(new rimuovi());
- button2.setBounds(120, 10, 100, 20);
- button3 = new JButton("Aggiorna");
- button3.setFont(f);
- button3.addActionListener(new aggiorna());
- button3.setBounds(230, 10, 100, 20);
- button4 = new JButton(img6);
- button4.setFont(f);
- button4.addActionListener(new cerca());
- button4.setBounds(230, 40, 100, 20);
- labelinfo = new JLabel("jRubri© hackme-java-programs©");
- labelinfo.setFont(f);
- labelinfo.setBounds(160, 400, 300, 25);
- table = new JTable(model);
- table1 = new JScrollPane(table);
- table1.setBounds(10, 70, 320, 320);
- icona = new ImageIcon("src/img/save.gif");
- finestra = new JFrame("jRubri hackme-java-programs©");
- finestra.setIconImage(icona.getImage());
- finestra.setLocationRelativeTo(null);
- finestra.setResizable(false);
- finestra.setLayout(null);
- finestra.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);
- finestra.setSize(350, 490);
- finestra.setJMenuBar(menubar);
- finestra.add(table1);
- finestra.add(labelinfo);
- finestra.add(button1);
- finestra.add(button2);
- finestra.add(button3);
- finestra.add(button4);
- finestra.add(box1);
- finestra.add(text1);
- finestra.setVisible(true);
- }
- public static void main(String[] args) {
- @SuppressWarnings("unused")
- rubrica form1 = new rubrica();
- }
- public class esci implements ActionListener{
- public void actionPerformed(ActionEvent arg0) {
- System.exit(0);
- }
- }
- public class about implements ActionListener{
- public void actionPerformed(ActionEvent arg0) {
- JOptionPane.showMessageDialog(null, "jRubri hackme-java-programs©\nProgramma libero e OpenSource\nhackme-java-programs©", "jRubri - info", JOptionPane.INFORMATION_MESSAGE);
- }
- }
- public class rimuovi implements ActionListener{
- @SuppressWarnings("unchecked")
- public void actionPerformed(ActionEvent e) {
- try {
- Class.forName("org.sqlite.JDBC");
- Connection con = DriverManager.getConnection("jdbc:sqlite:src/db/db" );
- cmd = con.createStatement();
- String query = "DELETE FROM `tabella`";
- cmd.execute(query);
- rs = cmd.executeQuery("SELECT * FROM `tabella`");
- colonne = new Vector();
- colonne.addElement("nome");
- colonne.addElement("cognome");
- colonne.addElement("numero");
- righe = new Vector();
- while (rs.next()) {
- Vector vett = new Vector();
- vett.addElement(rs.getString("nome"));
- vett.addElement(rs.getString("cognome"));
- vett.addElement(rs.getString("numero"));
- righe.addElement(vett);
- }
- model = new DefaultTableModel(righe, colonne);
- table.setModel(model);
- table1.repaint();
- } catch (ClassNotFoundException e3) {
- System.out.println(e3);
- } catch (SQLException e3) {
- System.out.println(e3);
- }
- }
- }
- public class aggiorna implements ActionListener{
- @SuppressWarnings("unchecked")
- public void actionPerformed(ActionEvent e) {
- try {
- Class.forName("org.sqlite.JDBC");
- Connection con = DriverManager.getConnection("jdbc:sqlite:src/db/db" );
- cmd = con.createStatement();
- String query = "SELECT * FROM `tabella`";
- rs = cmd.executeQuery(query);
- colonne = new Vector();
- colonne.addElement("nome");
- colonne.addElement("cognome");
- colonne.addElement("numero");
- righe = new Vector();
- while (rs.next()) {
- Vector vett = new Vector();
- vett.addElement(rs.getString("nome"));
- vett.addElement(rs.getString("cognome"));
- vett.addElement(rs.getString("numero"));
- righe.addElement(vett);
- }
- model = new DefaultTableModel(righe, colonne);
- table.setModel(model);
- table1.repaint();
- } catch (ClassNotFoundException e2) {
- } catch (SQLException e2) {
- }
- }
- }
- public class cerca implements ActionListener {
- @SuppressWarnings("unchecked")
- public void actionPerformed(ActionEvent arg0) {
- try {
- Class.forName("org.sqlite.JDBC");
- Connection con = DriverManager.getConnection("jdbc:sqlite:src/db/db" );
- cmd = con.createStatement();
- String query = "SELECT * FROM `tabella` where `"+ box1.getSelectedItem() +"` like '%" + text1.getText() + "%'";
- rs = cmd.executeQuery(query);
- colonne = new Vector();
- colonne.addElement("nome");
- colonne.addElement("cognome");
- colonne.addElement("numero");
- righe = new Vector();
- while (rs.next()) {
- Vector vett = new Vector();
- vett.addElement(rs.getString("nome"));
- vett.addElement(rs.getString("cognome"));
- vett.addElement(rs.getString("numero"));
- righe.addElement(vett);
- }
- model = new DefaultTableModel(righe, colonne);
- table.setModel(model);
- table1.repaint();
- } catch (ClassNotFoundException e) {
- } catch (SQLException e) {
- }
- }
- }
- public class aggiungi implements ActionListener{
- @SuppressWarnings("unchecked")
- public void actionPerformed(ActionEvent arg0) {
- String insNome = null;
- String insCognome = null;
- String insNumero = null;
- try {
- insNome = JOptionPane.showInputDialog(null, "Inserisci Nome:");
- insCognome = JOptionPane.showInputDialog(null, "Inserisci Cognome:");
- insNumero = JOptionPane.showInputDialog(null, "Inserisci Numero:");
- if (insNome.equals("") & insCognome.equals("") & insNumero.equals("")){
- JOptionPane.showMessageDialog(null, "Inserisci almeno un campo per aggiungere\nuna persona a jRubri", "jRubri - Errore:", JOptionPane.ERROR_MESSAGE);
- }else if(insNome.equals(null) & insCognome.equals(null) & insNumero.equals(null)){
- JOptionPane.showMessageDialog(null, "Inserisci almeno un campo per aggiungere\nuna persona a jRubri", "jRubri - Errore:", JOptionPane.ERROR_MESSAGE);
- }else{
- Class.forName("org.sqlite.JDBC");
- Connection con = DriverManager.getConnection("jdbc:sqlite:src/db/db" );
- cmd = con.createStatement();
- String query = "INSERT into `tabella` (`nome`,`cognome`,`numero`) values('" + insNome + "','" + insCognome + "','"+ insNumero + "')";
- cmd.execute(query);
- rs = cmd.executeQuery("SELECT * FROM `tabella`");
- colonne = new Vector();
- colonne.addElement("nome");
- colonne.addElement("cognome");
- colonne.addElement("numero");
- righe = new Vector();
- while (rs.next()) {
- Vector vett = new Vector();
- vett.addElement(rs.getString("nome"));
- vett.addElement(rs.getString("cognome"));
- vett.addElement(rs.getString("numero"));
- righe.addElement(vett);
- }
- model = new DefaultTableModel(righe, colonne);
- table.setModel(model);
- table1.repaint();
- }
- } catch (ClassNotFoundException e) {
- System.out.println(e);
- } catch (SQLException e) {
- System.out.println(e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement