JSP和MySQL-在数据库中查找特定项目 [英] JSP & Mysql - finding a specific item in database

查看:175
本文介绍了JSP和MySQL-在数据库中查找特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一种 find 方法,该方法将在mysql数据库中搜索带有我在JSP文本字段中输入的isbn的图书.我的问题是,如何正确地在ManagerBook.java类中实现 find 方法,以及如何通过调用来显示在同一JSP页面上找到的书(如果该书在db中找到). find 方法.看看到目前为止我写的代码:

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

<%@page import="java.util.*, book.*" %>

    <jsp:useBean id = "bm" class="book.ManagerBook" scope = "session"/> 

    <%
        int success = 0;

    Boolean submitted = Boolean.parseBoolean(request.getParameter("submitted"));
    if(submitted){
        int isbn = Integer.parseInt(request.getParameter("isbn"));      
        success = bm.findBook(isbn);
    }
    %>

    <h1> Welcome to ABC Library</h1>

    <form>
    <table>
        <tr>
            <td> Enter Details </td>
            <td><input type="text" name="isbn"></td>
            <td><input type="submit" name="find" value="find"></td>
    </tr>
    </table>
        <input type="hidden" name="submitted" value="true">
</form> 

        <%
        if((success == 1) && (submitted)){%> <h3> The book is found</h3> <br>


        <table>
    <tr>
    <td colspan=2>
    <h2>Book Found</h2>
    </td>
    </tr>

    <tr>
        <td><h3>ISBN</h3></td>
        <td><h3>Title</h3></td>
    </tr>   

    <tr>
        <td><%=bm.getIsbn() %></td>
        <td><%=bm.getTitle() %></td>
    </tr>
        </table>    

    <%} else if (submitted){ %>
        <h3> Book not found</h3>
        <% } %>

感谢&亲切的问候..:)

Thanks & Kind Regards.. :)

推荐答案

您的findBook方法的返回类型为int,但实际上返回的是Book类型的对象.因此它将无法编译.

Your findBook method has return type as int, but it is actually returning an object of type Book. So it won't compile.

您可以在ManagerBook类中声明Book类型的实例变量,例如

You can declare an instance variable of type Book in ManagerBook class, say

Book searchedBook;

现在在您的findBook方法中,将此变量值设置为SQL查询返回的书,并返回一个int值1.

Now in your findBook method, set this variable value to the book returned by your SQL query and return an int value 1.

在JSP中,您可以使用:

In JSP, you can use :

    <tr>
        <td><%=bm.getSearchedBook().getIsbn() %></td>
        <td><%=bm.getSearchedBook().getTitle() %></td>
    </tr>

这篇关于JSP和MySQL-在数据库中查找特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆