Advertisement
Guest User

ass

a guest
Apr 26th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. package parcaToplama;
  2.  
  3. import java.net.URL;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9.  
  10. import hardwares.CPU;
  11. import hardwares.GPU;
  12. import hardwares.HDD;
  13. import hardwares.MOBO;
  14. import hardwares.PSU;
  15. import hardwares.RAM;
  16. import parcaEkleCikar.MainFrame;
  17.  
  18. public class Sqltest {
  19.  
  20. public static ArrayList<CPU> cpu = new ArrayList<>();
  21. public static ArrayList<GPU> gpu = new ArrayList<>();
  22. public static ArrayList<MOBO> mobo = new ArrayList<>();
  23. public static ArrayList<HDD> hdd = new ArrayList<>();
  24. public static ArrayList<PSU> psu = new ArrayList<>();
  25. public static ArrayList<RAM> ram = new ArrayList<>();
  26. int i = 1;
  27. private String whatAreWeDeleting="";
  28.  
  29. /*public static void main(String[] args) {
  30. // TODO Auto-generated method stub
  31. try {
  32. new Sqltest().getConnection();
  33. } catch (Exception e) {
  34. // TODO Auto-generated catch block
  35. System.out.println("error");
  36. e.printStackTrace();
  37. }
  38.  
  39. }*/
  40.  
  41. public Connection getConnection() throws Exception {
  42.  
  43. String driver = "com.mysql.jdbc.Driver";
  44. String url = "jdbc:mysql://localhost:3306/computer_hw";
  45. String username = "root";
  46. String password = "";
  47. Class.forName(driver);
  48.  
  49. Connection conn = DriverManager.getConnection(url, username, password);
  50. // System.out.println("Connection successfull");
  51.  
  52. Statement statement = conn.createStatement();
  53.  
  54. // int result=statement.executeUpdate("ALTER TABLE `test_table` ADD
  55. // `bu_adi` INT(150) NOT NULL AFTER `hey`");
  56. // int result = statement.executeUpdate("ALTER TABLE `test_table` DROP
  57. // `bu_adi`");
  58.  
  59. // queryler
  60.  
  61. // queryler
  62.  
  63. String query = "SELECT * FROM `cpu`";
  64. ResultSet result = statement.executeQuery(query);
  65.  
  66. while (result.next()) {
  67. cpu.add(new CPU(result.getInt("id"), result.getString("brand"), result.getString("model"),
  68. result.getInt("price"), result.getFloat("power"), result.getInt("core"), result.getFloat("ghz"),
  69. result.getString("socket"), result.getString("ddr"), new URL(result.getString("URL"))));
  70. }
  71. String query2 = "SELECT * FROM `ram`";
  72. ResultSet result2 = statement.executeQuery(query2);
  73.  
  74. while (result2.next()) {
  75. ram.add(new RAM(result2.getInt("id"), result2.getString("brand"), result2.getString("model"),
  76. result2.getInt("price"), result2.getFloat("power"), result2.getString("memory_type"),
  77. result2.getInt("clock_speed"), result2.getInt("memory_capacity"),
  78. new URL(result2.getString("URL"))));
  79. }
  80.  
  81. String query3 = "SELECT * FROM `gpu`";
  82. ResultSet result3 = statement.executeQuery(query3);
  83.  
  84. while (result3.next()) {
  85. gpu.add(new GPU(result3.getInt("id"), result3.getString("brand"), result3.getString("model"),
  86. result3.getInt("price"), result3.getFloat("power"), result3.getInt("memory_capacity"),
  87. new URL(result3.getString("URL"))));
  88. }
  89. String query4 = "SELECT * FROM `hdd`";
  90. ResultSet result4 = statement.executeQuery(query4);
  91.  
  92. while (result4.next()) // hdd
  93. {
  94. hdd.add(new HDD(result4.getInt("id"), result4.getString("brand"), result4.getString("model"),
  95. result4.getInt("price"), result4.getFloat("power"), result4.getInt("memory"),
  96. result4.getString("type"), new URL(result4.getString("URL"))));
  97. }
  98.  
  99. String query5 = "SELECT * FROM `mobo`";
  100. ResultSet result5 = statement.executeQuery(query5);
  101.  
  102. while (result5.next()) // mobo
  103. {
  104. mobo.add(new MOBO(result5.getInt("id"), result5.getString("brand"), result5.getString("model"),
  105. result5.getInt("price"), 0, result5.getInt("max_ram"), result5.getString("ddr"),
  106. result5.getString("cpu_socket"), new URL(result5.getString("URL"))));
  107. }
  108.  
  109. String query6 = "SELECT * FROM `psu`";
  110. ResultSet result6 = statement.executeQuery(query6);
  111. while (result6.next()) // psu
  112. {
  113. psu.add(new PSU(result6.getInt("id"), result6.getString("brand"), result6.getString("model"),
  114. result6.getInt("price"), 0, result6.getInt("power_output"), new URL(result6.getString("URL"))));
  115. }
  116. conn.close();
  117.  
  118. /*
  119. * for (CPU a : cpu) { System.out.println(a.getshortDescription()); }
  120. * for (GPU a : gpu) { System.out.println(a.getshortDescription()); }
  121. * for (MOBO a : mobo) { System.out.println(a.getshortDescription()); }
  122. * for (RAM a : ram) { System.out.println(a.getshortDescription()); }
  123. * for (PSU a : psu) { System.out.println(a.getshortDescription()); }
  124. * for (HDD a : hdd) { System.out.println(a.getshortDescription()); }
  125. *
  126. * i = 6;
  127. */
  128.  
  129. return conn;
  130. }
  131. public void sayHello(){
  132. System.out.println("hellooooO!!");
  133. }
  134.  
  135. public Connection addDeleteOperations(Hardware hardware, boolean trueIfAdding, int i) {
  136. try {
  137. String driver = "com.mysql.jdbc.Driver";
  138. String url = "jdbc:mysql://localhost:3306/computer_hw";
  139. String username = "root";
  140. String password = "";
  141. Class.forName(driver);
  142.  
  143. Connection conn = DriverManager.getConnection(url, username, password);
  144. Statement statement = conn.createStatement();
  145. String query="";
  146.  
  147. if(trueIfAdding){
  148. switch(i){
  149. case 1: //mobo
  150. query=
  151. "INSERT INTO `computer_hw`.`mobo` "
  152. + "(`id`, `brand`, `model`, `price`, `max_ram`, `ddr`, `cpu_socket`, `URL`)"
  153. + " VALUES (NULL, '" + hardware.brand + "', '" + hardware.model + "', '"+ hardware.price +
  154. "', '" + ((MOBO)hardware).maxRAM + "', '" + ((MOBO)hardware).DDR +"', '"+ ((MOBO)hardware).CPUSocket +
  155. "', '"+ hardware.url.toString() +"')";
  156.  
  157. break;
  158. case 2: //cpu
  159. //System.out.println("hey");
  160. query=
  161. "INSERT INTO `computer_hw`.`cpu` "
  162. + "(`id`, `brand`, `model`, `price`, `power`, `core`, `socket`, `ghz`, `ddr`, `URL`)"
  163. + " VALUES (NULL, '" + hardware.brand + "', '" + hardware.model + "', '"+ hardware.price +
  164. "', '" + ((CPU)hardware).power + "', '" + ((CPU)hardware).coreCount +"', '"+ ((CPU)hardware).socket +
  165. "', '"+ ((CPU)hardware).ghz +"', '" + ((CPU)hardware).DDR +"', '" + ((CPU)hardware).url.toString() +"')";
  166. //System.out.println("query");
  167. break;
  168. case 3:
  169. query=
  170. "INSERT INTO `computer_hw`.`ram` "
  171. + "(`memory_type`, `clock_speed`, `memory_capacity`, `power`, `price`, `brand`, `model`, `id`, `URL`)"
  172. + " VALUES ('" + ((RAM)hardware).DDR + "', '" + ((RAM)hardware).mhz + "', '"+ ((RAM)hardware).memory +
  173. "', '" + (hardware).power + "', '" + hardware.price +"', '"+ hardware.brand +
  174. "', '"+ hardware.model +"', '" + 0 +"', '" + (hardware).url.toString() +"')";
  175. break;
  176. case 4:
  177. query=
  178. "INSERT INTO `computer_hw`.`gpu` "
  179. + "(`id`, `brand`, `model`, `price`, `power`, `memory_capacity`, `URL`)"
  180. + " VALUES (NULL, '" + hardware.brand + "', '" + hardware.model + "', '"+ hardware.price +
  181. "', '" + (hardware).power + "', '" + ((GPU)hardware).memory +"', '"+ (hardware).url.toString() +"')";
  182. break;
  183. case 5:
  184. System.out.println("heyy");
  185. query=
  186. "INSERT INTO `computer_hw`.`hdd` "
  187. + "(`id`, `brand`, `model`, `price`, `power`, `type`, `memory`, `URL`)"
  188. + " VALUES (NULL, '" + hardware.brand + "', '" + hardware.model + "', '"+ hardware.price + "', '" + (hardware).power
  189. + "', '" + ((HDD)hardware).type + "', '" + ((HDD)hardware).memory +"', '"+ (hardware).url.toString() +"')";
  190. //System.out.println(query);
  191. break;
  192. case 6:
  193. query=
  194. "INSERT INTO `computer_hw`.`psu` "
  195. + "(`id`, `brand`, `model`, `price`, `power_output`, `URL`)"
  196. + " VALUES (NULL, '" + hardware.brand + "', '" + hardware.model + "', '"+ hardware.price +
  197. "', '" + ((PSU)hardware).power_output +"', '"+ (hardware).url.toString() +"')";
  198. //System.out.println(query);
  199. break;
  200. }
  201. System.out.println("hey2");
  202. } else {
  203. switch(i){
  204. case 1:
  205. whatAreWeDeleting = "mobo";
  206. break;
  207. case 2:
  208. whatAreWeDeleting = "cpu";
  209. break;
  210. case 3:
  211. whatAreWeDeleting = "ram";
  212. break;
  213. case 4:
  214. whatAreWeDeleting = "gpu";
  215. break;
  216. case 5:
  217. whatAreWeDeleting = "hdd";
  218. break;
  219. case 6:
  220. whatAreWeDeleting = "psu";
  221. break;
  222. }
  223. query = "DELETE FROM `computer_hw`.`" + whatAreWeDeleting + "`" +
  224. "WHERE `" + whatAreWeDeleting + "`.`model`" + " = '" + hardware.model + "'";
  225. }
  226.  
  227. System.out.println(query);
  228.  
  229. statement.executeUpdate(query);
  230. conn.close();
  231. /*Thread t = new Thread(new Runnable(){
  232.  
  233. @Override
  234. public void run() {
  235. // TODO Auto-generated method stub
  236. try {
  237. Thread.sleep(500);
  238. MainFrame.updateDB();
  239. } catch (InterruptedException e) {
  240. // TODO Auto-generated catch block
  241. e.printStackTrace();
  242. }
  243. }
  244.  
  245. });
  246. t.start();*/
  247. } catch (Exception e) {
  248. System.out.println(e.toString());
  249. }
  250.  
  251. return null;
  252. }
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement