Hakunin

OOP_RadSaBazama

May 13th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. //////////////////BAZA PODATAKA BazaPodataka.java
  2.  
  3. /**
  4.  * Created by markort2815 on 13.5.2017..
  5.  */
  6.     import java.sql.*;
  7.     public class BazaPodataka {
  8.     private Connection conn;
  9.     private static BazaPodataka instanca;
  10.  
  11.     private BazaPodataka() {
  12.         try {
  13.             Class.forName("org.sqlite.JDBC");
  14.             conn = DriverManager.getConnection("jdbc:sqlite:knjige.db");
  15.         } catch ( Exception e ) {
  16.             System.err.println("Doslo je do greske pri konekciji na bazu podataka" + e.getClass().getName() + ": " + e.getMessage() );
  17.             System.exit(0);
  18.         }
  19.     }
  20.  
  21.     public static BazaPodataka getInstanca() {  //singluton design pattern
  22.         if(instanca == null)
  23.             instanca = new BazaPodataka();
  24.         return instanca;
  25.     }
  26.  
  27.     public void automatskaTransakcija(boolean on_off) throws SQLException { conn.setAutoCommit(on_off); }
  28.     public void snimiTransakciju() throws SQLException { conn.commit(); }
  29.  
  30.  
  31.     //metoda za upite koji su tipa INSERT, UPDATE, DELETE
  32.     public int iudQuery(String sql) throws SQLException {
  33.         System.out.println(sql);
  34.         Statement statement = conn.createStatement();
  35.         return statement.executeUpdate(sql);
  36.     }
  37.  
  38.  
  39.     public ResultSet select(String sql) throws SQLException {
  40.         System.out.println(sql);
  41.         Statement statement= conn.createStatement();
  42.         return statement.executeQuery(sql);
  43.     }
  44. }
  45.  
  46. ///////////////////////////////////// Autor.java
  47.  
  48. /**
  49.  * Created by markort2815 on 13.5.2017..
  50.  */
  51.  
  52. import java.sql.ResultSet;
  53. import java.sql.SQLException;
  54. import java.util.ArrayList;
  55.  
  56.     public class Autor {
  57.         private int autorId;
  58.         private String autor;
  59.         private int godinaRodjenja;
  60.         private int godinaSmrti;
  61.  
  62.         public Autor(int autorId, String autor, int godinaRodjenja, int godinaSmrti) {
  63.             this.autorId = autorId;
  64.             this.autor = autor;
  65.             this.godinaRodjenja = godinaRodjenja;
  66.             this.godinaSmrti = godinaSmrti;
  67.         }
  68.  
  69.         public static Autor dohvatiZaId(int id) {
  70.             String upit = "SELECT * FROM autori WHERE autorId = " + id;
  71.             Autor autor = null;
  72.             try {
  73.                 ResultSet odgovorBaze = BazaPodataka.getInstanca().select(upit);
  74.                 int autorId = odgovorBaze.getInt("autorId");
  75.                 String imeautora = odgovorBaze.getString("autor");
  76.                 int godinaRodjenja = odgovorBaze.getInt("godinaRodjenja");
  77.                 int godinaSmrti = odgovorBaze.getInt("godinaSmrti");
  78.                 autor = new Autor(autorId, imeautora, godinaRodjenja, godinaSmrti);
  79.             } catch (SQLException e) {
  80.                 e.printStackTrace();
  81.             }
  82.             return autor;
  83.         }
  84.  
  85.         public int snimi() {
  86.             String upit = "INSERT INTO autori (autorId, autor, godinaRodjenja, godinaSmrti)" +
  87.                     "VALUES (" + autorId + ",'" + autor + "'," + godinaRodjenja + "," +
  88.                     godinaSmrti + ")";
  89.             try {
  90.                 return BazaPodataka.getInstanca().iudQuery(upit);
  91.             } catch (SQLException e) {
  92.                 e.printStackTrace();
  93.                 return 0;
  94.             }
  95.         }
  96.  
  97.         public int azuriraj(ArrayList<Podaci> podaciZaIzmenu) {
  98.             String upit = "UPDATE autori SET";
  99.             int i = 0;
  100.             for(Podaci podatak : podaciZaIzmenu) {
  101.                 upit += podatak.getPoljeUBazi() + "='" + podatak.getVrednost() + "'";
  102.                 i++;
  103.                 if(i < podaciZaIzmenu.size()) upit +=", ";
  104.  
  105.             }
  106.             upit += " WHERE autorId = " + autorId;
  107.  
  108.             try {
  109.                 return BazaPodataka.getInstanca().iudQuery(upit);
  110.             } catch (SQLException e) {
  111.                 e.printStackTrace();
  112.                 return 0;
  113.             }
  114.         }
  115.  
  116.         public static int obrisi(int autorId) {
  117.                  //za trenutni nivo radimo ovako... kasnije ucimo destruktore i nema potrebe za ovim...
  118.             String upit = "DELETE FROM autori WHERE autorId = " + autorId;
  119.             try {
  120.                 return BazaPodataka.getInstanca().iudQuery(upit);
  121.             } catch (SQLException e) {
  122.                 e.printStackTrace();
  123.                 return 0;
  124.             }
  125.         }
  126.  
  127.         @Override
  128.         public String toString() {
  129.             return "Autor{" +
  130.                     "autorId=" + autorId +
  131.                     ", autor='" + autor + '\'' +
  132.                     ", godinaRodjenja=" + godinaRodjenja +
  133.                     ", godinaSmrti=" + godinaSmrti +
  134.                     '}';
  135.         }
  136.  
  137.     }
  138.  
  139. //////////////////////////////////// Program.java
  140.  
  141. import java.util.ArrayList;
  142.  
  143. /**
  144.  * Created by markort2815 on 13.5.2017..
  145.  */
  146. public class Program {
  147.     public static void main(String[] args) {
  148.         Autor marcelPrust = Autor.dohvatiZaId(1);
  149.         System.out.println(marcelPrust);
  150.         /*Autor ivoAndric = new Autor(13,"Ivo Andric",1892,1975);
  151.         //System.out.println(ivoAndric.snimi());
  152.         Autor.obrisi(13);
  153.         System.out.println(ivoAndric);*/
  154.  
  155.         ArrayList<Podaci> podaciZaAzuriranje= new ArrayList<Podaci>();
  156.         podaciZaAzuriranje.add(new Podaci("godinaRodjenja","1900"));
  157.         podaciZaAzuriranje.add(new Podaci("godinaSmrti","1960"));
  158.         marcelPrust.azuriraj(podaciZaAzuriranje);
  159.     }
  160. }
  161.  
  162. ////////////////////////////////////////// Podaci.java
  163.  
  164. /**
  165.  * Created by markort2815 on 13.5.2017..
  166.  */
  167. public class Podaci {
  168.     String poljeUBazi;
  169.     String vrednost;
  170.  
  171.     public Podaci(String poljeUBazi, String vrednost) {
  172.         this.poljeUBazi = poljeUBazi;
  173.         this.vrednost = vrednost;
  174.     }
  175.  
  176.     public String getPoljeUBazi() {
  177.         return poljeUBazi;
  178.     }
  179.  
  180.     public String getVrednost() {
  181.         return vrednost;
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment