Advertisement
Guest User

klk2018G1 - Izvlacenje masine

a guest
Nov 21st, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  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 databasestorage;
  7.  
  8. import database.connection.ConnectionFactory;
  9. import domain.Alat;
  10. import domain.Masina;
  11. import domain.TipMasine;
  12. import java.sql.Connection;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16. import java.util.ArrayList;
  17. import java.util.Date;
  18. import java.util.List;
  19.  
  20. /**
  21.  *
  22.  * @author Ivan
  23.  */
  24. public class DatabaseStorageMasina {
  25.    
  26.    
  27.     public List<Masina> getAll() throws SQLException{
  28.     List<Masina> masine= new ArrayList<>();
  29.     List<Alat> alati = new ArrayList<>();
  30.    
  31.     Connection connection = ConnectionFactory.getInstance().getConnection();
  32.     String query = "Select * from "
  33.             + "masine_alati ma"
  34.             + " inner join"
  35.             + " masina m on m.ID=ma.masinaid";
  36.     String query1= "Select * from "
  37.             + "masine_alati ma"
  38.             + " inner join"
  39.             + " alat a on a.ID=ma.alatid";
  40.         Statement s = connection.createStatement();
  41.         Statement s1 = connection.createStatement();
  42.         ResultSet rs = s.executeQuery(query);
  43.        
  44.         while (rs.next()) {            
  45.             int masinaid=rs.getInt("masinaid");
  46.             int godinaProizvodnje=rs.getInt("GodinaProizvodnje");
  47.             String proizvodjac = rs.getString("proizvodjac");
  48.             Date pocetakEksploatacije = new Date(rs.getDate("PocetakEksploatacije").getTime());
  49.             String tip=rs.getString("Tip");
  50.             String naziv = rs.getString("Naziv");
  51.             int ocekivaniRadniVek = rs.getInt("OcekivaniRadniVek");
  52.            
  53.             Masina masina = new Masina(masinaid, naziv, proizvodjac, ocekivaniRadniVek, godinaProizvodnje, pocetakEksploatacije, TipMasine.trafo, new
  54.                 ArrayList<>());
  55.             if (!masine.contains(masina)) {
  56.                 masine.add(masina);
  57.             }
  58.            
  59.         }
  60.         rs.close();
  61.         ResultSet rs1 = s1.executeQuery(query1);
  62.         while (rs1.next()) {
  63.             int alatid=rs1.getInt("alatid");
  64.             int stanje=rs1.getInt("stanje");
  65.             String naziv=rs1.getString("naziv");
  66.             Alat alat= new Alat(alatid, naziv, stanje, new ArrayList<>());
  67.            
  68.             for (Masina masina1 : masine) {
  69.                 if (rs1.getInt("masinaid")==masina1.getId()) {
  70.                     masina1.getAlati().add(alat);
  71.                     alat.getMasine().add(masina1);
  72.                 }
  73.             }
  74.         }
  75.         rs1.close();
  76.        
  77.     return masine;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement