javaからMysqlを操作するやり方について質問です。
現在jspの入力フォームに入力した情報を使ってMysqlへ登録、更新の処理を行うコードを作成中なのですが、登録の処理を行うコードは完成したのですが更新の処理の書き方がよくわかりません。
Mysqlに直接sql文を入力し更新することはできるのですがこれをjavaでやろうとするところで躓いてます。
登録(insert)の処理はjsp→servlet→Daoと値を受け渡して下記のように行っています。
public void InsertInformation(String name, String name_hiragana, String birthday, String sex, String mail_address, String telephone_number, String company_info_id, String business_maneger, String department, String commissioning_status) throws ClassNotFoundException, SQLException { // SQL文 String sql = "INSERT INTO employee_info (name, name_hiragana, birthday, sex, mail_address, telephone_number, company_info_id, business_maneger, department, commissioning_status) values(?, ?, cast(? as date), ?, ?, ?, ?, ?, ?, ?)"; try { Class.forName("com.mysql.jdbc.Driver"); // ドライバクラスのロード conn = DriverManager.getConnection(url, user, password); // Connectionクラス生成 pstmt = conn.prepareStatement(sql); // PreparedStatementクラス生成 pstmt.setString(1, name); pstmt.setString(2, name_hiragana); pstmt.setString(3, birthday); pstmt.setString(4, sex); pstmt.setString(5, mail_address); pstmt.setString(6, telephone_number); pstmt.setString(7, company_info_id); pstmt.setString(8, business_maneger); pstmt.setString(9, department); pstmt.setString(10, commissioning_status); pstmt.executeUpdate(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { } finally { // 終了処理 // PreparedStatement終了 if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { } pstmt = null; } // Connection終了 if (conn != null) { try { conn.close(); } catch (SQLException e) { } conn = null; } } }
更新(update)の際は、登録の際に自動採番で割り振られるidをwhere句に当てはめて行います。
上記のInsertの処理をUpdateに置き換えて書くとどのようになるでしょうか?
どうかご教授お願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。