永发信息网

为什么我的jsp老是打印不出数据库中的表格?

答案:3  悬赏:0  手机版
解决时间 2021-07-17 00:22

我在My SQL 中建了一个学生信息表。

写了一个查询JSP 页面:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>信息查询</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<h2 align="center">客户信息查询</h2>
<form action="ch13/customerByName.jsp">
<center>
<p>请输入客户姓名:<input type="text" name="customerName">
<input type="submit" value="提交">
<input type="reset" value="重置">


</center>
</form>
</body>
</html>

例外写了一个接收的页面 :

<%@ page language="java" import="java.sql.*,java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'customerByName.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%!
public String getString(String str){
if(str==null){
str="";
}else{
try{
byte[]b=str.getBytes("ISO-8859-1");
str=new String(b);
} catch(Exception e){
e.printStackTrace();
}

}
return str;
}
%>
<%
String Name=request.getParameter("customerName");
if(Name==null)
{
Name="";
}
if(Name.trim().equals(""))
{
out.println("您输入的有误!请重新输入。");
out.println("<a href='/ch13/search.jsp'>返回</a>");

}
else
{
Name=this.getString(Name);
Connection con=null;
Statement stmt=null;
ResultSet rs=null;


try
{

Class.forName("com.mysql.jdbc.Driver");


con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student","root","123456");
stmt=con.createStatement();
String sql="select * from table1where Name="+""+Name+"";
rs=stmt.executeQuery(sql);


out.println("<table border='1'>");
out.println("<tr>");
out.println("<th width=80>"+"姓名");
out.println("<th width=50>"+"年龄");
out.println("<th width=100>"+"地址");
out.println("<th width=80>"+"电话");
out.println("<th width=120>"+"邮箱");
out.println("</tr>");
boolean result=false;

while(rs.next())
{
String customName=rs.getString(1);
//Name=new String(Name.getBytes("ISO-8859-1"));
int age=rs.getInt(2);
String address=rs.getString(3);
//address=new String(address.getBytes("ISO-8859-1"));
String tel=rs.getString(4);
String email=rs.getString(5);

out.println("<tr>");
out.println("<td>"+customName+"</td>");
out.println("<td>"+age+"</td>");
out.println("<td>"+address+"</td>");
out.println("<td>"+tel+"</td>");
out.println("<td>"+email+"</td>");

out.println("</tr>");
result=true;
}
out.println("</table>");
if(!result)
{
out.println("该客户不存在");
out.println("<a href='/ch13/search.jsp'>返回</a>");
}
rs.close();
stmt.close();
con.close();

}
catch(Exception e)
{
System.out.println("e="+e.toString());
}
}
%>
</body>
</html>
但是老是提示客户端错误: 显示不出结果。。请各位大侠帮小女子看看!!!谢谢!!!

最佳答案

String sql="select * from table1where Name="+"'"+Name+"'";//这里少了对单引号


所以查询的结果不正确

全部回答

String sql="select * from table1where Name="+""+Name+"";

少了一个空格

String sql="select * from table1 where Name="+""+Name+"";

有问题再回我

怎么写了两个body
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
女孩哭了男孩改怎么办?
怎么把名字制作成下面这样
怀孕的人总是颈椎痛是怎么回事 ,有绣十字绣
我爱的人因为我的原因和别的女孩子在一起
怎样点亮问文
下面是我的电脑配置 我在特效都开中等的时候
我买了塔罗牌后没有抽出一张属于自己的牌灵就
脸上有红血丝能用白醋水洗脸吗
我的电脑变慢了
李米的猜想中跳下去的那个诗人是谁
说谎谁唱的?
每天对着老师你烦吗??
手机型号是OBEE5878 的 能不能把手机QQ下载下
最近淘宝里什么样的十字绣成品卖的最火啊?
求推荐几本介绍西方魔法种族的书
推荐资讯
you can't stop me翻译成中文是什麽意思
GT劲舞团2里的家族可以转让吗?
怎样戒掉劲舞?
我用追击僵尸换了高毒蝎子值吗?
现在很厌烦这个世界!我该怎么办啊?
怎样知道一首歌的歌名
我的显示器换了个显卡后,经常屏幕黑下 1秒后
联想手机和夏普手机哪个好?
天津市塘沽区哪儿卖东西便宜?
联通新势力QQ卡几时到包头?
甲流可持续多长时间?
厦门地区诺基亚5530xm的报价
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?