永发信息网

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

答案:3  悬赏:0  手机版
解决时间 2021-07-17 00:22
  • 提问者网友:城市野鹿
  • 2021-07-16 09:44

我在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>
但是老是提示客户端错误: 显示不出结果。。请各位大侠帮小女子看看!!!谢谢!!!

最佳答案
  • 五星知识达人网友:爱难随人意
  • 2021-07-16 09:56

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


所以查询的结果不正确

全部回答
  • 1楼网友:雾月
  • 2021-07-16 11:59

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

少了一个空格

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

有问题再回我

  • 2楼网友:旧脸谱
  • 2021-07-16 10:30
怎么写了两个body
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯