Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mysql;
- import java.io.RandomAccessFile;
- import java.nio.ByteBuffer;
- import java.nio.channels.FileChannel.MapMode;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.apache.log4j.Logger;
- import com.mysql.database.ConnectionConfig;
- import com.mysql.database.ConnectionDriver;
- public class ImportItemDefinitions {
- private static Logger logger = Logger.getLogger(ImportItemDefinitions.class);
- public static void main(String[] args) {
- logger.info("Importing Item Definitions...");
- try {
- new ImportItemDefinitions().importData();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- static {
- try {
- new ConnectionDriver("jdbc:items:", "com.mysql.jdbc.Driver", new ConnectionConfig("jdbc:mysql://localhost/dbs_development", "root", "password"));
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- public Connection getConnection() throws SQLException {
- return DriverManager.getConnection("jdbc:items:jdcpool");
- }
- public void importData() throws SQLException {
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet result = null;
- try {
- connection = getConnection();
- /*
- * Clear items first.
- */
- statement = connection.prepareStatement("CALL spd_Items_Truncate();");
- result = statement.executeQuery();
- if (result.next()) {
- logger.info("Deleted all Items from database.");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- /*
- * Clear up any remaining objects
- */
- if (connection != null && !connection.isClosed()) {
- connection.close();
- connection = null;
- }
- if (statement != null && !statement.isClosed()) {
- statement.close();
- statement = null;
- }
- if (result != null && !result.isClosed()) {
- result.close();
- result = null;
- }
- }
- ItemDefinition[] definitions = null;
- try {
- RandomAccessFile raf = new RandomAccessFile("./data/item-definitions.bin", "r");
- ByteBuffer buffer = raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length());
- int length = buffer.getInt();
- definitions = new ItemDefinition[20000];
- for (int id = 0; id < length - 1; id ++) {
- int itemId = buffer.getShort() & 0xFFFF;
- if (itemId == -1 || itemId == 65535)
- continue;
- String name = readString(buffer);
- int notedId = buffer.getShort() & 0xFFFF;
- if (notedId == 65535)
- notedId = -1;
- int parentId = -1;
- if (notedId < itemId && notedId != -1) {
- parentId = notedId;
- notedId = -1;
- }
- definitions[itemId] = new ItemDefinition(name, itemId, notedId, parentId);
- }
- raf.close();
- logger.info("Loaded " + definitions.length + " Item Definitions.");
- Thread.sleep(1200);
- } catch (Exception e) {
- e.printStackTrace();
- }
- for (ItemDefinition definition : definitions) {
- try {
- if (definition == null) {
- continue;
- }
- connection = getConnection();
- statement = connection.prepareStatement("CALL spd_Items_Insert(?,?,?,?,?);");
- statement.setInt(1, definition.getId());
- statement.setString(2, definition.getName());
- statement.setString(3, definition.getDescription());
- statement.setInt(4, definition.getNotedId());
- statement.setInt(5, definition.getParentId());
- result = statement.executeQuery();
- if (result.next()) {
- logger.info("Last insert id: " + result.getInt(1));
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- /*
- * Clear up any remaining objects
- */
- if (connection != null && !connection.isClosed()) {
- connection.close();
- connection = null;
- }
- if (statement != null && !statement.isClosed()) {
- statement.close();
- statement = null;
- }
- if (result != null && !result.isClosed()) {
- result.close();
- result = null;
- }
- }
- }
- }
- public class ItemDefinition {
- private final String name;
- private String description = null;
- private int insertId = -1;
- private final int id;
- private final int notedId;
- private final int parentId;
- public ItemDefinition(String name, int id, int notedId, int parentId) {
- this.name = name;
- this.id = id;
- this.notedId = notedId;
- this.parentId = parentId;
- }
- public String getName() {
- return name;
- }
- public int getId() {
- return id;
- }
- public int getNotedId() {
- return notedId;
- }
- public int getParentId() {
- return parentId;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public String getDescription() {
- return description;
- }
- public void setInsertId(int insertId) {
- this.insertId = insertId;
- }
- public int getInsertId() {
- return insertId;
- }
- }
- public static String readString(ByteBuffer buffer) {
- StringBuilder bldr = new StringBuilder();
- while(buffer.hasRemaining()) {
- byte b = buffer.get();
- if(b == 0) {
- break;
- }
- bldr.append((char) b);
- }
- return bldr.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement