Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package databasestorage;
- import database.connection.ConnectionFactory;
- import domain.Alat;
- import domain.Masina;
- import domain.TipMasine;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- /**
- *
- * @author Ivan
- */
- public class DatabaseStorageMasina {
- public List<Masina> getAll() throws SQLException{
- List<Masina> masine= new ArrayList<>();
- List<Alat> alati = new ArrayList<>();
- Connection connection = ConnectionFactory.getInstance().getConnection();
- String query = "Select * from "
- + "masine_alati ma"
- + " inner join"
- + " masina m on m.ID=ma.masinaid";
- String query1= "Select * from "
- + "masine_alati ma"
- + " inner join"
- + " alat a on a.ID=ma.alatid";
- Statement s = connection.createStatement();
- Statement s1 = connection.createStatement();
- ResultSet rs = s.executeQuery(query);
- while (rs.next()) {
- int masinaid=rs.getInt("masinaid");
- int godinaProizvodnje=rs.getInt("GodinaProizvodnje");
- String proizvodjac = rs.getString("proizvodjac");
- Date pocetakEksploatacije = new Date(rs.getDate("PocetakEksploatacije").getTime());
- String tip=rs.getString("Tip");
- String naziv = rs.getString("Naziv");
- int ocekivaniRadniVek = rs.getInt("OcekivaniRadniVek");
- Masina masina = new Masina(masinaid, naziv, proizvodjac, ocekivaniRadniVek, godinaProizvodnje, pocetakEksploatacije, TipMasine.trafo, new
- ArrayList<>());
- if (!masine.contains(masina)) {
- masine.add(masina);
- }
- }
- rs.close();
- ResultSet rs1 = s1.executeQuery(query1);
- while (rs1.next()) {
- int alatid=rs1.getInt("alatid");
- int stanje=rs1.getInt("stanje");
- String naziv=rs1.getString("naziv");
- Alat alat= new Alat(alatid, naziv, stanje, new ArrayList<>());
- for (Masina masina1 : masine) {
- if (rs1.getInt("masinaid")==masina1.getId()) {
- masina1.getAlati().add(alat);
- alat.getMasine().add(masina1);
- }
- }
- }
- rs1.close();
- return masine;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement