Guest User

Untitled

a guest
Nov 18th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. package test;
  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 DbOperations {
  9.  
  10. static void deleteClient(int userId) {
  11.  
  12. Connection conn = null;
  13. try {
  14. conn = DriverManager.getConnection(
  15. "jdbc:mysql://localhost:3306/products_ex?useSSL=false",
  16. "root",
  17. "coderslab"
  18. );
  19.  
  20. String sql = "DELETE FROM products WHERE id=?";
  21. PreparedStatement ps = conn.prepareStatement(sql);
  22.  
  23. ps.setInt(1, userId);
  24.  
  25. ps.executeUpdate();
  26.  
  27. conn.close();
  28.  
  29. } catch(SQLException e) {
  30. e.printStackTrace();
  31. } finally {
  32. if (conn != null) {
  33. try {
  34. conn.close();
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40. }
  41.  
  42. static void addClient(String name, String surname) {
  43.  
  44. Connection conn = null;
  45. try {
  46. conn = DriverManager.getConnection(
  47. "jdbc:mysql://localhost:3306/products_ex?useSSL=false",
  48. "root",
  49. "coderslab"
  50. );
  51.  
  52. String sql = "INSERT INTO clients(name, surname) VALUES(?, ?)";
  53. PreparedStatement ps = conn.prepareStatement(sql);
  54.  
  55. ps.setString(1, name);
  56. ps.setString(2, surname);
  57.  
  58. ps.executeUpdate();
  59.  
  60. conn.close();
  61.  
  62. } catch(SQLException e) {
  63. e.printStackTrace();
  64. } finally {
  65. if (conn != null) {
  66. try {
  67. conn.close();
  68. } catch (SQLException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. }
  73. }
  74.  
  75. }
Add Comment
Please, Sign In to add comment