Advertisement
Latinist

Untitled

Apr 8th, 2020
1,521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 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.                 ResultSet rs = pstmt.getGeneratedKeys()
  34.                     if (rs.next()) {
  35.                         id = rs.getLong(1);
  36.                     }
  37.             }
  38.         } catch (SQLException e) {
  39.             System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
  40.         }
  41.         return id;
  42.     }
  43.  
  44.     public static void select(Connection conn) {
  45.         List<User> result = new ArrayList<>();
  46.         String SQL_SELECT = "Select * from test_jdbc.public.user";
  47.  
  48.         try {
  49.             PreparedStatement preparedStatement = conn.prepareStatement(SQL_SELECT);
  50.             ResultSet resultSet = preparedStatement.executeQuery();
  51.  
  52.             while (resultSet.next()) {
  53.  
  54.                 long id = resultSet.getLong("user_id");
  55.                 String name = resultSet.getString("name");
  56.                 String email = resultSet.getString("email");
  57.  
  58.                 User obj = new User();
  59.                 obj.setId(id);
  60.                 obj.setName(name);
  61.                 obj.setEmail(email);
  62.                 result.add(obj);
  63.             }
  64.  
  65.             for (User user : result) {
  66.                 System.out.println("User {id: " + user.getId() + "," +
  67.                         " name: " + user.getName() +
  68.                         ", email: " + user.getEmail() + "}");
  69.             }
  70.  
  71.         } catch (SQLException e) {
  72.             System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
  73.         } catch (Exception e) {
  74.             e.printStackTrace();
  75.         }
  76.     }
  77.  
  78.     public static void delete(Connection conn, long id) {
  79.  
  80.         String SQL_SELECT = "delete from test_jdbc.public.user where user_id = ?";
  81.  
  82.         try {
  83.             PreparedStatement preparedStatement = conn.prepareStatement(SQL_SELECT);
  84.             preparedStatement.setLong(1, id);
  85.             preparedStatement.executeUpdate();
  86.         } catch (SQLException e) {
  87.             System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
  88.         } catch (Exception e) {
  89.             e.printStackTrace();
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement