Advertisement
Guest User

Untitled

a guest
Jun 17th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. package Modelo;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.sql.*;
  9. import java.util.Properties;
  10.  
  11. import controlador.Controlador;
  12. import vistas.*;
  13.  
  14. public class bbdd {
  15.  
  16. private Vista_global vistaGlobal;
  17. private Controlador controlador;
  18. private Vista_login vista_login;
  19. private Vista_singin vista_singin;
  20.  
  21. private File config;
  22. private Properties propiedades;
  23. private InputStream entrada = null;
  24.  
  25. private String bd;
  26. private String user;
  27. private String password;
  28. private String ubicacion;
  29. private String driver;
  30. private String url;
  31. private Connection conexion;
  32.  
  33.  
  34. /**
  35. * CONSTRUCTOR: establece el nombre del archivo con los datos de configuración
  36. * Llama a método setDatosAcceso()
  37. * @param archivo: objeto File.
  38. * @throws FileNotFoundException
  39. */
  40. public bbdd(File archivo) throws FileNotFoundException {
  41.  
  42. config = archivo;
  43.  
  44. setDatosAcceso();
  45.  
  46. }
  47.  
  48. /**
  49. * Establece la conexión con la base de datos.
  50. */
  51. public void conexionON(){
  52.  
  53. try {
  54. Class.forName("com.mysql.jdbc.Driver");
  55. conexion = DriverManager.getConnection(url, user, password);
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. // Método failDdbbAccess() lanza mensaje de que algo ha ido mal con la conexión
  59. // y da la posibilidad de modificar los datos del archivo de configuración.
  60. vista_login.failDdbbAccess();
  61. }
  62. }
  63.  
  64. /**
  65. * Cierra la conexión con la base de datos.
  66. */
  67. public void conexionOFF(){
  68. try {
  69. conexion.close();
  70. } catch (SQLException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74.  
  75. /* **************************** */
  76. /* MÉTODO SETTER DE PROPIEDADES */
  77. /* **************************** */
  78.  
  79. public void setVistaGlobal(Vista_global eVistaGlobal) {
  80. vistaGlobal = eVistaGlobal;
  81. }
  82.  
  83. public void setControlador(Controlador eControlador) {
  84. controlador = eControlador;
  85. }
  86.  
  87. public void setVista_login(Vista_login eVista_login) {
  88. vista_login = eVista_login;
  89. }
  90.  
  91. public void setVista_singin(Vista_singin vista_singin) {
  92. this.vista_singin = vista_singin;
  93. }
  94.  
  95. /**
  96. * setDatosAcceso()
  97. * establece los valores de las propiedades de acceso a la bbdd
  98. *
  99. * @throws FileNotFoundException
  100. */
  101. private void setDatosAcceso() throws FileNotFoundException {
  102.  
  103. entrada = new FileInputStream(config);
  104. propiedades = new Properties();
  105. try {
  106. propiedades.load(entrada);
  107. // Establecer valores de las propiedades de acceso a la bbdd.
  108. bd = propiedades.getProperty("bd");
  109. user = propiedades.getProperty("user");
  110. password = propiedades.getProperty("password");
  111. ubicacion = propiedades.getProperty("ubicacion");
  112. driver = propiedades.getProperty("driver");
  113. url = driver + ubicacion + bd;
  114. } catch (IOException e1) {
  115. vista_login.failDdbbAccess();
  116. }
  117. }
  118.  
  119. /* ******************************************** */
  120. /* MÉTODOS RELACIONADOS SELECT, INSERT Y UPDATE */
  121. /* ******************************************** */
  122.  
  123. public void setLogeo(String eEmail, String ePassword) throws SQLException {
  124.  
  125. conexionON();
  126.  
  127. int nIntentos=3;
  128.  
  129. if (eEmail.equals("") || ePassword.equals("")) {
  130. vista_login.showMessage("No debes dejar ningun campo vacío");
  131. } else {
  132. try {
  133. String select = "SELECT * FROM usuarios WHERE email=? AND password=?";
  134. PreparedStatement stmt = conexion.prepareStatement(select);
  135. stmt.setString(1, eEmail);
  136. stmt.setString(2, ePassword);
  137. ResultSet resultados = stmt.executeQuery();
  138.  
  139. resultados.next();
  140. if (ePassword.equals(resultados.getString("password"))) {
  141.  
  142. vista_login.irAlPrograma();
  143. resultados.close();
  144. stmt.close();
  145. } else {
  146. throw new Exception();
  147. }
  148. } catch (Exception e) {
  149. nIntentos--;
  150. if (nIntentos == 0) {
  151. vista_login.cerrarApp();
  152. } else {
  153. vista_login.failLogin(nIntentos);
  154.  
  155. }
  156. }
  157. }
  158. conexionOFF();
  159. }
  160. public void register(String name, String eEmail, String ePassword, String passwordConfirm){
  161.  
  162. conexionON();
  163.  
  164. boolean cojemos;
  165. String marcaEmail;
  166.  
  167. if (name.equals("") || ePassword.equals("") || eEmail.equals("")
  168. || passwordConfirm.equals("")) {
  169. vista_singin.showMessage("Todos los campos son necesarios para completar el registro");
  170.  
  171. } else if (!ePassword.equals(passwordConfirm)) {
  172. vista_singin.showMessage("Las contraseñas deben coincidir para continuar con el registro");
  173. } else {
  174.  
  175. cojemos = false;
  176. marcaEmail = "";
  177. for (int i = 0; i < eEmail.length(); i++) {
  178. if (eEmail.charAt(i) == '@') {
  179. cojemos = true;
  180. }
  181. if (cojemos == true) {
  182. marcaEmail += eEmail.charAt(i);
  183. }
  184. }
  185. if (cojemos == false) {
  186. vista_singin.showMessage("La estructura del email debe ser tunombre@tuservidor.es/com");
  187.  
  188. } else if (marcaEmail.equals("@hotmail.com")
  189. || marcaEmail.equals("@hotmail.es")
  190. || marcaEmail.equals("@gmail.com")
  191. || marcaEmail.equals("@yahoo.com")) {
  192.  
  193. // SI TODO ESTA CORRECTO SE REGISTRA
  194. System.out.println(eEmail);
  195.  
  196. try {
  197.  
  198. String comprobar = "SELECT * FROM inventario.usuario WHERE email=?";
  199. PreparedStatement stmt = conexion.prepareStatement(comprobar);
  200. stmt.setString(1, eEmail);
  201. ResultSet resultados = stmt.executeQuery();
  202.  
  203. resultados.next();
  204. String texto = resultados.getString("email");
  205. vista_singin.showMessage("El email "
  206. + texto
  207. + " ya esta registrado en nuestra base de datos \n Utilize otro E-mail");
  208.  
  209. resultados.close();
  210. stmt.close();
  211.  
  212. } catch (SQLException e1) {
  213.  
  214. // SI SE PRODUCE LA EXCEPCION SE GENERARA EL MENSAJE DE
  215. // REGISTRO; SINO, SE DIRÁ QUE EL EMAIL YA ESTA REGISTRADO
  216. // EN LA BBDD
  217.  
  218. String insercion = ("INSERT INTO inventario.usuario(nombre,password,email) VALUES ('"
  219. + name + "','" + ePassword + "','" + eEmail + "')");
  220. vista_singin.showMessage("El registro se ha completado");
  221. vista_singin.desactivarVista();
  222. vista_login.activarVista(vista_singin.getX(),
  223. vista_singin.getY());
  224.  
  225. try {
  226.  
  227. PreparedStatement stmt2 = conexion.prepareStatement(insercion);
  228. stmt2.executeUpdate();
  229.  
  230. } catch (SQLException e) {
  231.  
  232. vista_singin.showMessage("Fallo al registrarse en la base de datos");
  233.  
  234. }
  235. }
  236. } else {
  237. vista_singin.showMessage("Tu email debe ser de los siguientes dominios \n - hotmail.com \n - hotmail.es \n - gmail.com \n - yahoo.com");
  238.  
  239. }
  240. }
  241. conexionOFF();
  242. }
  243.  
  244. public void getTabla(String tabla) throws SQLException {
  245.  
  246. // Establecemos la conexión con la base de datos.
  247. conexionON();
  248.  
  249. String select = "SELECT * FROM " + tabla;
  250. PreparedStatement stmt = conexion.prepareStatement(select);
  251. ResultSet resultados = stmt.executeQuery();
  252.  
  253. if (tabla.equals("usuario")) {
  254.  
  255. while (resultados.next()) {
  256. int id_usuario = resultados.getInt("id_usuario");
  257. String nombre = resultados.getString("nombre");
  258. String password = resultados.getString("password");
  259. String email = resultados.getString("email");
  260. vistaGlobal.recibir(id_usuario, nombre, password, email);
  261. }
  262.  
  263. } else if (tabla.equals("equipo")) {
  264.  
  265. while (resultados.next()) {
  266. int id = resultados.getInt("id");
  267. String placa_base = resultados.getString("placa_base");
  268. int num_armario = resultados.getInt("num_armario");
  269. vistaGlobal.recibir(id, placa_base, num_armario);
  270. }
  271.  
  272. } else {
  273. // MENSAJE DE ERROR
  274. }
  275. // Cerramos la conexión con la base de datos:
  276. conexionOFF();
  277. }
  278.  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement