怎么在jsp网页中显示数据库数据
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
前面要导入使用的包哈.. 参考参考
<table >
<%String url="jdbc:mysql://localhost:3306/d?useUnicode=true&chatacterEncoding=utf8";
String username="root";
String password="123456";
Connection con=null;
Statement st=null;
ResultSet rs=null;
try{Class.forName("org.gjt.mm.mysql.Driver");
con=DriverManager.getConnection(url,username,password);
st=con.createStatement();
}catch(Exception e)
{ e.printStackTrace();
out.print("网站遇到问题,请稍后再试");
}
String title;
String author;
String publish;
Float price;
try
{
String sql="select * from book";
rs=st.executeQuery(sql);
if(rs!=null)
{
out.print(" <table width=843border=1>");
out.print("<tr><td>书名</td>");
out.print("<td>作者</td>");
out.print("<td>出版社</td>");
out.print("<td>价格</td>");
out.print("<td>操作</td></tr>");
while(rs.next())
{
title=rs.getString(2);
author=rs.getString(4);
publish=rs.getString(3);
price=rs.getFloat(6);
out.print("<tr><td>"+title+"</td>");
out.print("<td>"+author+"</td>");
out.print("<td>"+publish+"</td>");
out.print("<td>"+price+"</td>");
%>
<td>
<a href="bookdetail.jsp?isbn=<%=rs.getString(1)%>"><img src="image/buy.gif" width="45" height="25" border="0"></a>
</td>
<%
}
out.print("</table>");
}
}
catch(Exception e)
{
e.printStackTrace();
out.print("网站遇到问题,请稍后再试");
}
}
%>
</table>
<table >
<%String url="jdbc:mysql://localhost:3306/d?useUnicode=true&chatacterEncoding=utf8";
String username="root";
String password="123456";
Connection con=null;
Statement st=null;
ResultSet rs=null;
try{Class.forName("org.gjt.mm.mysql.Driver");
con=DriverManager.getConnection(url,username,password);
st=con.createStatement();
}catch(Exception e)
{ e.printStackTrace();
out.print("网站遇到问题,请稍后再试");
}
String title;
String author;
String publish;
Float price;
try
{
String sql="select * from book";
rs=st.executeQuery(sql);
if(rs!=null)
{
out.print(" <table width=843border=1>");
out.print("<tr><td>书名</td>");
out.print("<td>作者</td>");
out.print("<td>出版社</td>");
out.print("<td>价格</td>");
out.print("<td>操作</td></tr>");
while(rs.next())
{
title=rs.getString(2);
author=rs.getString(4);
publish=rs.getString(3);
price=rs.getFloat(6);
out.print("<tr><td>"+title+"</td>");
out.print("<td>"+author+"</td>");
out.print("<td>"+publish+"</td>");
out.print("<td>"+price+"</td>");
%>
<td>
<a href="bookdetail.jsp?isbn=<%=rs.getString(1)%>"><img src="image/buy.gif" width="45" height="25" border="0"></a>
</td>
<%
}
out.print("</table>");
}
}
catch(Exception e)
{
e.printStackTrace();
out.print("网站遇到问题,请稍后再试");
}
}
%>
</table>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |