JAVA与SQL的结合方法二

课题

问题:请在JAVA中执行delete语句
使用软件:XAMPP、eclipse

Paraphrased in Chinese:
问题:请用JAVA执行删除语句
使用软件: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。
今天的学习:可以轻松地通过标准输入实现,取决于如何进行连接。