Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. package com.RS09HD.model;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import org.apache.commons.dbutils.DbUtils;
  12. import org.apache.commons.dbutils.QueryRunner;
  13. import org.apache.commons.dbutils.handlers.MapListHandler;
  14.  
  15. import com.RS09HD.util.XStreamUtil;
  16. import com.RS09HD.util.log.Logger;
  17.  
  18. /**
  19. * Represents a object, primarily manages it's examine option.
  20. */
  21.  
  22. public class ObjectDefinition {
  23.  
  24. // JDBC driver name and database URL
  25. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  26. static final String DB_URL = "jdbc:mysql://localhost:3306/rs09hd";
  27.  
  28. // Database credentials
  29. static final String USER = "server";
  30. static final String PASS = "Runescape09";
  31.  
  32. private static Map<Integer, ObjectDefinition> definitions;
  33.  
  34. public static void load() throws SQLException {
  35.  
  36. Connection conn = null;
  37. QueryRunner queryRunner = new QueryRunner();
  38.  
  39. //Step 1: Register JDBC driver
  40. DbUtils.loadDriver(JDBC_DRIVER);
  41.  
  42. //Step 2: Open a connection
  43. System.out.println("Connecting to database...");
  44. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  45.  
  46. try {
  47. List<Map<String, Object>> definitions
  48. = queryRunner.query(conn, "SELECT * FROM object_examines where id = '1276'", new MapListHandler());
  49. System.out.println(definitions);
  50.  
  51. } finally {
  52. DbUtils.close(conn);
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. /*List<ObjectDefinition> defs = (List<ObjectDefinition>) XStreamUtil.getXStream().fromXML(new FileInputStream("data/objectDefinitions.xml"));
  62. definitions = new HashMap<Integer, ObjectDefinition>();
  63. for(ObjectDefinition def : defs) {
  64. definitions.put(def.getId(), def);
  65. }*/
  66. //Logger.getInstance().info("Loaded " + definitions.size() + " object examines.");
  67. }
  68.  
  69. public static ObjectDefinition forId(int id) {
  70. ObjectDefinition d = definitions.get(id);
  71. if(d == null) {
  72. d = produceDefinition(id);
  73. }
  74. return d;
  75. }
  76.  
  77. private int id;
  78. private String examine;
  79.  
  80. public int getId() {
  81. return id;
  82. }
  83.  
  84. public String getExamine() {
  85. return examine;
  86. }
  87.  
  88. public static ObjectDefinition produceDefinition(int id) {
  89. ObjectDefinition def = new ObjectDefinition();
  90. def.id = id;
  91. String idString;
  92. idString = new String( "Object ID: " + def.id);
  93. def.examine = idString;
  94. return def;
  95.  
  96. }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement