課題
問題:JAVAでdelete文を実行せよ
使用ソフト: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 DBA2 {
public static void main(String[] args) throws SQLException {
System.out.println("会社ID入力");
Scanner s=new Scanner(System.in);
int ID1=s.nextInt();
String url="jdbc:mysql://localhost/JDBC";
String user ="root";
String password ="";
Connection con = DriverManager.getConnection(url,user,password);
PreparedStatement pstmt =con.prepareStatement
("delete from worker where co_id=?");
pstmt.setInt(1,ID1);
int rows =pstmt.executeUpdate();
if(rows>0) {
System.out.println("削除成功");
}else {
System.out.println("削除失敗");
}
}
}
本日の気付き:失敗した場合0になるため、IF文の要件は0以上にします。
本日の学び:標準入力からも問題なく可能、結びつけ方次第。