package ad2_practica2_ejercicio1; import java.sql.*; public class Database { protected Connection connection; protected Statement query; public Database() throws ClassNotFoundException, SQLException{ Class.forName("org.sqlite.JDBC"); connection = DriverManager.getConnection("jdbc:sqlite:db.sqlite"); query = null; } } package ad2_practica2_ejercicio1; import java.util.ArrayList; import java.sql.*; public class DBLibro extends Database { public DBLibro() throws ClassNotFoundException, SQLException { super(); } public void Insertar(Libro l) throws Exception { try { query = connection.createStatement(); String sql = "INSERT INTO LIBRO (ISBN,TITULO,AUTOR,NUMEJEMPLARES,ANYOPUBLICACION,EDITORIAL,NUMPAG) " + "VALUES (" + l.getIsbn() + ", '" + l.getTitulo() + "','"+ l.getAutor()+ "',"+ l.getNumejemplares() + ","+ l.getAnyopublicacion()+",'" + l.getEditorial() + "'," + l.getNumpag() + ");"; query.executeUpdate(sql); query.close(); connection.close(); } catch(Exception e) { //System.out.println("Error,\n" + e.getMessage()); throw new Exception("Error, intentando registrar un libro."); } } public Libro LeerLibro(int isbn) throws Exception { try { query = connection.createStatement(); ResultSet rs = query.executeQuery("SELECT * FROM LIBRO WHERE ISBN=" + isbn + ";"); Libro l = null; if ( rs.next() ) { l = new Libro(); l.setIsbn(rs.getInt("isbn")); l.setTitulo(rs.getString("titulo")); l.setAutor(rs.getString("autor")); l.setNumejemplares(rs.getInt("numejemplares")); l.setAnyopublicacion(rs.getInt("anyopublicacion")); l.setEditorial(rs.getString("editorial")); l.setNumpag(rs.getInt("numpag")); } rs.close(); query.close(); connection.close(); return l; } catch(Exception e) { throw new Exception("Error, intentando lee el libro."); } } public ArrayListLeer() throws Exception { ArrayList libros = new ArrayList (); try { query = connection.createStatement(); ResultSet rs = query.executeQuery("SELECT * FROM LIBRO;"); Libro l = null; while ( rs.next() ) { l = new Libro(); l.setIsbn(rs.getInt("isbn")); l.setTitulo(rs.getString("titulo")); l.setAutor(rs.getString("autor")); l.setNumejemplares(rs.getInt("numejemplares")); l.setAnyopublicacion(rs.getInt("anyopublicacion")); l.setEditorial(rs.getString("editorial")); l.setNumpag(rs.getInt("numpag")); libros.add(l); } rs.close(); query.close(); connection.close(); return libros; } catch(Exception e) { throw new Exception("Error, intentando leer los libros."); } } public void Eliminar(int isbn) throws Exception { try { query = connection.createStatement(); String sql = "DELETE from LIBRO where ISBN="+isbn+";"; query.executeUpdate(sql); query.close(); connection.close(); } catch ( Exception e ) { throw new Exception("Error, intentando eliminar el libro."); } } }
Friday, December 13, 2013
Sqlite
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment