在文本框中显示数据库列值 [英] Display database column value in a text box
问题描述
嘿朋友,
我正在尝试使用jsp,servlet,java和mysql进行在线图书馆系统。在我的系统中,通过文本框输入书籍ID并搜索书籍,然后在下一个文本框中显示该框的名称。我的问题是如何在文本框中显示db列值。值传递给jsp-> servlet-> service-> dao,反之亦然。但没有在第二个文本框中显示名称。
这里是我的系统代码。
Hey friends,
I'm trying to do a online library system using jsp, servlet, java and mysql. In my system im tring to enter a book id through a text box and search the book then display the name of the box in next text box.my problem is how to display a db column value in a text box. values are passing to jsp->servlet->service->dao and vice versa. but did not display the name in the 2nd text box.
here my code of my system.
<div id="page">
<form name="input" action="book" method="post">
Book Id : <input type="text" name="txtBid" class="resizedTextbox"><br><br>
<input type="submit" value="Search Book" onclick= <a href="main/Servelet"</a>
<input type="hidden" name="operation" value="searchBookbtn"><br><br>
Book Name : <input type="text" name="txtName" class="resizedTextbox"><br><br>
<input type="submit" value="Delete Book" onclick= <a href="main/Servelet"</a>
<input type="hidden" name="operation" value="deleteBookbtn">
<input type="submit" value="Cancel/Clear" onclick= <a href="main/Servelet"</a>
<p><%=request.getAttribute("deleteBookMsg") %></p>
</form>
<br><br><br><br><br><br><br><br><br><br><br>
</div>
< br $>
servlet
servlet
private void searchBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
System.out.println("Inside searchBook");
book b =new book();
bookService bs = new bookServiceImpl();
String b_id=request.getParameter("txtBid");
String b_name=request.getParameter("txtName");
b.setB_id(b_id);
b.setB_title(b_name);
System.out.println(b_id);
if(bs.searchbook(b)){
String message = "Click Delete button to delete the book detail";
request.setAttribute("deleteBookMsg", message);
request.setAttribute("txtName",b);
}else{
String message = "Book does not Exist";
request.setAttribute("deleteBookMsg", message);
}
RequestDispatcher rd = getServletContext().getRequestDispatcher("/deleteBook.jsp");
rd.forward(request, response);
}
服务层
service layer
public boolean searchbook(book b) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
System.out.println("Inside bs.searchbook");
boolean book_result=true;
bookDAO bd = new bookDAOImpl();
System.out.println("===Inside bs.searchbook");
book bk =bd.search(b);
if(bk==null){
System.out.println("....");
System.out.println(book_result);
book_result=false;
}return book_result;
}
dao
dao
public book search(book b) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
System.out.println("Inside book search");
book b_result = null;
Connection conn= null;
PreparedStatement ptmt= null;
ResultSet rset = null;
try{
conn= getConnection();
String queryString = "SELECT * FROM book WHERE b_id=?";
ptmt = conn.prepareStatement(queryString);
ptmt.setString(1, b.getB_id());
System.out.println(b.getB_id());
rset = ptmt.executeQuery();
System.out.println(rset.first());
if (rset.first()){
System.out.println(rset.first());
b_result = new book();
b_result.setB_title(rset.getString(String.valueOf("b_title")));
b_result.setB_suplier(rset.getString(String.valueOf("b_supplier")));
b_result.setB_publisher(rset.getString(String.valueOf("b_publisher")));
return b_result;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
finally {
try {
ptmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}System.out.println("======");
System.out.println(b_result);
return b_result;
}
推荐答案
当你的servlet返回到jsp时,你需要在jsp中引用txtName请求属性,例如:
you need to reference the txtName request attribute in the jsp when your servlet returns to that jsp e.g.
<input type="text" name="txtName" value="<%=request.getAttribute('txtName')%>" class="resizedTextbox">
只要确保你处理案件wh当首次加载jsp时,即在调用servlet之前,该属性中的值为null。比使用这些java scriptlet更好的解决方案是使用taglib库。 GoogleJSLT核心代码用于示例和教程。
Just make sure you handle the case where the value in that attribute is null when the jsp is first loaded i.e. before the servlet is invoked. A better solution than using these java scriptlets is to use a taglib library. Google "JSLT core tags" for examples and tutorials.
我创建了一个简化的项目版本,并将eclipse项目压缩到此URL:http://www.filedropper.com/librarysearch [ ^ ]。以此为基础并添加您的搜索等。您的代码中存在许多问题,例如onclick处理程序不能有标签。我建议你使用struts或spring mvc,因为使用jsps和servlet不是最好的解决方案
I've created a simplied version of your project and zipped the eclipse project to this URL: http://www.filedropper.com/librarysearch[^]. Use this as a base and add your search etc. There are many problems in your code e.g. the onclick handler cannot have tags. I would suggest you use struts or spring mvc as using jsps and servlets is not the best solution
这篇关于在文本框中显示数据库列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!