sergAccount

Untitled

Feb 13th, 2021
704
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.mycompany.app12;
  7.  
  8. import com.mycompany.app11.DataBaseUtil;
  9. import java.sql.Connection;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.time.LocalDateTime;
  14.  
  15. /**
  16.  *
  17.  * @author Admin
  18.  */
  19. public class AccountService {
  20.  
  21.     // метод возвращает значение типа Account
  22.     public Account getAccountByUserName(String userName) throws SQLException {
  23.         //
  24.         Account a = null;
  25.         final String sql = "select * from accounts where username = ?";
  26.         try (Connection c = DataBaseUtil.getConnection();
  27.                 PreparedStatement s = c.prepareStatement(sql)) {
  28.             s.setString(1, userName);
  29.             try (ResultSet rs = s.executeQuery()) {
  30.                 if (rs.next()) {
  31.                     String email = rs.getString("email");
  32.                     System.out.println("getAccountByUserName.email=" + email);
  33.                     // используем метод getObject для получения даты и времени из created_on
  34.                     LocalDateTime createdOn = rs.getObject("created_on", LocalDateTime.class);
  35.                     // st.setObject(1, createdOn);
  36.                     System.out.println("getAccountByUserName.createdOn=" + createdOn);
  37.                     a = new Account(userName);
  38.                     a.setCreatedOn(createdOn);
  39.                 }
  40.             }
  41.         } catch (SQLException ex) {
  42.             ex.printStackTrace();
  43.             throw ex;
  44.         }
  45.         return a;
  46.     }
  47. }
  48.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×