Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.32 KB | None | 0 0
  1. package com.mysql;
  2.  
  3. import java.io.RandomAccessFile;
  4. import java.nio.ByteBuffer;
  5. import java.nio.channels.FileChannel.MapMode;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11.  
  12. import org.apache.log4j.Logger;
  13.  
  14. import com.mysql.database.ConnectionConfig;
  15. import com.mysql.database.ConnectionDriver;
  16.  
  17. public class ImportItemDefinitions {
  18.    
  19.     private static Logger logger = Logger.getLogger(ImportItemDefinitions.class);
  20.        
  21.     public static void main(String[] args) {
  22.         logger.info("Importing Item Definitions...");      
  23.         try {
  24.             new ImportItemDefinitions().importData();
  25.         } catch (SQLException e) {
  26.             e.printStackTrace();
  27.         }
  28.     }
  29.    
  30.     static {
  31.         try {
  32.             new ConnectionDriver("jdbc:items:", "com.mysql.jdbc.Driver",  new ConnectionConfig("jdbc:mysql://localhost/dbs_development", "root", "password"));
  33.         } catch (SQLException e) {
  34.             e.printStackTrace();
  35.         } catch (InstantiationException e) {
  36.             e.printStackTrace();
  37.         } catch (IllegalAccessException e) {
  38.             e.printStackTrace();
  39.         } catch (ClassNotFoundException e) {
  40.             e.printStackTrace();
  41.         }
  42.     }
  43.    
  44.     public Connection getConnection() throws SQLException {
  45.         return DriverManager.getConnection("jdbc:items:jdcpool");
  46.     }
  47.    
  48.     public void importData() throws SQLException {     
  49.         Connection connection = null;
  50.         PreparedStatement statement = null;
  51.         ResultSet result = null;       
  52.         try {
  53.             connection = getConnection();
  54.             /*
  55.              * Clear items first.
  56.              */
  57.             statement = connection.prepareStatement("CALL spd_Items_Truncate();");
  58.             result = statement.executeQuery();
  59.             if (result.next()) {
  60.                 logger.info("Deleted all Items from database.");
  61.             }
  62.         } catch (Exception e) {
  63.             e.printStackTrace();
  64.         } finally {
  65.             /*
  66.              * Clear up any remaining objects
  67.              */
  68.             if (connection != null && !connection.isClosed()) {
  69.                 connection.close();
  70.                 connection = null;
  71.             }
  72.             if (statement != null && !statement.isClosed()) {
  73.                 statement.close();
  74.                 statement = null;
  75.             }
  76.             if (result != null && !result.isClosed()) {
  77.                 result.close();
  78.                 result = null;
  79.             }
  80.         }
  81.        
  82.         ItemDefinition[] definitions = null;
  83.        
  84.         try {
  85.             RandomAccessFile raf = new RandomAccessFile("./data/item-definitions.bin", "r");           
  86.             ByteBuffer buffer = raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length());
  87.             int length = buffer.getInt();
  88.             definitions = new ItemDefinition[20000];
  89.             for (int id = 0; id < length - 1; id ++) {
  90.                 int itemId = buffer.getShort() & 0xFFFF;
  91.                 if (itemId == -1 || itemId == 65535)
  92.                     continue;
  93.                 String name = readString(buffer);
  94.                 int notedId = buffer.getShort() & 0xFFFF;
  95.                 if (notedId == 65535)
  96.                     notedId = -1;
  97.                 int parentId = -1;
  98.                 if (notedId < itemId && notedId != -1) {
  99.                     parentId = notedId;
  100.                     notedId = -1;
  101.                 }
  102.                 definitions[itemId] = new ItemDefinition(name, itemId, notedId, parentId);
  103.             }
  104.             raf.close();
  105.             logger.info("Loaded " + definitions.length + " Item Definitions.");        
  106.             Thread.sleep(1200);
  107.         } catch (Exception e) {
  108.             e.printStackTrace();
  109.         }
  110.        
  111.         for (ItemDefinition definition : definitions) {
  112.             try {
  113.                 if (definition == null) {
  114.                     continue;
  115.                 }
  116.                 connection = getConnection();
  117.                 statement = connection.prepareStatement("CALL spd_Items_Insert(?,?,?,?,?);");
  118.                 statement.setInt(1, definition.getId());
  119.                 statement.setString(2, definition.getName());
  120.                 statement.setString(3, definition.getDescription());
  121.                 statement.setInt(4, definition.getNotedId());
  122.                 statement.setInt(5, definition.getParentId());
  123.                 result = statement.executeQuery();
  124.                 if (result.next()) {
  125.                     logger.info("Last insert id: " + result.getInt(1));
  126.                 }
  127.             } catch (Exception e) {
  128.                 e.printStackTrace();
  129.             } finally {
  130.                 /*
  131.                  * Clear up any remaining objects
  132.                  */
  133.                 if (connection != null && !connection.isClosed()) {
  134.                     connection.close();
  135.                     connection = null;
  136.                 }
  137.                 if (statement != null && !statement.isClosed()) {
  138.                     statement.close();
  139.                     statement = null;
  140.                 }
  141.                 if (result != null && !result.isClosed()) {
  142.                     result.close();
  143.                     result = null;
  144.                 }
  145.             }
  146.         }
  147.        
  148.     }
  149.    
  150.     public class ItemDefinition {
  151.         private final String name;
  152.         private String description = null;
  153.         private int insertId = -1;
  154.         private final int id;
  155.         private final int notedId;
  156.         private final int parentId;
  157.        
  158.         public ItemDefinition(String name, int id, int notedId, int parentId) {
  159.             this.name = name;
  160.             this.id = id;
  161.             this.notedId = notedId;
  162.             this.parentId = parentId;
  163.         }
  164.  
  165.         public String getName() {
  166.             return name;
  167.         }
  168.  
  169.         public int getId() {
  170.             return id;
  171.         }
  172.  
  173.         public int getNotedId() {
  174.             return notedId;
  175.         }
  176.        
  177.         public int getParentId() {
  178.             return parentId;
  179.         }
  180.  
  181.         public void setDescription(String description) {
  182.             this.description = description;
  183.         }
  184.  
  185.         public String getDescription() {
  186.             return description;
  187.         }
  188.  
  189.         public void setInsertId(int insertId) {
  190.             this.insertId = insertId;
  191.         }
  192.  
  193.         public int getInsertId() {
  194.             return insertId;
  195.         }
  196.        
  197.     }
  198.    
  199.     public static String readString(ByteBuffer buffer) {
  200.         StringBuilder bldr = new StringBuilder();
  201.         while(buffer.hasRemaining()) {
  202.             byte b = buffer.get();
  203.             if(b == 0) {
  204.                 break;
  205.             }
  206.             bldr.append((char) b);
  207.         }
  208.         return bldr.toString();
  209.     }
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement