Advertisement
Latinist

Untitled

Apr 7th, 2020
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package com.antonromanov.jdbc;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         Connection conn = PostgreSQLConnUtils.getMySQLConnection();
  10.         long[] idArray = new long[3];
  11.         idArray[0] =  insertUser(new User("Василий", "vasiliy@yandex.ru"), conn);
  12.         idArray[1] =  insertUser(new User("Антон", "anton@yandex.ru"), conn);
  13.         idArray[2] =  insertUser(new User("Геннадий", "gen@yandex.ru"), conn);
  14.  
  15.         select(conn);
  16.  
  17.         for (int i = 0; i < idArray.length; i++) {
  18.             System.out.println("Удаляем елемент с Id = " + idArray[i]);
  19.             delete(conn, idArray[i]);
  20.         }
  21.     }
  22.  
  23.     public static long insertUser(User user, Connection connection) {
  24.         String SQL = "INSERT INTO test_jdbc.public.user (name, email) VALUES(?,?)";
  25.         long id = 0;
  26.         try {
  27.             PreparedStatement pstmt = connection.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS);
  28.             pstmt.setString(1, user.getName());
  29.             pstmt.setString(2, user.getEmail());
  30.  
  31.             int affectedRows = pstmt.executeUpdate();
  32.             if (affectedRows > 0) {
  33.                 try (ResultSet rs = pstmt.getGeneratedKeys()) {
  34.                     if (rs.next()) {
  35.                         id = rs.getLong(1);
  36.                     }
  37.                 } catch (SQLException ex) {
  38.                     System.out.println(ex.getMessage());
  39.                 }
  40.             }
  41.         } catch (SQLException e) {
  42.             e.printStackTrace();
  43.         }
  44.         return id;
  45.     }
  46.  
  47.     public static void select(Connection conn) {
  48.         List<User> result = new ArrayList<>();
  49.         String SQL_SELECT = "Select * from test_jdbc.public.user";
  50.  
  51.         try {
  52.             PreparedStatement preparedStatement = conn.prepareStatement(SQL_SELECT);
  53.             ResultSet resultSet = preparedStatement.executeQuery();
  54.  
  55.             while (resultSet.next()) {
  56.  
  57.                 long id = resultSet.getLong("user_id");
  58.                 String name = resultSet.getString("name");
  59.                 String email = resultSet.getString("email");
  60.  
  61.                 User obj = new User();
  62.                 obj.setId(id);
  63.                 obj.setName(name);
  64.                 obj.setEmail(email);
  65.                 result.add(obj);
  66.             }
  67.  
  68.             for (User user : result) {
  69.                 System.out.println("User {id: " + user.getId() + "," +
  70.                         " name: " + user.getName() +
  71.                         ", email: " + user.getEmail() + "}");
  72.             }
  73.  
  74.         } catch (SQLException e) {
  75.             System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
  76.         } catch (Exception e) {
  77.             e.printStackTrace();
  78.         }
  79.     }
  80.  
  81.     public static void delete(Connection conn, long id) {
  82.  
  83.         String SQL_SELECT = "delete from test_jdbc.public.user where user_id = ?";
  84.  
  85.         try {
  86.             PreparedStatement preparedStatement = conn.prepareStatement(SQL_SELECT);
  87.             preparedStatement.setLong(1, id);
  88.             preparedStatement.executeUpdate();
  89.         } catch (SQLException e) {
  90.             System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
  91.         } catch (Exception e) {
  92.             e.printStackTrace();
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement