Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1.         Connection conexion = null; //Objeto para la conexión a la BD
  2.         Statement sentencia = null; //Objeto para la ejecutar una sentencia
  3.         ResultSet resultados = null;//Objeto para guardar los resultados
  4.  
  5.  
  6.        
  7.         try {
  8.            
  9.             Class.forName("com.mysql.jdbc.Driver");
  10.             DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  11.             System.out.println("Conectando...");
  12.             conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/biblioteca?user=root&password=nerea");
  13.             System.out.println("CONECTADO.");
  14.             sentencia = conexion.createStatement();
  15.             resultados = sentencia.executeQuery("SELECT ID, Categoria FROM `biblioteca`.`usuarios` WHERE Nombre='" + nombre + "' AND Apellido='" + apellido + "'");
  16.             while (resultados.next())
  17.             {
  18.                 ID= resultados.getString("ID");
  19.                
  20.             }
  21.            
  22.      
  23.                    
  24.         } catch (SQLException e) {
  25.             throw new ServletException("Servlet Could not display records.", e);
  26.          } catch (ClassNotFoundException e) {
  27.                 throw new ServletException("JDBC Driver not found.", e);
  28.          } finally {
  29.             // it is a good idea to release
  30.             // resources in a finally{} block
  31.             // in reverse-order of their creation
  32.             // if they are no-longer needed
  33.              
  34.             if (resultados != null) {
  35.                 try {
  36.                     resultados.close();
  37.                 } catch (SQLException sqlEx) { } // ignore
  38.  
  39.                 resultados = null;
  40.             }
  41.  
  42.             if (sentencia != null) {
  43.                 try {
  44.                     sentencia.close();
  45.                 } catch (SQLException sqlEx) { } // ignore
  46.  
  47.                 sentencia = null;
  48.             }
  49.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement