Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. public class Conectate {
  2. private String driver ="com.mysql.jdbc.Driver";
  3. private String cadenaConexion ="jdbc:mysql://localhost/XboxOne";
  4. private String pass = "";
  5. private String usuario = "root";
  6. public Connection con;
  7.  
  8. //public Conectate(Map<String, Map<String, Item>> gamesByCountry, Map<String, String> codesByTitle,Map<String, String> countries) {
  9. public Conectate(ArrayList<Item> games) {
  10.  
  11. try {
  12. Class.forName(driver);
  13. con = DriverManager.getConnection(cadenaConexion, usuario, pass);
  14. System.out.println("¡Conectado!");
  15.  
  16.  
  17. //CREAMOS LA TABLA
  18. Statement st = con.createStatement();
  19.  
  20. st.executeUpdate("CREATE TABLE IF NOT EXISTS info_XboxOne (id INT AUTO_INCREMENT, PRIMARY KEY(id), "
  21. + "Juego_vinculado VARCHAR(500), Juego VARCHAR(500), Tipologia VARCHAR (500), Pertenece VARCHAR (500), "
  22. + "Nota VARCHAR (10), Descripcion_Ingles TEXT(4000), Descripcion_Castellano TEXT(4000), Pegi VARCHAR(10), Descripcion_Pegi VARCHAR(200),"
  23. + "Lanzamiento VARCHAR (50))");
  24.  
  25. System.out.println( "Tabla creada!");
  26.  
  27.  
  28. for (Item game : games) {
  29. String titulo = game.getName();
  30.  
  31. boolean isInsert;
  32. try (PreparedStatement ps = con.prepareStatement("SELECT * FROM info_XboxOne WHERE juego = ?")) {
  33. ps.setString(1, titulo);
  34.  
  35. try (ResultSet rs = ps.executeQuery()) {
  36. isInsert = !rs.next();
  37. }
  38. }
  39.  
  40. if (isInsert) { //si se cumple esta condicción significa que el juego no está incluido, con lo que lo metemos
  41. try(PreparedStatement ps = con.prepareStatement("INSERT INTO info_XboxOne (Juego, Tipologia, Pertenece, "
  42. + "Nota, Descripcion_Ingles, Descripcion_Castellano, Pegi, Descripcion_Pegi"
  43. + ") VALUES (?,?,?,?,?,?,?,?)")) {
  44.  
  45. ps.setString(1,titulo);
  46. ps.setString(2,game.getValues().get(Constants.TIPOLOGIA));
  47. ps.setString(3,game.getValues().get(Constants.PERTENECE));
  48. ps.setString(4,game.getValues().get(Constants.NOTA));
  49. ps.setString(5,game.getValues().get(Constants.DESCRIPCION_INGLES));
  50. ps.setString(6,game.getValues().get(Constants.DESCRIPCION_CASTELLANO));
  51. ps.setString(7,game.getValues().get(Constants.PEGI));
  52. ps.setString(8,game.getValues().get(Constants.DESCRIPCION_PEGI));
  53.  
  54. ps.executeUpdate();
  55. }
  56. } else {
  57. String query = "UPDATE info_XboxOne SET Tipologia = ?, Pertenece = ?, "
  58. + "Nota = ?, Descripcion_Ingles = ?, Descripcion_Castellano = ?, "
  59. + "Pegi = ?, Descripcion_Pegi = ? WHERE juego = ?";
  60.  
  61. try (PreparedStatement ps = con.prepareStatement(query)) {
  62. ps.setString(1,game.getValues().get(Constants.TIPOLOGIA));
  63. ps.setString(2,game.getValues().get(Constants.PERTENECE));
  64. ps.setString(3,game.getValues().get(Constants.NOTA));
  65. ps.setString(4,game.getValues().get(Constants.DESCRIPCION_INGLES));
  66. ps.setString(5,game.getValues().get(Constants.DESCRIPCION_CASTELLANO));
  67. ps.setString(6,game.getValues().get(Constants.PEGI));
  68. ps.setString(7,game.getValues().get(Constants.DESCRIPCION_PEGI));
  69. ps.setString(8,titulo);
  70.  
  71. ps.executeUpdate();
  72. }
  73. }
  74. }
  75.  
  76. /*
  77. for (Item game : games) {
  78.  
  79.  
  80. String titulo = game.getName();
  81.  
  82. ResultSet rs = st.executeQuery("SELECT * FROM info_XboxOne WHERE juego = "" + titulo + """);
  83.  
  84. if (!rs.next()) { //si se cumple esta condicción significa que el juego no está incluido, con lo que lo metemos
  85. PreparedStatement ps = con.prepareStatement("INSERT INTO info_XboxOne (Juego, Tipologia, Pertenece, "
  86. + "Nota, Descripcion_Ingles, Descripcion_Castellano, Pegi, Descripcion_Pegi"
  87. + ") VALUES (?,?,?,?"
  88. + ",?,?,?,?"
  89. + ")");
  90.  
  91. ps.setString(1,game.getName());
  92. ps.setString(2,game.getValues().get(Constants.TIPOLOGIA));
  93. ps.setString(3,game.getValues().get(Constants.PERTENECE));
  94. ps.setString(4,game.getValues().get(Constants.NOTA));
  95. ps.setString(5,game.getValues().get(Constants.DESCRIPCION_INGLES));
  96. ps.setString(6,game.getValues().get(Constants.DESCRIPCION_CASTELLANO));
  97. ps.setString(7,game.getValues().get(Constants.PEGI));
  98. ps.setString(8,game.getValues().get(Constants.DESCRIPCION_PEGI));
  99.  
  100. ps.executeUpdate();
  101.  
  102. } else {
  103. PreparedStatement ps = con.prepareStatement("UPDATE info_XboxOne SET Tipologia = ?, Pertenece = ?, Nota = ?, Descripcion_Ingles = ?, Descripcion_Castellano = ?, Pegi = ?, Descripcion_Pegi = ? WHERE juego = "" + titulo + """);
  104.  
  105. ps.setString(1,game.getValues().get(Constants.TIPOLOGIA));
  106. ps.setString(2,game.getValues().get(Constants.PERTENECE));
  107. ps.setString(3,game.getValues().get(Constants.NOTA));
  108. ps.setString(4,game.getValues().get(Constants.DESCRIPCION_INGLES));
  109. ps.setString(5,game.getValues().get(Constants.DESCRIPCION_CASTELLANO));
  110. ps.setString(6,game.getValues().get(Constants.PEGI));
  111. ps.setString(7,game.getValues().get(Constants.DESCRIPCION_PEGI));
  112.  
  113. ps.executeUpdate();
  114. }
  115. }
  116. */
  117.  
  118. } catch (Exception e) {
  119. JOptionPane.showMessageDialog(null, "No se ha podido establecer la conexión con la DB" + e);
  120. e.printStackTrace();
  121. }
  122.  
  123. }
  124.  
  125. public String ConvertirObjectToString(Object Obj) {
  126. String Str="";
  127. if(Obj!=null){
  128. Str = Obj.toString();
  129. }
  130. return Str;
  131. }
  132.  
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement