I am trying to implement a method find that will search a book in the mysql database bearing the isbn I have entered in the the text field of JSP. My issue is that how do I correctly implement the find method in the ManagerBook.java class and how do I display the book found on the same JSP page (if the book is found in the db) by calling the find method. Have a look at my codes written so far:
ManagerBook.java
public int 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 = ?";
Statement stt = con.createStatement();
ResultSet rs = stt.executeQuery(find);
while(rs.next()){
int isbn1 = rs.getInt("isbn");
String title1 = rs.getString("title");
Book b2 = new Book();
b2.setIsbn(isbn1);
b2.setTitle(title1);
}
con.close();
stt.close();
rs.close();
return b2;
}
Book.java
package book;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Book {
private int isbn;
private String title;
@Id
public int getIsbn() {
return isbn;
}
public void setIsbn(int isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
find.jsp
int success = 0;
Boolean submitted = Boolean.parseBoolean(request.getParameter("submitted"));
if(submitted){
int isbn = Integer.parseInt(request.getParameter("isbn"));
success = bm.findBook(isbn);
}
%>
Welcome to ABC Library
Enter Details |
if((success == 1) && (submitted)){%>
The book is found
Book Found | |
ISBN | Title |
Book not found
Thanks & Kind Regards.. :)
解决方案
Your findBook method has return type as int, but it is actually returning an object of type Book. So it won't compile.
You can declare an instance variable of type Book in ManagerBook class, say
Book searchedBook;
Now in your findBook method, set this variable value to the book returned by your SQL query and return an int value 1.
In JSP, you can use :