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