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

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

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

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

Java

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

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

Q&A

0回答

332閲覧

セルに指定された文字列が存在するときのみ更新を行う

sait_you

総合スコア6

MySQL

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

Java

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

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

0グッド

0クリップ

投稿2020/05/11 05:27

前提・実現したいこと

ステータスというカラムが存在し、そのセルの初期値は「-」となっている。
セルは「貸出中」や「返却」へと更新をすることが可能
そこで、複数のデータの中で選択したデータのみ「貸出中」に変更し、更新をかける際に
セルが「-」の状態のままや「返却」が選択されている場合に更新されないようにしたいです。

発生している問題・エラーメッセージ

更新までのコードを組み立てられ、if文にてコードを組み立てようと思ったが
思ったような結果を返すことができない

該当のソースコード

Java

1package Book; 2 3import java.awt.EventQueue; 4import java.awt.event.ActionEvent; 5import java.awt.event.ActionListener; 6import java.sql.Connection; 7import java.sql.PreparedStatement; 8import java.sql.ResultSet; 9import java.sql.SQLException; 10import java.util.ArrayList; 11import java.util.Iterator; 12import java.util.Random; 13 14import javax.swing.DefaultCellEditor; 15import javax.swing.JButton; 16import javax.swing.JComboBox; 17import javax.swing.JFrame; 18import javax.swing.JLabel; 19import javax.swing.JOptionPane; 20import javax.swing.JScrollPane; 21import javax.swing.JTable; 22import javax.swing.JTextField; 23import javax.swing.ListSelectionModel; 24import javax.swing.table.DefaultTableModel; 25import javax.swing.table.TableColumn; 26import javax.swing.table.TableModel; 27 28public class LendingandReserch { 29 30 protected static final Object Boolean = null; 31 32 private JFrame frame; 33 34 private JTable table; 35 private String[] columnNames = { "ISBN","タイトル", "作者", "出版社", "出版年", "ステータス" }; 36 private DefaultTableModel tableModel; 37 private JScrollPane scrollPane = null; 38 39 40 /** 41 * Launch the application. 42 */ 43 public static void main(String[] args) { 44 EventQueue.invokeLater(new Runnable() { 45 @Override 46 public void run() { 47 try { 48 LendingandReserch window = new LendingandReserch(); 49 window.frame.setVisible(true); 50 } catch (Exception e) { 51 e.printStackTrace(); 52 } 53 } 54 }); 55 } 56 57 /** 58 * Create the application. 59 */ 60 public LendingandReserch() { 61 initialize(); 62 } 63 64 /** 65 * Initialize the contents of the frame. 66 */ 67 private void initialize() { 68 69 scrollPane = new JScrollPane(); 70 scrollPane.setBounds(12, 149, 964, 271); 71 frame.getContentPane().add(scrollPane); 72 73 Lent lent = new Lent(); 74 75 table = new JTable(); 76 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 77 table.setColumnSelectionAllowed(true); 78 table.setRowSelectionAllowed(true); 79 scrollPane.setViewportView(table); 80 81 /** 82 * ボタンの名称 83 */ 84 JButton btnLent = new JButton("レンタル"); 85 JButton btnFullOpen = new JButton("全表示"); 86 /** 87 * ボタンの位置 88 */ 89 btnLent.setBounds(1004, 80, 125, 60); 90 btnFullOpen.setBounds(1004, 290, 125, 60); 91 92 //レンタルボタンのアクション 93 btnLent.addActionListener(new ActionListener() { 94 95 @Override 96 public void actionPerformed(ActionEvent e) { 97 // TODO 自動生成されたメソッド・スタブ 98 99 int row = table.getSelectedRow(); 100 int col = table.getSelectedColumn(); 101 if(row == -1 || col == -1) { 102 JOptionPane.showMessageDialog(frame, "データが選択されていません"); 103 return; 104 }else { 105 Connection con = null; 106 PreparedStatement pstmt = null; 107 108 try { 109 //データベースに接続 110 con = DBconect.getConnection(); 111 112 //オートコミット解除 113 con.setAutoCommit(false); 114 //SQL文作成 115 String mySql = "update book.tbl_book SET title = ?, author = ?, publisher = ?, releaseday = ?, status = ? where ISBN = ?"; 116 117 for(int i = 0; i < table.getRowCount(); i++) { 118 //BookNoをString型で返す 119 String isbn = String.valueOf(table.getValueAt(i, 0)); 120 //内容をString型で返す 121 String title = String.valueOf(table.getValueAt(i, 1)); 122 String author = String.valueOf(table.getValueAt(i, 2)); 123 String publisher = String.valueOf(table.getValueAt(i, 3)); 124 String releaseday = String.valueOf(table.getValueAt(i, 4)); 125 String status = String.valueOf(table.getValueAt(i, 5)); 126 System.out.println("id:" + isbn + "\t"); 127 System.out.println("naiyou:" + status + "\t"); 128 129 130 131 //更新データ存在チェック 132 int errorCode = Validate.ckExistsUpData(con, isbn); 133 if(errorCode != Validate.getErrCode0()) { 134 //エラーダイアログ表示 135 JOptionPane.showMessageDialog(frame, Validate.getErrMsg(errorCode)); 136 return; 137 } 138 //ステートメントオブジェクトを作成 139 pstmt = con.prepareStatement(mySql); 140 //条件値セット 141 pstmt.setString(1, title); 142 pstmt.setString(2, author); 143 pstmt.setString(3, publisher); 144 pstmt.setString(4, releaseday); 145 pstmt.setString(5, status); 146 //Stringをintに変換 147 pstmt.setString(6, isbn); 148 149 //SQLを実行 150 int num = pstmt.executeUpdate(); 151 System.out.println("結果:" + num + "\t"); 152 } 153 154 //コミット 155 con.commit(); 156 } 157 catch(Exception ex) { 158 try { 159 //ロールバック 160 if(con != null) { 161 con.rollback(); 162 } 163 }catch(SQLException se) { 164 //何もしない 165 } 166 } 167 //表示ボタン押下 168 btnFullOpen.doClick(); 169 } 170 } 171 172 }); 173 frame.getContentPane().setLayout(null); 174 frame.getContentPane().add(btnLent); 175 176 //全表示ボタンのアクション 177 btnFullOpen.addActionListener(new ActionListener() { 178 179 @Override 180 public void actionPerformed(ActionEvent e) { 181 // TODO 自動生成されたメソッド・スタブ 182 Connection con = null; 183 PreparedStatement pstmt = null; 184 ResultSet rs = null; 185 186 try { 187 //データベースに接続 188 con = DBconect.getConnection(); 189 190 //表示データ存在チェック 191 int errorCode = Validate.ckExistsData1(con); 192 if(errorCode != Validate.getErrCode0()) { 193 //エラーダイアログ表示 194 JOptionPane.showMessageDialog(frame, Validate.getErrMsg(errorCode)); 195 return; 196 } 197 198 199 //SQL文作成 200 String mySql = "select ISBN, title, author, publisher, releaseday, status from book.tbl_book order by ISBN"; 201 //ステートメントオブジェクトを作成 202 pstmt = con.prepareStatement(mySql); 203 //検索するSQL実行 204 rs = pstmt.executeQuery(); 205 206 // 表のヘッダー部作成 207 tableModel = new DefaultTableModel(columnNames, 0); 208 // java.sql.ResultSet の行数を取得するためカーソルを最終行に移動 209 rs.last(); 210 // 結果の行数をセット 211 tableModel.setRowCount(rs.getRow()); 212 // カーソルを先頭行に移動 213 rs.beforeFirst(); 214 System.out.println("表件数=" + tableModel.getRowCount() + "\t"); 215 216 int i = 0; // カウントアップ変数 217 //結果セットからデータを取り出す next()で次の行に移動 218 while(rs.next()) { 219 // 検索結果から表に書き込み 220 tableModel.setValueAt(rs.getString("ISBN"), i, 0); 221 tableModel.setValueAt(rs.getString("title"), i, 1); 222 tableModel.setValueAt(rs.getString("author"), i, 2); 223 tableModel.setValueAt(rs.getString("publisher"), i, 3); 224 tableModel.setValueAt(rs.getString("releaseday"), i, 4); 225 tableModel.setValueAt(rs.getString("status"), i, 5); 226 System.out.println("行数=" + i + "\t"); 227 i++; 228 } 229 // 表を描画 230 frame.getContentPane().add(scrollPane); 231 table.setModel(tableModel); 232 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 233 table.setColumnSelectionAllowed(false); 234 table.setRowSelectionAllowed(true); 235 table.getColumnModel().getColumn(0).setMinWidth(150); 236 table.getColumnModel().getColumn(1).setPreferredWidth(150); 237 table.getColumnModel().getColumn(1).setMinWidth(1); 238 scrollPane.setViewportView(table); 239 scrollPane.repaint(); 240 241 //コンボボックスの設定 242 String[] dataItem = {"-", "貸出中", "返却"}; 243 JComboBox<Object> statsComboBox = new JComboBox<Object>(dataItem); 244 TableColumn sutatsColumn = table.getColumnModel().getColumn(5); 245 sutatsColumn.setCellEditor(new DefaultCellEditor(statsComboBox)); 246 247 } catch (Exception ex) { 248 System.out.println("例外発生:" + ex.toString()); 249 JOptionPane.showMessageDialog(frame, "例外発生:" + ex.toString()); 250 } finally { 251 try { 252 // 実行結果をクローズ 253 if(rs != null) { 254 rs.close(); 255 } 256 // ステートメントをクローズ 257 if(pstmt != null) { 258 pstmt.close(); 259 } 260 // 接続をクローズ 261 if(con != null) { 262 con.close(); 263 } 264 } catch (SQLException se) { 265 // 何もしない 266 } 267 } 268 269 270 } 271 272 }); 273 frame.getContentPane().setLayout(null); 274 frame.getContentPane().add(btnFullOpen); 275 276

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問