Advertisement
Guest User

Untitled

a guest
May 6th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package Persistance;
  7.  
  8. import ItemManagement.scoreValues;
  9. import ItemSets.itemSet;
  10. import java.io.ByteArrayInputStream;
  11. import java.io.ByteArrayOutputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.ObjectInputStream;
  15. import java.io.ObjectOutputStream;
  16. import java.sql.*;
  17. import java.util.ArrayList;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20.  
  21. /**
  22. *
  23. * @author olive
  24. */
  25. public class DbConnect {
  26.  
  27. private Connection con;
  28. private Statement st;
  29. private ResultSet rs;
  30.  
  31. public DbConnect() {
  32. try {
  33. Class.forName("com.mysql.jdbc.Driver");
  34. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/itemsets?useSSL=false", "root", "root");
  35. st = con.createStatement();
  36.  
  37. } catch (Exception ex) {
  38. System.out.println("Error; " + ex);
  39. }
  40. }
  41.  
  42. public void testGet() {
  43. try {
  44. String query = "select * from item_set";
  45. rs = st.executeQuery(query);
  46. System.out.println("Records from item_set");
  47. while (rs.next()) {
  48. int iditem_set = rs.getInt(1);
  49. int power = rs.getInt(3);
  50. System.out.println("iditem_set: " + iditem_set + " power: " + power);
  51. }
  52.  
  53. } catch (Exception ex) {
  54. System.out.println("Error; " + ex);
  55. }
  56. }
  57.  
  58. public void storeItem(ArrayList mySets) throws IOException {
  59. int index = 0;
  60. while (index != mySets.size()) {
  61. itemSet myset = (itemSet) mySets.get(index);
  62. scoreValues setValues = myset.getSetValues();
  63. try {
  64.  
  65. String sql = "INSERT INTO ITEM_SET(stored_set,power,set_precision,vitality,toughness,concentration,condition_damage,expertise,ferocity,healing_power) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
  66. con.setAutoCommit(false);
  67. PreparedStatement preparedStatement
  68. = con.prepareStatement(sql);
  69.  
  70. Blob blob = con.createBlob();
  71.  
  72. ObjectOutputStream oos;
  73. oos = new ObjectOutputStream(blob.setBinaryStream(1));
  74. oos.writeObject(myset);
  75. oos.close();
  76.  
  77. int power = setValues.getPower();
  78. int precision = setValues.getPrecision();
  79. int toughness = setValues.getToughness();
  80. int vitality = setValues.getVitality();
  81. int boon_duration = setValues.getBoon_duration();
  82. int condition_damage = setValues.getCondition_damage();
  83. int condition_duration = setValues.getCondition_duration();
  84. int crit_damage = setValues.getCrit_damage();
  85. int healing_power = setValues.getHealing_power();
  86.  
  87. preparedStatement.setBlob(1, blob);
  88. preparedStatement.setInt(2, power);
  89. preparedStatement.setInt(3, precision);
  90. preparedStatement.setInt(4, toughness);
  91. preparedStatement.setInt(5, vitality);
  92. preparedStatement.setInt(6, boon_duration);
  93. preparedStatement.setInt(7, condition_damage);
  94. preparedStatement.setInt(8, condition_duration);
  95. preparedStatement.setInt(9, crit_damage);
  96. preparedStatement.setInt(10, healing_power);
  97.  
  98. int rowsAffected = preparedStatement.executeUpdate();
  99. oos.flush();
  100. System.out.println(rowsAffected + " rows affected");
  101. System.out.println(index + " updated of " + mySets.size());
  102. con.commit();
  103.  
  104. } // write list to ObjectOutputStream
  105. catch (SQLException ex) {
  106. Logger.getLogger(DbConnect.class.getName()).log(Level.SEVERE, null, ex);
  107. }
  108. index++;
  109. }
  110.  
  111. }
  112.  
  113. public ArrayList getItemSets(scoreValues myvals) throws IOException, ClassNotFoundException {
  114. try {
  115. /* SELECT * from item_set
  116. order by ABS(power-500) + ABS(set_precision-300) + ABS(vitality-0) + ABS(toughness-20) + ABS(concentration-0) + ABS(condition_damage-300) + ABS(expertise-0) + ABS(ferocity-300) + ABS(healing_power-500)
  117. limit 10;*/
  118.  
  119. // for user values where !0 include abs, where 0 ignore
  120. String Sql = "SELECT * FROM item_set ORDER BY " + buildSqlString(myvals) + " limit 10;";
  121. ArrayList myResults = new ArrayList();
  122. System.out.println(Sql);
  123. Statement stmt = null;
  124. String query = Sql;
  125.  
  126. stmt = con.createStatement();
  127. ResultSet rs = stmt.executeQuery(query);
  128.  
  129. while (rs.next()) {
  130. Blob myBlob = con.createBlob();
  131. myBlob = rs.getBlob("stored_set");
  132. InputStream binaryStream = myBlob.getBinaryStream();
  133. ObjectInputStream ois = new ObjectInputStream(binaryStream);
  134. String toString = myBlob.toString();
  135. System.out.println(toString);
  136. itemSet mySet = new itemSet();
  137. mySet = (itemSet) ois.readObject();
  138.  
  139. myResults.add(mySet);
  140. }
  141. return myResults;
  142.  
  143. } catch (SQLException ex) {
  144. Logger.getLogger(DbConnect.class.getName()).log(Level.SEVERE, null, ex);
  145. }
  146. return null;
  147. }
  148.  
  149. public String buildSqlString(scoreValues myvals) {
  150.  
  151. String genSql = "";
  152. boolean preceding = false;
  153.  
  154. if (myvals.getPower() != 0) {
  155. genSql = genSql + "ABS(power-" + myvals.getPower() + ")";
  156. preceding = true;
  157. }
  158. if (myvals.getPrecision() != 0) {
  159. if (preceding == true) {
  160. genSql = genSql + " + ";
  161. }
  162. genSql = genSql + "ABS(set_precision-" + myvals.getPrecision() + ")";
  163. preceding = true;
  164. }
  165. if (myvals.getVitality() != 0) {
  166. if (preceding == true) {
  167. genSql = genSql + " + ";
  168. }
  169. genSql = genSql + "ABS(vitality-" + myvals.getVitality() + ")";
  170. preceding = true;
  171. }
  172. if (myvals.getToughness() != 0) {
  173. if (preceding == true) {
  174. genSql = genSql + " + ";
  175. }
  176. genSql = genSql + "ABS(toughness-" + myvals.getToughness() + ")";
  177. preceding = true;
  178. }
  179. if (myvals.getBoon_duration() != 0) {
  180. if (preceding == true) {
  181. genSql = genSql + " + ";
  182. }
  183. genSql = genSql + "ABS(concentration-" + myvals.getBoon_duration() + ")";
  184. preceding = true;
  185. }
  186. if (myvals.getCondition_damage() != 0) {
  187. if (preceding == true) {
  188. genSql = genSql + " + ";
  189. }
  190. genSql = genSql + "ABS(condition_damage-" + myvals.getCondition_damage() + ")";
  191. preceding = true;
  192. }
  193. if (myvals.getCondition_duration() != 0) {
  194. if (preceding == true) {
  195. genSql = genSql + " + ";
  196. }
  197. genSql = genSql + "ABS(expertise-" + myvals.getCondition_duration() + ")";
  198. preceding = true;
  199. }
  200. if (myvals.getCrit_damage() != 0) {
  201. if (preceding == true) {
  202. genSql = genSql + " + ";
  203. }
  204. genSql = genSql + "ABS(ferocity-" + myvals.getCrit_damage() + ")";
  205. preceding = true;
  206. }
  207. if (myvals.getHealing_power() != 0) {
  208. if (preceding == true) {
  209. genSql = genSql + " + ";
  210. }
  211. genSql = genSql + "ABS(healing_power-" + myvals.getHealing_power() + ")";
  212. preceding = true;
  213. }
  214. return genSql;
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement