質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

Q&A

解決済

2回答

1679閲覧

Jtableのデータを変更後に表示を再度実行すると過去のテーブルが表示されてしまう

退会済みユーザー

退会済みユーザー

総合スコア0

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

0グッド

0クリップ

投稿2020/03/31 02:55

編集2020/03/31 02:58

タイトルにあるように、がぞの手順で操作すると元のテーブルが表示されてしまいなんとか解決したい。

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; } } }

1
イメージ説明
2
イメージ説明
3
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2020/03/31 04:30

手順が全く分かりませんが... どこに入力してどこを押してどうなっているのでしょうか.
jimbe

2020/03/31 04:43

単に省略されているためにコンパイル出来ないだけでなく, 全角スペースや括弧の不備等があります. コンパイル・実行しているコードをコピペしてご提示ください.
guest

回答2

0

java

1 Statement stmt = null; 2 ResultSet rs = null; 3 try { 4 Class.forName(JDBC_DRIVER); 5 con=DriverManager.getConnection(STR_CONN); 6 con.setAutoCommit(false); 7 String Sql = "UPDATE tbl SET NAIYOU='"+table.getValueAt(0,col)+" WHERE PID='"+table.getValueAt(row, 0)+"'"; 8 String sql2="select*from tbl"; 9 stmt.executeUpdate(Sql);

少なくとも stmt が null の為 update が実行できていません.
例外の時の処理がテキトウな為, 処理が続行されてしまっています.

投稿2020/03/31 04:48

jimbe

総合スコア12646

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/03/31 05:26

ありがとうございます! 承知いたしました!
guest

0

ベストアンサー

https://java.keicode.com/lib/swing-jtable-6.php

上記のサイトを参考に解決いたしました。

投稿2020/03/31 04:40

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問