Advertisement
MikecIT

Baze

Nov 1st, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package podaci;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.util.HashMap;
  8.  
  9. import javax.swing.JFrame;
  10. import javax.swing.JOptionPane;
  11.  
  12.  
  13. public abstract class Baze
  14. {  
  15.     private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  16.     private static final String DB_URL = "jdbc:mysql://localhost:3306/osobe";
  17.     private static final String USER = "root";
  18.     private static final String PASS = "12345";
  19.    
  20.     private static Connection conn = null;
  21.     private static java.sql.Statement stmt = null;
  22.     private static ResultSet rs = null;
  23.     private static PreparedStatement preparedStatement = null;
  24.    
  25.     private static void close()
  26.     {
  27.         try
  28.         {
  29.             if(conn != null)
  30.             {
  31.                 conn.close();
  32.             }
  33.            
  34.             if(stmt != null)
  35.             {
  36.                 conn.close();
  37.             }
  38.            
  39.             if(rs != null)
  40.             {
  41.                 rs.close();
  42.             }
  43.            
  44.             if(preparedStatement != null)
  45.             {
  46.                 preparedStatement.close();
  47.             }
  48.         }
  49.         catch(Exception e)
  50.         {
  51.             e.printStackTrace();
  52.         }
  53.     }
  54.    
  55.     private static void start(boolean ind, String sql)
  56.     {
  57.         try
  58.         {
  59.             conn = DriverManager.getConnection(DB_URL, USER, PASS);
  60.             stmt = conn.createStatement();
  61.             if(ind == true)
  62.             {
  63.                 rs = stmt.executeQuery(sql);
  64.             }
  65.             else
  66.             {
  67.                 preparedStatement = conn.prepareStatement(sql);
  68.             }
  69.         }
  70.         catch(Exception e)
  71.         {
  72.             e.printStackTrace();
  73.             JOptionPane.showMessageDialog(new JFrame(), "Nemogu�a konencija sa bazom!", "Gre�ka", JOptionPane.ERROR_MESSAGE);
  74.             System.exit(0);
  75.         }
  76.     }
  77.    
  78.     public static HashMap<Integer, Osoba> iscitaj()
  79.     {
  80.         HashMap<Integer, Osoba> mapa = new HashMap<Integer, Osoba>();
  81.         try
  82.         {
  83.             Class.forName(JDBC_DRIVER);
  84.            
  85.             String sql = "select osoba.idOsoba, osoba.ime, osoba.prezime, osoba.godine from osoba";
  86.             start(true, sql);
  87.            
  88.             while(rs.next())
  89.             {
  90.                 int id = rs.getInt("osoba.idOsoba");
  91.                 String ime = rs.getString("osoba.ime");
  92.                 String prezime = rs.getString("osoba.prezime");
  93.                 int godine = rs.getInt("osoba.godine");
  94.                
  95.                 mapa.put(id, new Osoba(ime, prezime, godine));
  96.             }
  97.         }
  98.         catch(Exception e)
  99.         {
  100.             e.printStackTrace();
  101.         }
  102.         finally
  103.         {
  104.             close();
  105.         }
  106.        
  107.         return mapa;
  108.     }
  109.    
  110.     public static void upisi(Osoba u)
  111.     {  
  112.         try
  113.         {
  114.             Class.forName(JDBC_DRIVER);
  115.            
  116.             String sql = "insert into osoba(osoba.ime, osoba.prezime, osoba.godine) values(?, ?, ?)";
  117.             start(false, sql);
  118.            
  119.             preparedStatement.setString(1, u.getIme());
  120.             preparedStatement.setString(2, u.getPrezime());
  121.             preparedStatement.setInt(3, u.getGodine());
  122.             preparedStatement.execute();
  123.         }
  124.         catch(Exception e)
  125.         {
  126.             e.printStackTrace();
  127.         }
  128.         finally
  129.         {
  130.             close();
  131.         }
  132.     }
  133.    
  134.     public static void izmeni(HashMap.Entry<Integer, Osoba> par)
  135.     {  
  136.         try
  137.         {
  138.             Class.forName(JDBC_DRIVER);
  139.            
  140.                 String sql = "update osoba set ime = ?, prezime = ?, godine = ? where idOsoba = ?";
  141.                 start(false, sql);
  142.                
  143.                 preparedStatement.setString(1, par.getValue().getIme());
  144.                 preparedStatement.setString(2, par.getValue().getPrezime());
  145.                 preparedStatement.setInt(3, par.getValue().getGodine());
  146.                 preparedStatement.setInt(4, par.getKey());
  147.                 preparedStatement.execute();
  148.         }
  149.         catch(Exception e)
  150.         {
  151.             e.printStackTrace();
  152.         }
  153.         finally
  154.         {
  155.             close();
  156.         }  
  157.     }
  158.    
  159.     public static void obrisi(HashMap.Entry<Integer, Osoba> par)
  160.     {  
  161.         try
  162.         {
  163.             Class.forName(JDBC_DRIVER);
  164.            
  165.             String sql = "delete from osoba where idOsoba = ?";
  166.             start(false, sql);
  167.            
  168.             preparedStatement.setInt(1, par.getKey());
  169.             preparedStatement.execute();
  170.         }
  171.         catch(Exception e)
  172.         {
  173.             e.printStackTrace();
  174.         }
  175.         finally
  176.         {
  177.             close();
  178.         }
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement