//convert string to JSONArray jsonArray = new JSONArray(bufferString); //parse an Object from a random index in the JSONArray JSONObject anObject = jsonArray.getJSONObject(randomNum); Book aBook = new Book(); aBook.setTitle((String) anObject.get("title")); //you can continue to set the different attributes of the Book object using key/value pairs from the Book class (e.g. setPageNum, setChapter, etc).
Book book= books.addNewBook(); book.setAuthor("Fred"); book.setTitle("The Lady and a Little Dog");
// can be used in DAO to convert the object array into List of Objects public List<Book> getList(List<Object[]> bookResult){ List<Book> books = new ArrayList<Book>(); for(Object[] array:bookResult){ Book book= new Book(); book.setTitle(arr[0]); // you need to set the value of each pojo field here. books.add(book); } return books; }
Book tempBook = new Book(); tempBook.setTitle(title); ...
public static void main(String [] args) throws JAXBException { BookLists books = new BookLists(); String [] titles = {"To Kill a Mockingbird", "Twelve Angry Men", "Are You My Mother", "Green Eggs And Ham"}; List<Book> list = new ArrayList<Book>(); for (String title : titles) { Book book = new Book(); book.setTitle(title); list.add(book); } books.setBookslst(list); JAXBContext jc = JAXBContext.newInstance(BookLists.class, Book.class); Marshaller nm = jc.createMarshaller(); nm.setProperty("jaxb.formatted.output", true); nm.marshal(books, System.out); }
public List<Book> getBooks() { List<Book> bookList = new ArrayList(); SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor= db.rawQuery( "select * from "+TABLE_NAME, null); if (cursor.moveToFirst()) { do { Book book = new Book(); book.setId(cursor.getString(cursor.getColumnIndex("id"))); book.setTitle(cursor.getString(cursor.getColumnIndex("title"))); book.setDescription(cursor.getString(cursor.getColumnIndex("description"))); bookList.add(book); } while (cursor.moveToNext()); } return bookList; }
Book b = new Book(null,0); books.add(b); b.setRefNum(Integer.parseInt(line)); String bookTitle = br.readLine(); b.setTitle(bookTitle);
public List<Book> getBooks() { List<Book> bookList = new ArrayList<Book>(); SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor= db.rawQuery( "select * from tblbooks", null ); if (cursor.moveToFirst()) { // change here do { Book book = new Book(); // need to add here book.setId(cursor.getString(cursor.getColumnIndex("id"))); book.setTitle(cursor.getString(cursor.getColumnIndex("title"))); book.setDescription(cursor.getString(cursor.getColumnIndex("description"))); bookList.add(book); } while (cursor.moveToNext()); cursor.close(); } } return bookList; }
public Book findBook(int isbn) throws SQLException, ClassNotFoundException{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection(url, user, password); String find = "SELECT * from boo WHERE isbn = ?"; PreparedStatement stt = con.prepareStatement(find); stt.setInt(1, isbn); ResultSet rs = stt.executeQuery(); Book b2 = new Book(); while (rs.next()){ b2.setIsbn(rs.getInt("isbn"); b2.setTitle(rs.getString("title"); } // If more than one entry (row) is returned from the query, // b2 will only represent the last one. // Close these in reverse order of creation rs.close(); stt.close(); con.close(); return b2; }
@Override public Book deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final JsonObject jsonObject = json.getAsJsonObject(); final JsonElement jsonTitle = jsonObject.get("title"); final String title = jsonTitle.getAsString(); final String isbn10 = jsonObject.get("isbn-10").getAsString(); final String isbn13 = jsonObject.get("isbn-13").getAsString(); final JsonArray jsonAuthorsArray = jsonObject.get("authors").getAsJsonArray(); final String[] authors = new String[jsonAuthorsArray.size()]; for (int i = 0; i < authors.length; i++) { final JsonElement jsonAuthor = jsonAuthorsArray.get(i); authors[i] = jsonAuthor.getAsString(); } final Book book = new Book(); book.setTitle(title); book.setIsbn10(isbn10); book.setIsbn13(isbn13); book.setAuthors(authors); return book; } }
public static void main(String[] args) { Class myClass2 = null; try { myClass2 = Class.forName("Book"); } catch (ClassNotFoundException e) { System.out.println("Error: " + e); } if (myClass2 != null) { try { //Creating an instance of the Book class Book book1 = (Book) myClass2.newInstance(); book1.setAuthor("Khan"); System.out.println(book1.getAuthor()); book1.setTitle("Second Book"); book1.setIsbn("kh_s_b"); book1.printBookDetails(); } catch (IllegalAccessException e1) { System.out.println("Error1 " + e1); } catch (InstantiationException e2) { System.out.println("Error2 " + e2); } } }//main method ends here.
List<Book> list = new ArrayList<Book>(); // populate list with the results got from database while(this.results.next()) { Book book = new Book(); book.setBookID(this.results.getInt("bookID")); book.setTitle(this.results.getString("title")); book.setAuthor(this.results.getString("author")); book.setPages(this.results.getInt("pages")); list.add(book); } // set the List of Book as request attribute request.setAttribute("list",list); // forward the request to the JSP page RequestDispatcher view = request.getRequestDispatcher("view.jsp"); view.forward(request, response);
if (cursor.moveToFirst()) { do { Book book = new Book();//here book.setId(cursor.getString(cursor.getColumnIndex("id"))); book.setTitle(cursor.getString(cursor.getColumnIndex("title"))); book.setDescription(cursor.getString(cursor.getColumnIndex("description"))); bookList.add(book); } while (cursor.moveToNext()); }
public Book(String bookAuthor, String bookTitle) { author = bookAuthor; title = bookTitle; generatebook2(); } public Book(){ } public void generatebook2(){ Book book2 = new Book(); book2.setAuthor("Jumbo"); book2.setTitle("Fary"); }
@GET public Collection<Book> getBooks() { List<Book> outBooks = new ArrayList<>(); for (Book book : bookRepo.getBooks()) { Book outBook = new Book(); outBook.setId(book.getId()); outBook.setTitle(book.getTitle()); outBook.setCreator(book.getCreator()); outBooks.add(outBook); } return outBooks; }
byte[] bin = some binary data... Book b = new Book(); b.setAuthor("John"); b.setTitle("wild stuff"); b.setYear(2012); MultiPart multiPart = new MultiPart(); multiPart.bodyPart(new BodyPart(b, MediaType.APPLICATION_XML_TYPE)); multiPart.bodyPart(new BodyPart(bin, MediaType.APPLICATION_OCTET_STREAM_TYPE)); response = service.path("rest").path("multipart"). type(MultiPartMediaTypes.MULTIPART_MIXED). post(ClientResponse.class, multiPart);
public Book parse(Document doc) throws XPathExpressionException, URISyntaxException { Book book = new Book(); String id = xpath.evaluate("//pgterms:ebook/@rdf:about", doc); book.setId(id); book.setTitle(xpath.evaluate("//dcterms:title/text()", doc)); book.setCreator(xpath.evaluate("//dcterms:creator//pgterms:name/text()", doc)); NodeList files = (NodeList)xpath.evaluate("//pgterms:file", doc, XPathConstants.NODESET); //NodeList subjects = (NodeList)xpath.evaluate("//dcterms:subject/rdf:Description/rdf:value", doc, XPathConstants.NODESET); for (int c = 0; c < files.getLength(); c++) { Format format = parseFormat((Element)files.item(c)); if (format.getMediaType().startsWith("image")) { book.setCover(format.getFile()); } book.getFormats().add(format); } /* for (int c = 0; c < subjects.getLength(); c++) { Element subjectEl = (Element)subjects.item(c); book.getSubjects().add(new Subject(subjectEl.getTextContent())); } */ return book; }
// create manager and two example books SimpleBookManager manager = new SimpleBookManager(); Book harryPotter1 = manager.createBook(); Book harryPotter2 = manager.createBook(); harryPotter1.setAuthor("JK Rowling"); harryPotter1.setCourse("Harry Potter Kunskap"); harryPotter1.setPrice(199); harryPotter1.setTitle("Harry Potter and the philosifer Stone"); harryPotter1.setIsbn("9780590353403"); harryPotter2.setAuthor("JK Rowling"); harryPotter2.setCourse("Harry Potter Kunskap"); harryPotter2.setPrice(299); harryPotter2.setTitle("Harry Potter and snake"); harryPotter2.setIsbn("0439064872"); // let's use an intent to parcel the manager to Intent intent = new Intent(); intent.putExtra("extra_book_manager", manager); // read the parcelled manager back from the intent SimpleBookManager parcelledManager = intent.getParcelableExtra("extra_book_manager");