Advertisement
Guest User

Fuckit

a guest
Jul 18th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.50 KB | None | 0 0
  1. package JsonToMysql;
  2.  
  3. import java.io.*;
  4. import java.net.URL;
  5. import java.nio.charset.Charset;
  6.  
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12.  
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15.  
  16. import Constants.KeyReader;
  17.  
  18. public class jsonMysql {
  19.  
  20.     private static Connection conn;
  21.  
  22.     private static String readAll(Reader rd) throws IOException {
  23.         StringBuilder sb = new StringBuilder();
  24.         int cp;
  25.         while ((cp = rd.read()) != -1) {
  26.             sb.append((char) cp);
  27.         }
  28.         return sb.toString();
  29.     }
  30.  
  31.     public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
  32.         InputStream is = new URL(url).openStream();
  33.         try {
  34.             BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  35.             String jsonText = readAll(rd);
  36.             JSONObject json = new JSONObject(jsonText);
  37.             return json;
  38.         } finally {
  39.             is.close();
  40.         }
  41.     }
  42.  
  43.     public static void mysqlCon() {
  44.  
  45.         try {
  46.             Class.forName("com.mysql.jdbc.Driver");
  47.             String url = "jdbc:mysql://localhost/?autoReconnect=true&useSSL=false";
  48.             String user = "root"; String pass = "abcd1234";
  49.             conn = DriverManager.getConnection(url, user, pass);
  50.             conn.setAutoCommit(false);
  51.         } catch (ClassNotFoundException e) {
  52.             System.err.println(e);
  53.         } catch (SQLException e) {
  54.             System.err.println(e);
  55.         }
  56.     }
  57.  
  58.     public static void insertField(Object object, Object object2, Object object3, Object object4, Object object5, Object object6, Object object7, Object object8, Object object9, Object object10, Object object11, Object object12, Object object13, Object object14, Object object15, Object object16, Object object17, Object object18, Object object19, Object object20, Object object21, Object object22, Object object23, Object object24, Object object25, Object object26, Object object27, Object object28, Object object29, Object object30, Object object31, Object object32, Object object33, Object object34) {
  59.         int bool1; int bool2; int bool3; int bool4; int bool5;
  60.  
  61.         try {
  62.             Statement state = conn.createStatement();
  63.             String nameObj = (String)object4; nameObj = nameObj.replace("'", "\\'");
  64.  
  65.             // 13 25 26 31 32 - 9 10
  66.            
  67.             if(object13 == "true") { bool1 = 1; } else { bool1 = 0; }
  68.             if(object25 == "true") { bool2 = 1; } else { bool2 = 0; }
  69.             if(object26 == "true") { bool3 = 1; } else { bool3 = 0; }
  70.             if(object31 == "true") { bool4 = 1; } else { bool4 = 0; }
  71.             if(object32 == "true") { bool5 = 1; } else { bool5 = 0; }
  72.  
  73.             if(findDuplicate((int)object) == true) {
  74.                 System.err.println("DUP ERROR! REMOVING DUP ID: " +(int)object);
  75.                 state.executeUpdate("DELETE FROM `json_mysqlbase`.`item_data` WHERE `id`='"+object+"'");
  76.                 conn.commit();
  77.             }
  78.            
  79.             if(((int)object9 == 4 && (int)object10 == 0) || ((int)object9 == 4 && (int)object10 == 4)) {
  80.                 state.executeUpdate("INSERT INTO `json_mysqlbase`.`item_data` (`id`, `disenchantingSkillRank`, `description`, `name`, `icon`, `stackable`, `itemBind`, `buyPrice`, `itemClass`, `itemSubClass`, `containerSlots`, `inventoryType`, `equippable`, `itemLevel`, `maxCount`, `maxDurability`, `minFactionId`, `minReputation`, `quality`, `sellPrice`, `requiredSkill`, `requiredLevel`, `requiredSkillRank`, `baseArmor`, `hasSockets`, `isAuctionable`, `armor`, `displayInfoId`, `nameDescription`, `nameDescriptionColor`, `upgradable`, `heroicTooltip`, `context`, `artifactId`) VALUES ('"+object+"', '"+object2+"', '"+object3+"', '"+object4+"', '"+object5+"', '"+object6+"', '"+object7+"', '"+object8+"', '"+object9+"', '"+object10+"', '"+object11+"', '"+object12+"', '"+bool1+"', '"+object14+"', '"+object15+"', '"+object16+"', '"+object17+"', '"+object18+"', '"+object19+"', '"+object20+"', '"+object21+"', '"+object22+"', '"+object23+"', '"+object24+"', '"+bool2+"', '"+bool3+"', '"+object27+"', '"+object28+"', '"+object29+"', '"+object30+"', '"+bool4+"', '"+bool5+"', '"+object33+"', '"+object34+"')");
  81.                 conn.commit();
  82.             } else {
  83.                 System.err.println("ITEMCLASS ERROR! IGNORE ID: " +(int)object);
  84.             }
  85.  
  86.             //conn.commit();
  87.         } catch (SQLException e) {
  88.             e.printStackTrace();
  89.         }
  90.     }
  91.  
  92.     public static boolean findDuplicate(int id) throws SQLException {
  93.  
  94.         Statement state = conn.createStatement();
  95.         ResultSet rs = state.executeQuery("select * from `json_mysqlbase`.`item_data` where id = " +id);
  96.         conn.commit();
  97.  
  98.         if (rs.next()) {
  99.             return true;
  100.         } else {
  101.             return false;
  102.         }
  103.     }
  104.  
  105.     public static void main(String[] args) throws IOException, JSONException {
  106.  
  107.         mysqlCon(); JSONObject data = null;
  108.  
  109.         for (int item = 138000; item <= 139000; item++) {
  110.  
  111.             try {
  112.                 data = readJsonFromUrl("https://us.api.battle.net/wow/item/" + item + "?locale=en_US&apikey="+KeyReader.GetKey());
  113.                 insertField(data.get("id"), data.get("disenchantingSkillRank"), data.get("description"), data.get("name"), data.get("icon"), data.get("stackable"), data.get("itemBind"), data.get("buyPrice"), data.get("itemClass"), data.get("itemSubClass"), data.get("containerSlots"), data.get("inventoryType"), data.get("equippable"), data.get("itemLevel"), data.get("maxCount"), data.get("maxDurability"), data.get("minFactionId"), data.get("minReputation"), data.get("quality"), data.get("sellPrice"), data.get("requiredSkill"), data.get("requiredLevel"), data.get("requiredSkillRank"), data.get("baseArmor"), data.get("hasSockets"), data.get("isAuctionable"), data.get("armor"), data.get("displayInfoId"), data.get("nameDescription"), data.get("nameDescriptionColor"), data.get("upgradable"), data.get("heroicTooltip"), data.get("context"), data.get("artifactId"));
  114.  
  115.                 bonusStats value1 = new bonusStats(data, item); // bonusStats.java
  116.                 value1.bonusStatsManager(conn);
  117.                 itemSpells value2 = new itemSpells(data, item); // itemSpells.java
  118.                 value2.itemSpellsManager(conn);
  119.                 itemSource value3 = new itemSource(data, item); // itemSource.java
  120.                 value3.itemSourceManager(conn);
  121.                 bonusLists value4 = new bonusLists(data, item); // bonusLists.java
  122.                 value4.bonusListsManager(conn);
  123.                 availableContexts value5 = new availableContexts(data, item); // availableContexts.java
  124.                 value5.availableContextsManager(conn);
  125.                 bonusSummary value6 = new bonusSummary(data, item); // bonusSummary.java
  126.                 value6.bonusSummaryManager(conn);
  127.                
  128.                 System.out.println("Item id " + data.get("id") + " is OK!");
  129.             } catch (JSONException e) {
  130.                 System.out.println("JSONException" +e);
  131.             } catch (FileNotFoundException e) {
  132.                 System.out.println("FileNotFoundException: " + item);
  133.             }
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement