/********************************/ /* レコードの削除とデータの修正 */ /* coded by Y.Suganuma */ /********************************/ import java.io.*; import java.sql.*; public class Test { public static void main(String args[]) { int i1; boolean s; ResultSet result; Connection Con; // JDBCドライバの登録 try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception e1) { System.out.println("Driver Error: " + e1.toString()); } try { // データベースへの接続 Con = DriverManager.getConnection("jdbc:mysql://cs-www/base?" + "useUnicode=true&characterEncoding=sjis", "xxxxx", "*****"); try { // SQL の実行 Statement SQL = Con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); // テーブルから1レコードを削除(DELETEコマンド) SQL.execute("DELETE FROM gakuseki WHERE No=222222222"); // テーブルから1レコードを削除(Javaの関数) SQL.execute("SELECT * FROM gakuseki WHERE No=333333333"); result = SQL.getResultSet(); result.next(); result.deleteRow(); SQL.execute("SELECT * FROM gakuseki"); result = SQL.getResultSet(); System.out.println("No, name, math"); while (result.next()) { System.out.print(" "); for (i1 = 1; i1 <= 3; i1++) { if (i1 < 3) System.out.print(result.getString(i1) + ", "); else System.out.print(result.getString(i1)); } System.out.println(); } // レコード内のデータの修正(UPDATEコマンド) SQL.execute("UPDATE gakuseki SET math=100 WHERE No=111111111"); // レコード内のデータの修正(JAVAの関数) SQL.execute("SELECT * FROM gakuseki"); result = SQL.getResultSet(); result.absolute(3); result.updateInt("math", 90); result.updateRow(); // レコードの挿入(JAVAの関数) SQL.execute("SELECT * FROM gakuseki"); result = SQL.getResultSet(); result.absolute(2); result.moveToInsertRow(); // moves cursor to the insert row result.updateInt(1, 999999999); // updates the first column result.updateString(2, "小川"); // updates the second column result.updateInt(3, 88); // updates the third column result.insertRow(); result.moveToCurrentRow(); SQL.execute("SELECT * FROM gakuseki"); result = SQL.getResultSet(); System.out.println("No, name, math"); while (result.next()) { System.out.print(" "); for (i1 = 1; i1 <= 3; i1++) { if (i1 < 3) System.out.print(result.getString(i1) + ", "); else System.out.print(result.getString(i1)); } System.out.println(); } // SQLを閉じる SQL.close(); // 接続を閉じる Con.close(); } catch (SQLException e2) { System.out.println("SQL Error: " + e2.toString()); } } catch (SQLException e3) { System.out.println("SQLException: " + e3.getMessage()); System.out.println("SQLState: " + e3.getSQLState()); System.out.println("VendorError: " + e3.getErrorCode()); } } }