Advertisement
Guest User

DB

a guest
Dec 11th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.64 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.  
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.Vector;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16.  
  17. /**
  18.  *
  19.  * @author Mihael
  20.  */
  21. public class DB {
  22.     public java.sql.Connection conn;
  23.     public Logger logger;
  24.     public Logger logger_err;
  25.    
  26.    
  27.     public DB(){
  28.         try{
  29.            
  30.         }catch(Exception e){
  31.             e.printStackTrace();
  32.         }
  33.     }
  34.    
  35.        public boolean isConnected(){
  36.         if(this.conn!=null)
  37.             return true;
  38.         else
  39.             return false;
  40.     }
  41.  
  42.     public boolean disconnect() throws SQLException {
  43.         boolean OK=false;
  44.         try {
  45.             if (conn != null) {
  46.                 conn.close();
  47.                 OK=true;
  48.             }
  49.         } catch (SQLException e) {
  50.             logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());
  51.         }
  52.         finally{
  53.             return OK;
  54.         }  
  55.     }
  56.    
  57.     public void initConnection_MySQL() {
  58.            try {
  59.                Class.forName("com.mysql.jdbc.Driver").newInstance();
  60.                /*     DriverManager.getConnection("jdbc:mysql://"server":"port"/"dbname"?user="user"&password="password"&characterEncoding=Cp1250");
  61.                 */
  62.                java.util.Properties props = new java.util.Properties();
  63.  
  64.                props.put("characterEncoding", "cp1250");
  65.                props.put("characterSetResults", "cp1250");
  66.                props.put("user", "root");
  67.                props.put("password", "0000");
  68.  
  69.                this.conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mevb", props);
  70.  
  71.            } catch (Exception e) {
  72.                e.printStackTrace();
  73.            }
  74.     }
  75.    
  76.        public boolean disconnect(Connection con) throws SQLException {
  77.         boolean OK = false;
  78.         try {
  79.             if (con != null) {
  80.                 con.close();
  81.                 OK = true;
  82.             }
  83.         } catch (SQLException e) {
  84.             logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());
  85.         }
  86.         return OK;
  87.     }
  88.        
  89.     public boolean BrisanjePremaCijeni(int cijena) throws java.sql.SQLException{
  90.         if(this.conn == null){return false;}
  91.         try{
  92.             Statement stm;
  93.             String query ="DELETE FROM AUTOMOBIL WHERE CIJENA='"+cijena+"'";
  94.             PreparedStatement stmt = this.conn.prepareStatement(query);
  95.             stmt.executeUpdate();
  96.             stmt.close();
  97.             return true;
  98.         }catch(SQLException e){
  99.             return false;
  100.         }
  101.     }
  102.      
  103.        
  104.     public boolean BrisanjePremaModelu(String model) throws java.sql.SQLException{
  105.         if(this.conn == null){return false;}
  106.         try{
  107.             Statement stm;
  108.             String query = "DELETE FROM AUTOMOBIL WHERE MODEL='"+model+"'";
  109.             PreparedStatement stmt = this.conn.prepareStatement(query);
  110.             stmt.executeUpdate();
  111.             stmt.close();            
  112.             return true;
  113.            
  114.         }catch(SQLException e){
  115.             return false;
  116.         }
  117.     }  
  118.        
  119.     public boolean BrisanjePremaMarki(String marka) throws java.sql.SQLException{
  120.         if(this.conn == null){return false;}
  121.         try{
  122.             Statement stm;
  123.             String query = "DELETE FROM AUTOMOBIL WHERE MARKA='"+marka+"'";
  124.             PreparedStatement stmt = this.conn.prepareStatement(query);
  125.             stmt.executeUpdate();
  126.             stmt.close();
  127.             return true;
  128.         }catch(SQLException e){
  129.             return false;
  130.         }
  131.     }
  132.        
  133.        
  134.     public Vector PrikazPremaCijeni(int cijena) throws java.sql.SQLException{
  135.         Vector vec = new Vector();
  136.         if(this.conn == null){
  137.             return vec;            
  138.         }
  139.         try{
  140.             Statement stm = this.conn.createStatement();
  141.             String query = "SELECT * FROM AUTOMOBIL WHERE CIJENA='"+cijena+"'";
  142.             ResultSet rs = stm.executeQuery(query);
  143.             while(rs.next()){
  144.                 Automobil auto = new Automobil();
  145.                 auto.setID(rs.getInt(1));
  146.                 auto.setNaziv(rs.getString(2));
  147.                 auto.setModel(rs.getString(3));
  148.                 auto.setCijena(rs.getInt(4));
  149.                 vec.addElement(auto);                
  150.             }
  151.             stm.close();
  152.             rs.close();
  153.             query = null;
  154.             return vec;
  155.            
  156.         }catch(SQLException e){
  157.             logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());
  158.             return vec;
  159.         }
  160.                
  161.     }
  162.    
  163.      public Vector PrikazPremaModelu(String model) throws java.sql.SQLException{
  164.         Vector vec = new Vector();
  165.         if(this.conn == null){
  166.             return vec;
  167.         }
  168.         try{
  169.             Statement stm = this.conn.createStatement();
  170.             String query = "SELECT * FROM AUTOMOBIL WHERE MODEL='"+model+"'";
  171.             ResultSet rs = stm.executeQuery(query);
  172.             while(rs.next()){
  173.                 Automobil auto = new Automobil();
  174.                 auto.setID(rs.getInt(1));
  175.                 auto.setNaziv(rs.getString(2));
  176.                 auto.setModel(rs.getString(3));
  177.                 auto.setCijena(rs.getInt(4));
  178.                 vec.addElement(auto);
  179.             }
  180.             stm.close();
  181.             rs.close();
  182.             query = null;
  183.             return vec;
  184.            
  185.         }catch(SQLException e){
  186.             logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());
  187.             return vec;
  188.         }  
  189.      }
  190.        
  191.        
  192.     public Vector PrikazPremaNazivu(String naziv) throws java.sql.SQLException{
  193.         Vector vec = new Vector();
  194.         if(this.conn == null){
  195.             return vec;
  196.         }
  197.         try{
  198.             Statement stm = this.conn.createStatement();
  199.             String query = "SELECT * FROM AUTOMOBIL WHERE MARKA='"+naziv+"'";
  200.             ResultSet rs = stm.executeQuery(query);
  201.             while(rs.next()){
  202.                 Automobil auto = new Automobil();
  203.                 auto.setID(rs.getInt(1));
  204.                 auto.setNaziv(rs.getString(2));
  205.                 auto.setModel(rs.getString(3));
  206.                 auto.setCijena(rs.getInt(4));
  207.                 vec.addElement(auto);
  208.             }
  209.             stm.close();
  210.             rs.close();
  211.             query = null;
  212.             return vec;
  213.            
  214.         }catch(SQLException e){
  215.             logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());
  216.             return vec;
  217.         }
  218.              
  219.                
  220.     }
  221.        
  222.     public Vector PrikazAutomobila()throws java.sql.SQLException{        
  223.         Vector vec = new Vector();
  224.          if (this.conn == null) {
  225.             return vec;
  226.         }
  227.         try{
  228.             Statement stm = this.conn.createStatement();
  229.             String query = "SELECT * FROM AUTOMOBIL";
  230.             ResultSet rs = stm.executeQuery(query);
  231.             while(rs.next()){
  232.                 Automobil auto = new Automobil();
  233.                 auto.setID(rs.getInt(1));
  234.                 auto.setNaziv(rs.getString(2));
  235.                 auto.setModel(rs.getString(3));
  236.                 auto.setCijena(rs.getInt(4));
  237.                 vec.addElement(auto);
  238.             }
  239.             query = null;
  240.             stm.close();
  241.             rs.close();
  242.             return vec;
  243.            
  244.         }catch(SQLException e){
  245.             logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());          
  246.             return vec;
  247.         }
  248.     }  
  249.        
  250.        
  251.     public boolean UpisAuta(Automobil data){
  252.         if(this.conn == null){
  253.             return false;
  254.         }
  255.         try{
  256.             Statement stm;
  257.             String query = "INSERT INTO AUTOMOBIL VALUES(?,?,?,?)";
  258.             PreparedStatement stmt = this.conn.prepareStatement(query);
  259.            
  260.             stmt.setInt(1,data.getID());
  261.             stmt.setString(2, data.getNaziv());
  262.             stmt.setString(3, data.getModel());
  263.             stmt.setInt(4, data.getCijena());
  264.             stmt.executeUpdate();
  265.             stmt.close();
  266.             return true;
  267.            
  268.         }catch(SQLException e){
  269.             logger_err.log(Level.WARNING, "SQLException: " + e.getMessage());          
  270.             return false;
  271.         }
  272.     }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement