Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 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 UpdateMain {
  9.  
  10. private static final String UPDATE_PRODUCT_QUERY =
  11. "UPDATE products SET description = ? 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(UPDATE_PRODUCT_QUERY);
  18. ){
  19.  
  20. preparedStatement.setString(1, "new descript");
  21. preparedStatement.setString(2, "A%");
  22.  
  23. int updateRecords = preparedStatement.executeUpdate();
  24. System.out.println("Updated record count = " + updateRecords);
  25.  
  26. } catch (SQLException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30.  
  31. private static Connection getConnection(String url) throws SQLException {
  32. return DriverManager.getConnection(url,"root", "root");
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement