Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////BAZA PODATAKA BazaPodataka.java
- /**
- * Created by markort2815 on 13.5.2017..
- */
- import java.sql.*;
- public class BazaPodataka {
- private Connection conn;
- private static BazaPodataka instanca;
- private BazaPodataka() {
- try {
- Class.forName("org.sqlite.JDBC");
- conn = DriverManager.getConnection("jdbc:sqlite:knjige.db");
- } catch ( Exception e ) {
- System.err.println("Doslo je do greske pri konekciji na bazu podataka" + e.getClass().getName() + ": " + e.getMessage() );
- System.exit(0);
- }
- }
- public static BazaPodataka getInstanca() { //singluton design pattern
- if(instanca == null)
- instanca = new BazaPodataka();
- return instanca;
- }
- public void automatskaTransakcija(boolean on_off) throws SQLException { conn.setAutoCommit(on_off); }
- public void snimiTransakciju() throws SQLException { conn.commit(); }
- //metoda za upite koji su tipa INSERT, UPDATE, DELETE
- public int iudQuery(String sql) throws SQLException {
- System.out.println(sql);
- Statement statement = conn.createStatement();
- return statement.executeUpdate(sql);
- }
- public ResultSet select(String sql) throws SQLException {
- System.out.println(sql);
- Statement statement= conn.createStatement();
- return statement.executeQuery(sql);
- }
- }
- ///////////////////////////////////// Autor.java
- /**
- * Created by markort2815 on 13.5.2017..
- */
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- public class Autor {
- private int autorId;
- private String autor;
- private int godinaRodjenja;
- private int godinaSmrti;
- public Autor(int autorId, String autor, int godinaRodjenja, int godinaSmrti) {
- this.autorId = autorId;
- this.autor = autor;
- this.godinaRodjenja = godinaRodjenja;
- this.godinaSmrti = godinaSmrti;
- }
- public static Autor dohvatiZaId(int id) {
- String upit = "SELECT * FROM autori WHERE autorId = " + id;
- Autor autor = null;
- try {
- ResultSet odgovorBaze = BazaPodataka.getInstanca().select(upit);
- int autorId = odgovorBaze.getInt("autorId");
- String imeautora = odgovorBaze.getString("autor");
- int godinaRodjenja = odgovorBaze.getInt("godinaRodjenja");
- int godinaSmrti = odgovorBaze.getInt("godinaSmrti");
- autor = new Autor(autorId, imeautora, godinaRodjenja, godinaSmrti);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return autor;
- }
- public int snimi() {
- String upit = "INSERT INTO autori (autorId, autor, godinaRodjenja, godinaSmrti)" +
- "VALUES (" + autorId + ",'" + autor + "'," + godinaRodjenja + "," +
- godinaSmrti + ")";
- try {
- return BazaPodataka.getInstanca().iudQuery(upit);
- } catch (SQLException e) {
- e.printStackTrace();
- return 0;
- }
- }
- public int azuriraj(ArrayList<Podaci> podaciZaIzmenu) {
- String upit = "UPDATE autori SET";
- int i = 0;
- for(Podaci podatak : podaciZaIzmenu) {
- upit += podatak.getPoljeUBazi() + "='" + podatak.getVrednost() + "'";
- i++;
- if(i < podaciZaIzmenu.size()) upit +=", ";
- }
- upit += " WHERE autorId = " + autorId;
- try {
- return BazaPodataka.getInstanca().iudQuery(upit);
- } catch (SQLException e) {
- e.printStackTrace();
- return 0;
- }
- }
- public static int obrisi(int autorId) {
- //za trenutni nivo radimo ovako... kasnije ucimo destruktore i nema potrebe za ovim...
- String upit = "DELETE FROM autori WHERE autorId = " + autorId;
- try {
- return BazaPodataka.getInstanca().iudQuery(upit);
- } catch (SQLException e) {
- e.printStackTrace();
- return 0;
- }
- }
- @Override
- public String toString() {
- return "Autor{" +
- "autorId=" + autorId +
- ", autor='" + autor + '\'' +
- ", godinaRodjenja=" + godinaRodjenja +
- ", godinaSmrti=" + godinaSmrti +
- '}';
- }
- }
- //////////////////////////////////// Program.java
- import java.util.ArrayList;
- /**
- * Created by markort2815 on 13.5.2017..
- */
- public class Program {
- public static void main(String[] args) {
- Autor marcelPrust = Autor.dohvatiZaId(1);
- System.out.println(marcelPrust);
- /*Autor ivoAndric = new Autor(13,"Ivo Andric",1892,1975);
- //System.out.println(ivoAndric.snimi());
- Autor.obrisi(13);
- System.out.println(ivoAndric);*/
- ArrayList<Podaci> podaciZaAzuriranje= new ArrayList<Podaci>();
- podaciZaAzuriranje.add(new Podaci("godinaRodjenja","1900"));
- podaciZaAzuriranje.add(new Podaci("godinaSmrti","1960"));
- marcelPrust.azuriraj(podaciZaAzuriranje);
- }
- }
- ////////////////////////////////////////// Podaci.java
- /**
- * Created by markort2815 on 13.5.2017..
- */
- public class Podaci {
- String poljeUBazi;
- String vrednost;
- public Podaci(String poljeUBazi, String vrednost) {
- this.poljeUBazi = poljeUBazi;
- this.vrednost = vrednost;
- }
- public String getPoljeUBazi() {
- return poljeUBazi;
- }
- public String getVrednost() {
- return vrednost;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment