Advertisement
Guest User

pvz

a guest
Jun 20th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. public static ArrayList<Book> bookList;
  2.  
  3. public static void getBooks() throws SQLException {
  4.     ArrayList<Book> list=new ArrayList<Book>();
  5.     con = DriverManager.getConnection("jdbc:sqlite://"+dbFileLocation);
  6.     stmt = con.createStatement();
  7.     rs = stmt.executeQuery("SELECT * FROM books");
  8.     while (rs.next()) {
  9.         list.add(new Book(rs.getInt("BookId"),
  10.         rs.getString("Name"),
  11.         rs.getInt("Year"),
  12.         rs.getInt("AuthorId"),
  13.         rs.getBoolean("CheckedOut")));
  14.     }
  15.     bookList=list;
  16. }  
  17.  
  18. public static void insertBook(String name, int authorId, int year) {
  19.     try {
  20.         con = DriverManager.getConnection("jdbc:sqlite://"+dbFileLocation);
  21.         PreparedStatement stmt=con.prepareStatement("INSERT INTO books(Name, AuthorId, Year) VALUES (?,?,?)");
  22.         stmt.setString(1, name);
  23.         stmt.setInt(2, authorId);
  24.         stmt.setInt(3, year);
  25.         stmt.executeUpdate();
  26.         getBooks();
  27.     }catch (SQLException e) {
  28.         e.printStackTrace();
  29.     }finally {
  30.         try {
  31.             stmt.close();
  32.             con.close();
  33.         }catch (SQLException e) {
  34.             e.printStackTrace();
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement