タイトルにあるように、がぞの手順で操作すると元のテーブルが表示されてしまいなんとか解決したい。
package DBaccess; import java.awt.Component; public class Swing extends JFrame implements TableModelListener { private JTextField textField; private JFrame frame; private JPanel contentPane; private DefaultTableModel tableModel; private JTable table; private final Action action = new SwingAction(); //SwingAction1・2・3と続く private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; private static final String STR_CONN = "jdbc:mysql://localhost/tbl" +"?user=root&password=kei19940927&useUnicode=true&characterEncoding=UTF-8&serverTimezone=JST"; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Swing frame = new Swing(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public Swing() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton btnNewButton = new JButton("New button"); btnNewButton.setAction(action); btnNewButton.setBounds(27, 10, 91, 40); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("New button"); btnNewButton_1.setAction(action_1); btnNewButton_1.setBounds(123, 10, 91, 40); contentPane.add(btnNewButton_1); JButton btnNewButton_2 = new JButton("New button"); btnNewButton_2.setAction(action_2); btnNewButton_2.setBounds(219, 10, 91, 40); contentPane.add(btnNewButton_2); JButton btnNewButton_3 = new JButton("New button"); btnNewButton_3.setAction(action_3); btnNewButton_3.setBounds(315, 10, 91, 40); contentPane.add(btnNewButton_3); JLabel lblNewLabel = new JLabel(); lblNewLabel.setBounds(29, 64, 50, 13); contentPane.add(lblNewLabel); textField = new JTextField(); textField.setBounds(27, 87, 379, 19); contentPane.add(textField); textField.setColumns(10); tableModel = new DefaultTableModel(); table=new JTable(tableModel); tableModel.addTableModelListener(this); contentPane.add(table); JLabel lblNewLabel_1 = new JLabel(); lblNewLabel_1.setBounds(27, 116, 50, 13); contentPane.add(lblNewLabel_1); JScrollPane scrollPane = new JScrollPane(table); scrollPane.setBounds(27, 135, 379, 116); contentPane.add(scrollPane); } public void tableChanged(TableModelEvent e) { int row = e.getLastRow(); int col = e.getColumn(); Object dl =table.getValueAt(row,col); tableModel.setValueAt(dl, row, col); String message = "行" + row + "列" + col + "が変更されました。"; JOptionPane.showMessageDialog(Swing.this, message); } private class SwingAction extends AbstractAction { public SwingAction() { putValue(NAME, "登録"); putValue(SHORT_DESCRIPTION, "Some short description"); } public void actionPerformed(ActionEvent e) { String text = textField.getText(); Connection con = null; Statement stmt = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); con=DriverManager.getConnection(STR_CONN); stmt=con.createStatement(); String sql= "insert into tbl_dbaccess(NAIYOU)values('"+text+"')"; stmt.executeUpdate(sql); } catch(Exception r){r.printStackTrace();} finally { if(stmt != null) { try { stmt.close(); }catch (Exception r) { System.out.println(r.getMessage()); } } if(con != null) try { con.close(); }catch (Exception r) { System.out.println(r.getMessage()); } } } } private class SwingAction_1 extends AbstractAction { public SwingAction_1() { putValue(NAME, "更新"); putValue(SHORT_DESCRIPTION, "Some short description"); } public void actionPerformed(ActionEvent e) { int row = table.getSelectedRow(); int col = table.getSelectedColumn(); Connection con = null; Statement stmt = null; ResultSet rs = null; try { Class.forName(JDBC_DRIVER); con=DriverManager.getConnection(STR_CONN); con.setAutoCommit(false); String Sql = "UPDATE tbl SET NAIYOU='"+table.getValueAt(0,col)+" WHERE PID='"+table.getValueAt(row, 0)+"'"; String sql2="select*from tbl"; stmt.executeUpdate(Sql); rs=stmt.executeQuery(sql2); table.setModel(new MyTableModel(rs)); } } catch(Exception r){r.printStackTrace();} finally { if(stmt != null) { try { stmt.close(); }catch (Exception r) { System.out.println(r.getMessage()); } } if(con != null) try { con.close(); }catch (Exception r) { System.out.println(r.getMessage()); } } } } private class SwingAction_2 extends AbstractAction { public SwingAction_2() { putValue(NAME, "削除"); putValue(SHORT_DESCRIPTION, "Some short description"); } public void actionPerformed(ActionEvent e) {int row = table.getSelectedRow(); int col = table.getSelectedColumn(); Object dl =table.getValueAt(row,col); System.out.println("行" + row + "::" + "列" + col); Connection con = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); con=DriverManager.getConnection(STR_CONN); stmt=con.createStatement(); String sql="delete from tbl_dbaccess where PID='"+dl+"'"; stmt.executeUpdate(sql);} catch(Exception r){r.printStackTrace();} finally { if(rs != null) { try { rs.close(); }catch (Exception r) { System.out.println(r.getMessage());}} if(stmt != null) { try { stmt.close(); }catch (Exception r) { System.out.println(r.getMessage());} } if(con != null) { try { con.close();} catch (Exception r) { System.out.println(r.getMessage());} } } } } private class SwingAction_3 extends AbstractAction { public SwingAction_3() { putValue(NAME, "表示"); putValue(SHORT_DESCRIPTION, "Some short description"); } public void actionPerformed(ActionEvent e) { Connection con = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); con=DriverManager.getConnection(STR_CONN); stmt=con.createStatement(); String sql="select PID, NAIYOU as 内容 from tbl_dbaccess"; rs=stmt.executeQuery(sql); table.setModel(new MyTableModel(rs)); } catch(Exception r){ r.printStackTrace();} finally { if(rs != null) { try { rs.close(); }catch (Exception r) { System.out.println(r.getMessage()); }} if(stmt != null) { try { stmt.close(); }catch (Exception r) { System.out.println(r.getMessage()); } } if(con != null) { try { con.close(); }catch (Exception r) { System.out.println(r.getMessage()); } } } } } class MyTableModel extends AbstractTableModel { private ArrayList<String> columnname; private ArrayList<ArrayList> data; public boolean isCellEditable(int rowIndex, int columnIndex) { return true; } public MyTableModel(ResultSet rs) { try { ResultSetMetaData rm=rs.getMetaData(); int cnum = rm.getColumnCount(); columnname = new ArrayList<String>(cnum); for(int i=1; i<=cnum; i++) { columnname.add(rm.getColumnLabel(i)); } data = new ArrayList(); while(rs.next()) { ArrayList<String>rowdata = new ArrayList<String>(); for(int i=1; i<=cnum; i++) { rowdata.add(rs.getObject(i).toString()); } data.add(rowdata); } } catch(Exception e) { e.printStackTrace(); } } public void setValueAt(Object val, int rowIndex, int columnIndex) { data.get(rowIndex).set(columnIndex, val); fireTableCellUpdated(rowIndex, columnIndex);} public String getColumnName(int column) { return (String)columnname.get(column); } public int getRowCount() { return data.size(); } public int getColumnCount() { return columnname.size(); } public Object getValueAt(int row, int column) { ArrayList rowdata = (ArrayList)data.get(row); return rowdata.get(column); } } class MyTableCellRenderer extends DefaultTableCellRenderer{ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component renderer = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if(value == null) { return renderer; } return renderer; } } }
回答2件
あなたの回答
tips
プレビュー