您的位置:首页 > 数据库

jsp直接显示数据库表格信息

2017-10-04 21:57 671 查看
一个非常简单的jsp输出数据库的内容
一开始看书上想使用action的方法
后来发现action无法触发
对于后端java根本无法调用
后来发现只在jsp上直接调用操作就能输出了
数据库mysql与java链接后作为resultset参数传入java

<%@page import="JavaBean.MyFriBean" %>
<%@page import="DBJavaBean.DB" %>
<%@page import="java.sql.*" %>
<%@page import= "java.util.ArrayList" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@ page language ="java" contentType="text/html;charset=GB18030" pageEncoding="GB18030" %>


<article class="col-sm-12 col-md-12 col-lg-10">

<!-- Widget ID (each widget will need unique ID)-->
<div class="jarviswidget jarviswidget-color-greenLight" id="wid-id-3" data-widget-editbutton="false">

<header>
<span class="widget-icon"> <i class="fa fa-table"></i> </span>
<h2>Book Information</h2>

</header>

<!-- widget div-->
<div>

<!-- widget edit box -->
<div class="jarviswidget-editbox">
<!-- This area used as dropdown edit box -->

</div>
<!-- end widget edit box -->

<form action="lookFriends.jsp" method ="post">
<input type="text" name="friendname"/>
<input type="submit" value="查找"/>
</form>

<!-- widget content -->
<div class="widget-body no-padding">

<table class="table table-bordered">
<thead>
<tr>
<th>Book Number</th>
<th> ISBN(PK)</th>
<th> Title</th>
<th> Author(ID)</th>
<th> Publisher</th>
<th> PublishDate</th>
<th> Price</th>
</tr>
</thead>
<tbody>
<%
DB mysql = new DB();
String userName = mysql.returnLogin(request);

ResultSet rs = mysql.selectFriAll(request, userName);
String fri = mysql.myFriends(request,userName);
ArrayList friends= (ArrayList)session.getAttribute("friends");
System.out.println("1");
if(friends == null|| friends.size() == 0){
%>
<h1>书籍库中未有书籍</h1>
<%
}else{
for(int i=friends.size()-1;i>=0;i--)
{
MyFriBean ff =(MyFriBean)friends.get(i);

%>

<tr>
<th><%=ff.getName()%></th>
<th><%=ff.getISBN()%></th>
<th><%=ff.getName()%></th>
<th><%=ff.getauthor() %></th>
<th><%=ff.getpublisher()%></th>
<th><%=ff.getdate()%></th>
<th><%=ff.getprice()%></th>
</tr>
<%
}
}
%>

</tbody>
</table>

</div>
<!-- end widget content -->

</div>
<!-- end widget div -->

</div>
<!-- end widget -->
</article>


后端java

public String myFriends(HttpServletRequest request,String userName)
{
try{
ArrayList listName =null;
HttpSession session =request.getSession();
listName=new ArrayList();
rs=selectFriAll(request,userName);
if(rs.next())
{
rs=selectFriAll(request,userName);
while(rs.next())
{
MyFriBean mess = new MyFriBean();
mess.setName(rs.getString("BookName"));
mess.setISBN(rs.getString("ISBN"));
mess.setpublisher(rs.getString("Publisher"));
mess.setauthor(rs.getString("Author(ID)"));
mess.setdate(rs.getString("Date"));
mess.setprice(rs.getString("Price"));
//
listName.add(mess);
//System.out.println("2");
session.setAttribute("friends",listName);
}
}
else
{
session.setAttribute("friends", listName);
}
return "ok";

}catch(Exception e)
{
e.printStackTrace();
return null;
}

}
//获取全部书籍信息
public ResultSet selectFriAll(HttpServletRequest request,String userName)
{
try{
//String sql="select * from bookinformation where userName = '"+userName+"'";
String sql="select * from bookinformation ";
st = getStatement();
return st.executeQuery(sql);

}catch(Exception e)
{
e.printStackTrace();
return null;
}
}


在jsp上直接进行java函数的调用会使输出更为简单
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: