課題
問題: JBDCを使用しupdate文を実行せよ
使用ツール: XAMPP、eclipse
回答
package DBA;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class DBA3 {
public static void main(String[] args)throws SQLException {
System.out.println("社員ID入力");
Scanner s=new Scanner(System.in);
int ID1=s.nextInt();
System.out.println("部署ID(変更後)入力");
int ID2=s.nextInt();
String url="jdbc:mysql://localhost/JDBC";
String user ="root";
String password ="";
Connection con = DriverManager.getConnection(url,user,password);
PreparedStatement pstmt =con.prepareStatement
("update woker set co_id = ? where id = ?");
pstmt.setInt(1,ID2);
pstmt.setInt(2,ID1);
int rows= pstmt.executeUpdate();
System.out.println(rows+"件のデータを変更しました");
}
}
実行結果
社員ID入力
1
部署ID(変更後)入力
1
1件のデータを変更しました
本日の気付き:int rows= pstmt.executeUpdate();があることで変更件数がわかります