Guest User

Untitled

a guest
Nov 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 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 addClient(String name, String surname) {
  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 = "INSERT INTO clients(name, surname) VALUES(?, ?)";
  21. PreparedStatement ps = conn.prepareStatement(sql);
  22.  
  23. ps.setString(1, name);
  24. ps.setString(2, surname);
  25.  
  26. ps.executeUpdate();
  27.  
  28. conn.close();
  29.  
  30. } catch(SQLException e) {
  31. e.printStackTrace();
  32. } finally {
  33. if (conn != null) {
  34. try {
  35. conn.close();
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41. }
  42.  
  43. }
Add Comment
Please, Sign In to add comment