Search

Thứ Ba, 21 tháng 1, 2014

Delete record from table (Xóa dong từ bảng csdl trong java)



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Main {
  public static void main(String[] argsthrows Exception {
    String url = "jdbc:mysql://localhost/sampledb";
    String username = "root";
    String password = "";

    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection(url, username, password);

    String sql = "DELETE FROM users WHERE user_id = ?";
    int userId = 2;

    PreparedStatement statement = connection.prepareStatement(sql);

    statement.setInt(1, userId);

    int rows = statement.executeUpdate();

    System.out.println(rows + " record(s) deleted.");
    connection.close();
  }
}

0 nhận xét:

Đăng nhận xét

 
Le Van Chat