Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Connection conexion = null; //Objeto para la conexión a la BD
- Statement sentencia = null; //Objeto para la ejecutar una sentencia
- ResultSet resultados = null;//Objeto para guardar los resultados
- try {
- Class.forName("com.mysql.jdbc.Driver");
- DriverManager.registerDriver(new com.mysql.jdbc.Driver());
- System.out.println("Conectando...");
- conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/biblioteca?user=root&password=nerea");
- System.out.println("CONECTADO.");
- sentencia = conexion.createStatement();
- resultados = sentencia.executeQuery("SELECT ID, Categoria FROM `biblioteca`.`usuarios` WHERE Nombre='" + nombre + "' AND Apellido='" + apellido + "'");
- while (resultados.next())
- {
- ID= resultados.getString("ID");
- }
- } catch (SQLException e) {
- throw new ServletException("Servlet Could not display records.", e);
- } catch (ClassNotFoundException e) {
- throw new ServletException("JDBC Driver not found.", e);
- } finally {
- // it is a good idea to release
- // resources in a finally{} block
- // in reverse-order of their creation
- // if they are no-longer needed
- if (resultados != null) {
- try {
- resultados.close();
- } catch (SQLException sqlEx) { } // ignore
- resultados = null;
- }
- if (sentencia != null) {
- try {
- sentencia.close();
- } catch (SQLException sqlEx) { } // ignore
- sentencia = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement