Guest User

Untitled

a guest
Jun 28th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. package com.app.dao;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. /**
  8. * Created by Andre on 21.06.2016.
  9. */
  10. public class DAOImpl implements DAO {
  11.  
  12. static {
  13. try {
  14. //DriverManager.registerDriver("org.postgresql.Driver");
  15. Class.forName("org.postgresql.Driver");
  16. } catch (ClassNotFoundException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20.  
  21. public void createEmployee(String name) {
  22. try (Connection connection = getConnection()) {
  23. PreparedStatement ps = connection.prepareStatement("INSERT INTO employee (id, name) values ((SELECT MAX(id)+1 FROM employee),?)");
  24. ps.setString(1, name);
  25. ps.execute();
  26. } catch (SQLException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30.  
  31. public Employee getEmployye(long id) {
  32. try (Connection connection = getConnection()) {
  33. PreparedStatement ps = connection.prepareStatement("SELECT * FROM employee WHERE id = ?");
  34. ps.setLong(1, id);
  35. ResultSet resultSet = ps.executeQuery();
  36. Employee employee = null;
  37. while (resultSet.next()) {
  38. String name = resultSet.getString("name");
  39. long id2 = resultSet.getLong("id");
  40. employee = new Employee(id2, name);
  41. }
  42. return employee;
  43.  
  44. } catch (SQLException e) {
  45. throw new RuntimeException(e);
  46. }
  47. }
  48.  
  49. public List<Employee> getEmployeeByName(String name) {
  50. List<Employee> result = new ArrayList<>();
  51. try (Connection connection = getConnection()) {
  52. PreparedStatement ps = connection.prepareStatement("SELECT * FROM employee WHERE name =?");
  53. ps.setString(1, name);
  54. ResultSet resultSet = ps.executeQuery();
  55. Employee employee = null;
  56. while (resultSet.next()) {
  57. employee = new Employee(resultSet.getLong("id"), resultSet.getString("name"));
  58. result.add(employee);
  59. }
  60. return result;
  61. } catch (SQLException e) {
  62. throw new RuntimeException(e);
  63. }
  64. }
  65.  
  66. @Override
  67. public void readMetadata() {
  68. try (Connection connection = getConnection()) {
  69. PreparedStatement ps = connection.prepareStatement("SELECT * FROM employee");
  70. ResultSet resultSet = ps.executeQuery();
  71. ResultSetMetaData metaData = resultSet.getMetaData();
  72. int n = metaData.getColumnCount();
  73. for (int i = 1; i<n+1; i++) {
  74. System.out.print(metaData.getColumnClassName(i));
  75. System.out.println("====" + metaData.getColumnName(i));;
  76. }
  77. } catch (SQLException e) {
  78. throw new RuntimeException(e);
  79. }
  80. }
  81.  
  82. @Override
  83. public void bulkAction() {
  84. try (Connection connection = getConnection()) {
  85. connection.setAutoCommit(false);
  86. PreparedStatement ps = connection.prepareStatement("INSERT INTO employee (id, name) values ((SELECT MAX(id)+1 FROM employee), 'Test')");
  87. ps.execute();
  88. ps = connection.prepareStatement("SELECT_ * FROM employee");
  89. ps.execute();
  90. connection.commit();
  91. } catch (SQLException e) {
  92. throw new RuntimeException(e);
  93.  
  94. }
  95. }
  96.  
  97. public void updateEmployee(long id, Employee employee) {
  98. List<Employee> result = new ArrayList<>();
  99. try (Connection connection = getConnection()) {
  100. PreparedStatement ps = connection.prepareStatement("UPDATE employee SET name=? WHERE id=?");
  101. ps.setString(1, employee.getName());
  102. ps.setLong(2, employee.getId());
  103. ps.execute();
  104. } catch (SQLException e) {
  105. throw new RuntimeException(e);
  106. }
  107. }
  108.  
  109.  
  110. public void deleteEmployee(long id) {
  111. List<Employee> result = new ArrayList<>();
  112. try (Connection connection = getConnection()) {
  113. PreparedStatement ps = connection.prepareStatement("DELETE FROM employee WHERE id = ?");
  114. ps.setLong(1, id);
  115. ps.execute();
  116. } catch (SQLException e) {
  117. throw new RuntimeException(e);
  118. }
  119. }
  120.  
  121. private Connection getConnection() {
  122. try {
  123. return DriverManager.getConnection("jdbc:postgresql://localhost:5432/Nakladnaya","user", "111");
  124. } catch (SQLException e) {
  125. throw new RuntimeException(e);
  126. }
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment