jsp和mysql查询库信息,JSP和MySQL-在数据库中查找特定项目

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 :

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值