Studyon Minte9.com
Jdbc




H2 CRUD




package testexample; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.*; public class Test extends JFrame { private ActionInsert actionInsert = new ActionInsert(); // --- Test.getInstance().actionInsert --- // private ActionSelect actionSelect = new ActionSelect(); private ActionUpdate actionUpdate = new ActionUpdate(); private ActionDelete actionDelete = new ActionDelete(); private static Test app = new Test(); public static Test getInstance() { return app; } private Test() { initMenu(); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { // --- Safer, threads Concurrency --- // @Override public void run() { app.setBounds(200, 200, 300, 200); app.setDefaultCloseOperation(EXIT_ON_CLOSE); app.setLayout(new BorderLayout()); app.setVisible(true); } }); } public void initMenu() { // --- Menu actions --- // JMenuBar menuBar = new JMenuBar(); JMenu menuFile = new JMenu("File"); JMenuItem menuItem1 = new JMenuItem("Create"); JMenuItem menuItem2 = new JMenuItem("Read"); JMenuItem menuItem3 = new JMenuItem("Update"); JMenuItem menuItem4 = new JMenuItem("Delete"); menuBar.add(menuFile); menuFile.add(menuItem1); menuFile.add(menuItem2); menuFile.add(menuItem3); menuFile.add(menuItem4); setJMenuBar(menuBar); menuItem1.addActionListener(new MenuAction(actionInsert)); menuItem2.addActionListener(new MenuAction(actionSelect)); menuItem3.addActionListener(new MenuAction(actionUpdate)); menuItem4.addActionListener(new MenuAction(actionDelete)); } private class MenuAction implements ActionListener { private JPanel panel; private MenuAction(JPanel panel) { this.panel = panel; } @Override public void actionPerformed(ActionEvent e) { changePanel(panel); } } public void changePanel(JPanel panel) { // --- Change current panel, on Menu switch --- // getContentPane().removeAll(); getContentPane().add(panel, BorderLayout.CENTER); getContentPane().doLayout(); validate(); repaint(); } public void refresh(int action) { // --- Refresh lists --- // getContentPane().removeAll(); // drop old ones Test.getInstance().actionSelect.init(); Test.getInstance().actionUpdate.init(); Test.getInstance().actionDelete.init(); switch(action) { case 2: Test.getInstance().changePanel(Test.getInstance().actionSelect); break; case 4: Test.getInstance().changePanel(Test.getInstance().actionDelete); break; default: } } } class ActionInsert extends JPanel { // --- Controller --- // DbModel dbModel = new DbModel(); public ActionInsert() { JLabel jLabel1 = new JLabel("Team"); add(jLabel1); final JTextField jTextField1 = new JTextField("", 20); add(jTextField1); JButton jButton1 = new JButton("Insert"); add(jButton1); jButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dbModel.insert(jTextField1.getText()); // --- Action Insert --- // jTextField1.setText(""); Test.getInstance().refresh(2); } }); } } class ActionSelect extends JPanel { DbModel dbModel = new DbModel(); public ActionSelect() { init(); } public void init() { TreeMap<Integer, Properties> data = dbModel.select(); // --- Action Select --- // String output = ""; for(Object i : data.keySet()) { Properties p = (Properties) data.get(i); output += p.getProperty("name") + ", n"; } removeAll(); JTextPane pane = new JTextPane(); pane.setText(output); add(pane); } } class ActionDelete extends JPanel { DbModel dbModel = new DbModel(); public ActionDelete() { init(); } public void init() { removeAll(); JPanel main = new JPanel(); // --- Main panel, contains rows panels --- // main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS)); TreeMap data = dbModel.select(); // --- Action Select --- // for (Object i : data.keySet()) { Properties p = (Properties) data.get(i); final String name = p.getProperty("name"); final String id = p.getProperty("id"); JPanel panel = new JPanel(); // --- Row panel --- // JLabel label = new JLabel(name); panel.add(label); JButton button = new JButton("delete"); panel.add(button); button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { dbModel.delete(id); // --- Action Delete --- // Test.getInstance().refresh(4); } }); main.add(panel); // --- Row panel to Main panel --- // } add(main); } } class ActionUpdate extends JPanel { DbModel dbModel = new DbModel(); public ActionUpdate() { init(); } public void init() { removeAll(); JPanel main = new JPanel(); main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS)); TreeMap data = dbModel.select(); for (Object i : data.keySet()) { Properties p = (Properties) data.get(i); final String name = p.getProperty("name"); final String id = p.getProperty("id"); JPanel panel = new JPanel(); final JTextField nameField = new JTextField(name, 10); panel.add(nameField); JButton button = new JButton("update"); panel.add(button); button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { dbModel.update(id, nameField.getText()); // --- Action Update --- // Test.getInstance().refresh(2); } }); main.add(panel); } add(main); } } class DbModel { // --- Model --- // private static Connection con; public DbModel() { setConnection(); setTable(); } public static void setConnection() { // --- DB Connection --- // try { Class.forName("org.h2.Driver"); con = DriverManager.getConnection("jdbc:h2:file:data/test", "test", "" ); // relative to project // jdbc:h2:~/test - relative to user home directory } catch (Exception e) {} } public static void setTable() { // --- Create Table --- // try { Statement stmt = con.createStatement(); //stmt.executeUpdate( "DROP TABLE teams" ); stmt.executeUpdate( "CREATE TABLE IF NOT EXISTS teams ( id int(10) auto_increment, name varchar(50) )" ); stmt.close(); } catch (Exception e) {} } public void insert(String value) { if (value.equals("")) return; try { Statement stmt = con.createStatement(); // --- SQL Insert --- // stmt.executeUpdate( "INSERT INTO teams ( name ) VALUES ( '"+ value +"' )" ); stmt.close(); } catch (Exception sqlException) { System.out.println(sqlException.getMessage()); } } public TreeMap select() { TreeMap data = new TreeMap<Integer, Properties>(); try { Statement stmt = con.createStatement(); // --- SQL Select --- // ResultSet rs = stmt.executeQuery("SELECT * FROM teams"); int i=0; while( rs.next() ) { Properties p = new Properties(); p.put("id", rs.getString("id")); p.put("name", rs.getString("name")); data.put(i++, p); } stmt.close(); } catch (Exception e) {} return data; } public void delete(String id) { try { Statement stmt = con.createStatement(); // --- SQL Delete --- // stmt.executeUpdate( "DELETE FROM teams WHERE id = '"+ id +"'" ); stmt.close(); } catch (Exception sqlException) { System.out.println(sqlException.getMessage()); } } public void update(String id, String name) { try { Statement stmt = con.createStatement(); // --- SQL Update --- // stmt.executeUpdate( "UPDATE teams SET name = '"+ name +"' WHERE id = '"+ id +"'" ); stmt.close(); } catch (Exception sqlException) { System.out.println(sqlException.getMessage()); } } }