Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.80 KB | None | 0 0
  1. package db;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.List;
  9.  
  10. import org.jdom.Document;
  11. import org.jdom.Element;
  12. import org.jdom.JDOMException;
  13. import org.jdom.input.SAXBuilder;
  14.  
  15. public class DBUpdater {
  16.  
  17.     private SAXBuilder parser;
  18.     private Connection connection;
  19.  
  20.     private final String driver = "com.mysql.jdbc.Driver";
  21.     private final String url = "jdbc:mysql://localhost:3306/dealers";
  22.     private final String login = "root";
  23.     private final String password = "";
  24.  
  25.     public DBUpdater() {
  26.         parser = new SAXBuilder();
  27.  
  28.         // Connect to DB
  29.         try {
  30.             Class.forName(driver).newInstance();
  31.             connection = DriverManager.getConnection(url, login, password);
  32.         } catch (Exception e) {
  33.             System.err.println("Exception: " + e.getMessage());
  34.         }
  35.  
  36.     }
  37.  
  38.     public static void main(String args[]) {
  39.  
  40.         if (args.length == 0) {
  41.             System.err.println("No XML file specified.");
  42.             System.exit(0);
  43.         }
  44.  
  45.         DBUpdater db = new DBUpdater();
  46.  
  47.         try {
  48.             if (!db.connection.isClosed()) {
  49.                 for (String arg : args)
  50.                     db.readFile(arg);
  51.  
  52.                 db.disconnectFromDB();
  53.             }
  54.         } catch (SQLException e) {
  55.             System.err
  56.             .println("Error connecting to the Database. Exiting application.");
  57.             System.exit(0);
  58.         }
  59.     }
  60.  
  61.     private void disconnectFromDB() throws SQLException {
  62.         if (!this.connection.isClosed()) {
  63.             this.connection.close();
  64.             System.out.println("Disconnected from the Database.");
  65.             System.exit(0);
  66.         }
  67.     }
  68.  
  69.     private void readFile(String file) {
  70.         try {
  71.             Document doc = parser.build(file);
  72.             Element root = doc.getRootElement();
  73.             getData(root);
  74.         } catch (JDOMException e) {
  75.             System.err.println("XML file " + file + " is not well formed.");
  76.             System.err.println(e.getMessage());
  77.         } catch (IOException e) {
  78.             System.err.println("Could not access " + file + ".");
  79.             System.err.println(e.getMessage());
  80.         }
  81.     }
  82.  
  83.     private void getData(Element root) {
  84.  
  85.         String brand = root.getAttributeValue("brand");
  86.         String shop = root.getNamespaceURI();
  87.  
  88.         updateON(shop);
  89.        
  90.         @SuppressWarnings("unchecked")
  91.         List<Element> laptops = root.getChildren();
  92.  
  93.         for (Element temp : laptops) {
  94.             // for (int i = 0; i < laptops.size(); i++) {
  95.             // Element temp = laptops.get(i);
  96.  
  97.             String model = temp.getAttributeValue("model");
  98.             String price = temp.getChildText("price");
  99.             String processor = temp.getChildText("processor");
  100.             String clock = temp.getChildText("clock");
  101.             String cache = temp.getChildText("cache");
  102.             String ram = temp.getChildText("ram");
  103.             String ramtype = temp.getChildText("ramtype");
  104.             String disk = temp.getChildText("disk");
  105.             String screensize = temp.getChildText("screensize");
  106.             String screenres = temp.getChildText("screenres");
  107.             String graphadap = temp.getChildText("graphadap");
  108.             String graphmem = temp.getChildText("graphmem");
  109.             String os = temp.getChildText("os");
  110.             String software = temp.getChildText("software");
  111.             String link = temp.getAttributeValue("img");
  112.  
  113.             Laptop l = new Laptop(brand, shop, model, price, processor, clock,
  114.                     cache, ram, ramtype, disk, screensize, screenres,
  115.                     graphadap, graphmem, os, software, link);
  116.  
  117.             this.insertIntoDB(l);
  118.         }
  119.        
  120.         updateOff();
  121.     }
  122.  
  123.     private void updateOff() {
  124.         // TODO Auto-generated method stub
  125.        
  126.         String delete = "DELETE FROM laptops WHERE up = 1;";
  127.        
  128.         try {
  129.             this.connection.createStatement().execute(delete);
  130.         } catch (SQLException e) {
  131.             System.err.println("Error in Query: "+delete);
  132.             //e.printStackTrace();
  133.         }
  134.        
  135.     }
  136.  
  137.     private void updateON(String shop) {
  138.         // TODO Auto-generated method stub
  139.        
  140.         String query = "UPDATE laptops set up = 1 where SHOP LIKE '"+shop+"';";
  141.         try {
  142.             this.connection.createStatement().execute(query);
  143.         } catch (SQLException e) {
  144.             System.err.println("Error in Query: "+query);
  145.             //e.printStackTrace();
  146.         }
  147.  
  148.     }
  149.  
  150.     private void insertIntoDB(Laptop l) {
  151.         String query = "INSERT INTO laptops (brand, shop, model, price, processor, clock, cache, ram, ramtype, disk, screensize, screenres, graphadap,graphmem, os, software, link) VALUES ('"
  152.             + l.getBrand() + "', '" + l.getShop() + "', '" + l.getModel() + "', '"
  153.             + l.getPrice() + "', '" + l.getProcessor() + "', '" + l.getClock()  + "', '"
  154.             + l.getCache() + "', '" + l.getRam() + "', '" + l.getRamtype() + "', '"
  155.             + l.getDisk() + "', '" + l.getScreensize() + "', '" + l.getScreenres() + "', '"
  156.             + l.getGraphadap() + "', '" + l.getGraphmem() + "', '" + l.getOs() + "', '"
  157.             + l.getSoftware() + "', '" + l.getPicurl() + "');";
  158.        
  159.        
  160.         try {
  161.             if(this.checkDB(l))
  162.                 this.connection.createStatement().execute(query);
  163.         } catch (SQLException e) {
  164.             System.err.println("Error inserting laptop " + l.getBrand() + " - " + l.getModel() + " in the database.");
  165.             e.printStackTrace();
  166.         }
  167.     }
  168.  
  169.     private boolean checkDB(Laptop l) throws SQLException {
  170.        
  171.         int price = Integer.valueOf(l.getPrice());
  172.         int id = 0, temp = 0, count = 0;
  173.        
  174.         String query = "SELECT id, model, price, shop FROM laptops WHERE model LIKE '" + l.getModel() + "';";
  175.         ResultSet result = this.connection.createStatement().executeQuery(query);
  176.        
  177.         while(result.next()) {
  178.             id = result.getInt("id");
  179.             if(result.getString("shop").compareToIgnoreCase(l.getShop()) == 0){
  180.                 temp = Integer.valueOf(result.getString("price"));
  181.                 count++;
  182.                 String update ="UPDATE laptops SET up = 0 WHERE id = "+ id+";" ;
  183.                 this.connection.createStatement().execute(update);
  184.                 System.out.println("cenas "+id);
  185.                 break;
  186.             }
  187.         }
  188.        
  189.         if(count == 0)
  190.             return true;
  191.         else if(temp > price) {
  192.             String delquery = "UPDATE laptops set price = "+temp+" WHERE id = '" + id + "';";
  193.             this.connection.createStatement().execute(delquery);
  194.             return true;
  195.         }
  196.        
  197.         return false;
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement