Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. package mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7.  
  8. public class DeleteMain {
  9.  
  10. private static final String DELETE_PRODUCT_QUERY =
  11. "DELETE FROM products WHERE name LIKE ?";
  12.  
  13. public static void main(String[] args){
  14. String url = "jdbc:mysql://localhost:3306/products_ex";
  15. try(Connection connection = getConnection(url);
  16. PreparedStatement preparedStatement =
  17. connection.prepareStatement(DELETE_PRODUCT_QUERY);
  18. ){
  19.  
  20. preparedStatement.setString(1, "%1%");
  21.  
  22. int updateRecords = preparedStatement.executeUpdate();
  23. System.out.println("Deleted records count = " + updateRecords);
  24.  
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29.  
  30. private static Connection getConnection(String url) throws SQLException {
  31. return DriverManager.getConnection(url,"root", "root");
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement