Guest User

Untitled

a guest
May 23rd, 2016
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. package Cheke;
  2.  
  3. import java.sql.*;
  4. import java.io.*;
  5. /**
  6. * Esta clase es la que se comunica con la BBDD y la que lanza y recoge las
  7. * sentencias de SQL.
  8. * @author Cheke
  9. * @version 0.1a
  10. */
  11. public class ORM {
  12.  
  13. //Variables necesarias para poder establecer la conexión con la BBDD.
  14. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  15. static final String DB_URL = "jdbc:mysql://localhost/animeome";
  16. static final String USER = "root";
  17. static final String PASS = "";
  18. //Abre una conexión.
  19. static Connection con = null;
  20. //Crea un statement.
  21. static Statement stm = null;
  22. //Set para recoger los datos de la consulta.
  23. static ResultSet rs = null;
  24. //Nos servirá para escribir en un log en caso de fallo.
  25. static PrintWriter log = null;
  26.  
  27. /**
  28. * Método para conectar con la BBDD.
  29. */
  30. public void conectar() {
  31. try {
  32. log = new PrintWriter(
  33. new BufferedWriter(
  34. new FileWriter("conection.ao")
  35. )
  36. );
  37. Class.forName(JDBC_DRIVER);
  38. System.out.println("Conectando a la base de datos...");
  39. con = DriverManager.getConnection(DB_URL, USER, PASS);
  40. System.out.println("¡Conectado correctamente!");
  41. System.out.println("Creando sentencia...");
  42. stm = con.createStatement();
  43. } catch (ClassNotFoundException | SQLException | IOException ex) {
  44. ex.printStackTrace(log);
  45. } finally {
  46. if (log != null) {
  47. log.close();
  48. }
  49. /* try { if (rs != null) rs.close(); } catch (Exception ex) {}
  50. try { if (stm != null) stm.close(); } catch (Exception ex) {}
  51. try { if (con != null) con.close(); } catch (Exception ex) {}*/
  52. }
  53. }
  54. public static int insert(String sql) throws SQLException {
  55. int resultado;
  56. resultado = stm.executeUpdate(sql);
  57. if (resultado == 0) {
  58. return -1;
  59. } else {
  60. return resultado;
  61. }
  62. }
  63. }
Add Comment
Please, Sign In to add comment