public class Tables { public static final Class<?>[] RANKS_TYPE = { Integer.class, String.class }; public static final String[] RANKS_TABLE = { "ID", "Rank" }; }
public class AbsTable extends AbstractTableModel { private List<String> mColumnNames; private List<ArrayList<Object>> mTableData; private List<Object> mColumnTypes; public AbsTable(Class<?>[] types, String[] columns) { mColumnTypes = new ArrayList<Object>(types.length); mColumnNames = new ArrayList<String>(columns.length); for (int i = 0; i < columns.length; ++i) { mColumnTypes.add(i, types[i]); mColumnNames.add(columns[i]); } } @Override public int getColumnCount() { return mColumnNames.size(); } @Override public int getRowCount() { return mTableData.size(); } @Override public Object getValueAt(int row, int column) { return mTableData.get(row).get(column); } public String getColumnName(int column) { return mColumnNames.get(column); } @Override public boolean isCellEditable(int row, int column) { return false; } @Override public void setValueAt(Object obj, int row, int column) { } @Override public Class<?> getColumnClass(int col) { return (Class<?>) mColumnTypes.get(col); } public void setTableData(ArrayList<ArrayList<Object>> tableData) { mTableData = tableData; } }
public class DBHelper { private Connection dbConnection; private static DBHelper sDBHelper ; private static final String DRIVER = "org.firebirdsql.jdbc.FBDriver"; private static final String URL = "jdbc:firebirdsql:localhost/3050:C:\\DB\\DB.FDB"; private static final String LOGIN = "SYSDBA"; private static final String PASSWORD = "masterkey"; public static synchronized DBHelper getInstance() { if (sDBHelper == null) { sDBHelper = new DBHelper (); } return sDBHelper ; } private DBHelper () { } public void connect() { try { Class.forName(DRIVER); dbConnection = DriverManager.getConnection(URL, LOGIN, PASSWORD); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public PreparedStatement getPrepareStatement(String sql) throws SQLException { return dbConnection.prepareStatement(sql); } public synchronized ArrayList<ArrayList<Object>> getData(String query) { ArrayList<ArrayList<Object>> dataVector = new ArrayList<ArrayList<Object>>(); Statement st = null; ResultSet rs = null; try { st = dbConnection.createStatement(); rs = st.executeQuery(query); int columns = rs.getMetaData().getColumnCount(); while (rs.next()) { ArrayList<Object> nextRow = new ArrayList<Object>(columns); for (int i = 1; i <= columns; i++) { nextRow.add(rs.getObject(i)); } dataVector.add(nextRow); } } catch (SQLException e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } } return dataVector; } public void release() { if (sDBHelper != null) { sDBHelper = null; } if (dbConnection != null) { try { dbConnection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
public class Components { public static AbsTable createTableModel(Class<?>[] types, String[] col, String sql) { AbsTable table = new AbsTable(types, col); table.setTableData(DBHelper .getInstance().getData(sql)); return table; } public static JTable createTable(AbsTable model) { JTable table = new JTable(model); table.getColumnModel().getColumn(0).setMaxWidth(50); return table; } public static JScrollPane createScroll(JTable table) { return new JScrollPane(table); } public static JComboBox<String> createCombo(String[] items, ItemListener listener) { JComboBox<String> combo = new JComboBox<String>(items); combo.setEditable(false); combo.setSelectedIndex(-1); combo.addItemListener(listener); return combo; } public static JLabel createLabel(String name) { JLabel label = new JLabel(name); label.setHorizontalTextPosition(JLabel.LEFT); label.setIconTextGap(5); label.setForeground(Color.black); return label; } public static JTextField createEdit(String text) { JTextField tf= new JTextField(text); tf.setEditable(true); tf.setForeground(Color.black); return tf; } public static JButton createButton(String name, ActionListener listener) { JButton button = new JButton(name); button.addActionListener(listener); return button; } }
abstract class BaseFrame extends JFrame implements ListSelectionListener { protected JButton mDeleteBtn; protected JButton mAddBtn; protected JButton mSaveBtn; protected JPanel mControlArea; protected JPanel mEditArea; protected JScrollPane mScroll; protected JTable mTable; protected Container mContainer; protected AbsTable mTableModel; protected DBHelper sDBHelper ; private static final int SIZE_X = 300; private static final int SIZE_Y = 450; public BaseFrame(String name) { super(name); sDBHelper = DBHelper .getInstance(); sBDHelper.connect(); mAddBtn = Components.createButton(Strings.ADD, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { add(); } }); mDeleteBtn = Components.createButton(Strings.DELETE, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { delete(); } }); mSaveBtn = Components.createButton(Strings.SAVE, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { save(); } }); setSize(new Dimension(SIZE_X, SIZE_Y)); setVisible(true); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } abstract void updateTable(); abstract void add(); abstract void delete(); abstract void save(); }
public class RanksFrame extends BaseFrame { private JLabel mIdLabel; private JLabel mRankLabel; private JTextField mIdEdit; private JTextField mRankEdit; public RanksFrame() { super(Strings.RANK); mContainer = getContentPane(); mTableModel = Components.createTableModel(Tables.RANKS_TYPE, Tables.RANKS_TABLE, "SELECT * FROM RANKS ORDER BY ID"); mTable = Components.createTable(mTableModel); mScroll = Components.createScroll(mTable); mIdLabel = Components.createLabel(Strings.ID); mIdEdit = Components.createEdit(""); mRankLabel = Components.createLabel(Strings.RANK); mRankEdit = Components.createEdit(""); mTable.getSelectionModel().addListSelectionListener(this); mControlArea = new JPanel(new GridLayout(1, 3)); mEditArea = new JPanel(new GridLayout(2, 2)); mEditArea.add(mIdLabel); mEditArea.add(mIdEdit); mEditArea.add(mRankLabel); mEditArea.add(mRankEdit); mControlArea.add(mSaveBtn); mControlArea.add(mDeleteBtn); mControlArea.add(mAddBtn); mContainer.add(mScroll); mContainer.add(mEditArea); mContainer.add(mControlArea); mContainer.setLayout(new BoxLayout(mContainer, BoxLayout.Y_AXIS)); } @Override public void updateTable() { SwingUtilities.invokeLater(new Runnable() { public void run() { mTableModel.setTableData(sBDHelper .getData("SELECT * FROM RANKS ORDER BY ID")); mTable.updateUI(); mRankEdit.setText(null); mIdEdit.setText(null); } }); } @Override public void add() { PreparedStatement ps = null; try { ps = sBDHelper .getPrepareStatement("INSERT INTO RANKS (ID,RANK) VALUES(?,?)"); ps.setString(1, mIdEdit.getText()); ps.setString(2, mRankEdit.getText()); ps.executeUpdate(); } catch (SQLException r) { r.printStackTrace(); } finally { if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } updateTable(); } } @Override public void delete() { PreparedStatement ps = null; try { ps = sBDHelper.getPrepareStatement("DELETE FROM RANKS WHERE ID=?"); ps.setString(1, mIdEdit.getText()); ps.executeUpdate(); } catch (SQLException r) { r.printStackTrace(); } finally { if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } updateTable(); } } @Override public void save() { PreparedStatement ps = null; try { ps = sBDHelper .getPrepareStatement("UPDATE RANKS SET RANK=? WHERE ID=?"); ps.setString(1, mRankEdit.getText()); ps.setString(2, mIdEdit.getText()); ps.executeUpdate(); } catch (SQLException r) { r.printStackTrace(); } finally { if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } updateTable(); } } @Override public void valueChanged(ListSelectionEvent e) { mIdEdit.setText(mTable.getModel() .getValueAt(mTable.getSelectedRow(), 0).toString()); mRankEdit.setText(mTable.getModel() .getValueAt(mTable.getSelectedRow(), 1).toString()); } }
public class Application extends JFrame implements WindowListener { private String[] mTables = { "Ranks" }; private static final int RANKS = 0; private JComboBox<String> mComboMenu; public Application() throws SQLException { super(Strings.DB_NAME); mComboMenu = Components.createCombo(mTables, new ItemListener() { @Override public void itemStateChanged(ItemEvent evt) { switch (mComboMenu.getSelectedIndex()) { case RANKS: new RanksFrame(); break; } SwingUtilities.invokeLater(new Runnable() { public void run() { mComboMenu.setSelectedIndex(-1); } }); } }); Container container = getContentPane(); container.add(mComboMenu); container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 80); setResizable(false); setVisible(true); addWindowListener(this); } public static void main(String[] args) throws SQLException { new Application(); } @Override public void windowClosing(WindowEvent arg0) { DBHelper .getInstance().release(); } }
Source: https://habr.com/ru/post/145531/
All Articles