Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 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. } catch (Exception e) {
  78. JOptionPane.showMessageDialog(null, "No se ha podido establecer la conexión con la DB" + e);
  79. e.printStackTrace();
  80. }
  81.  
  82. }
  83.  
  84. public String ConvertirObjectToString(Object Obj) {
  85. String Str="";
  86. if(Obj!=null){
  87. Str = Obj.toString();
  88. }
  89. return Str;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement