jsp 实现查询功能
要求:

实现查询功能
1.数据库代码
create database mvce;
use mvce;
create table test2(
id int not null identity,
tname char(10) not null,
tttype char(20) not null,
tatt char(20) not null,
tkfsname char(10) not null,
tcity char(20) not null,
taddress char(50) not null,
tbeizhu char(50) not null,
)
---------------------------------------------------------
insert into test2 values('wanke bai','big','black','vanke','changchun','beihu','king')
数据库code
2.数据
po/User.jsp
package po;
public class User {
int id;
String tname;
String ttype;
String tatt;
String tkfsname;
String tcity;
String taddress;
String tbeizhu;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String tname, String ttype, String tatt,
String tkfsname, String tcity, String taddress, String tbeizhu) {
super();
this.id = id;
this.tname = tname;
this.ttype = ttype;
this.tatt = tatt;
this.tkfsname = tkfsname;
this.tcity = tcity;
this.taddress = taddress;
this.tbeizhu = tbeizhu;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public String getTtype() {
return ttype;
}
public void setTtype(String ttype) {
this.ttype = ttype;
}
public String getTatt() {
return tatt;
}
public void setTatt(String tatt) {
this.tatt = tatt;
}
public String getTkfsname() {
return tkfsname;
}
public void setTkfsname(String tkfsname) {
this.tkfsname = tkfsname;
}
public String getTcity() {
return tcity;
}
public void setTcity(String tcity) {
this.tcity = tcity;
}
public String getTaddress() {
return taddress;
}
public void setTaddress(String taddress) {
this.taddress = taddress;
}
public String getTbeizhu() {
return tbeizhu;
}
public void setTbeizhu(String tbeizhu) {
this.tbeizhu = tbeizhu;
}
}
数据 和数据库 关联
3.数据连接 db/Dbconn.jsp
package db; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class DbConn { public static Connection getConn()
{
Connection con =null;
try {
// Class.forName("com.mysql.jdbc.Driver"); // 加载驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) {
System.out.println("加载驱动程序错误" + e.getMessage());
}
try {
// 创建连接 testdb是数据库名称
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=mvce", "sa", "123456"); } catch (SQLException e) { System.out.println("数据库连接操作出错" + e.getMessage());
} return con;
}
}
数据库连接
4.数据库处理 dao/UserDAO.java
package dao; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; import db.DbConn;
import po.User; public class UserDAO { public List<User> getUserList()
{
List<User> ls=new ArrayList<User>();
try {
// 创建连接 testdb是数据库名称
Connection con =DbConn.getConn();
// 创建声明SQL对象
Statement stm = con.createStatement();
// 执行SQL语句,得到结果集,结果集放到ResultSet对象中
ResultSet rs = stm.executeQuery("select * from test2");
// 通过循环,从结果集对象中取得数据
while (rs.next()) { //从数据库中获取字段 内容 放到main.jsp 界面
int id = rs.getInt("id"); // 取得int类型的字段id的值,
String tname = rs.getString("tname"); // 取得字符类型的字段username的值,
String ttype = rs.getString("ttype");
String tatt=rs.getString("tatt");
String tkfsname=rs.getString("tkfsname");
String tcity=rs.getString("tcity");
String taddress=rs.getString("taddress");
String tbeizhu=rs.getString("tbeizhu");
User u=new User(); u.setId(id);
u.setTname(tname);
u.setTtype(ttype);
u.setTatt(tatt);
u.setTkfsname(tkfsname);
u.setTcity(tcity);
u.setTaddress(taddress);
u.setTbeizhu(tbeizhu);
ls.add(u); }
} catch (SQLException e) { System.out.println("数据库操作出错" + e.getMessage());
}
return ls;
}
}
getUserList
5.中间媒介 servlet/GetUsersServlet.java

用LoginServlet 需要跳转到 Get 要不然的没有数据没有传递过去 会报错的
package servlet; import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import dao.UserDAO;
import db.DbConn; public class LoginServlet extends HttpServlet { /**
* Constructor of the object.
*/
public LoginServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>"); response.sendRedirect("../servlet/GetUsersServlet"); out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
LoginServlet
package servlet; import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import po.User; import dao.UserDAO; public class GetUsersServlet extends HttpServlet { /**
* Constructor of the object.
*/
public GetUsersServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//
response.sendRedirect("../servlet/GetUsersServlet"); response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
HttpSession session=request.getSession(true); UserDAO udao=new UserDAO();
List<User> ls= udao.getUserList();
session.setAttribute("userlist", ls);
response.sendRedirect("../main.jsp"); out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
GetUsersServlet.jsp
6.jsp 页面
main.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>My JSP 'Login.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>
<div align="center" >
<form method="get" action="servlet/LoginServlet"> 用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" name="button1" value="登录">
</form>
</div>
</body>
</html>
Login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="po.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'main.jsp' starting page</title>
</head> <body>
<table width="502" border="1">
<tr>
<td width="37">房屋编号</td>
<td width="63">房屋名称</td>
<td width="52">房屋类型</td>
<td width="52">房屋属性</td>
<td width="61">开发商名称</td>
<td width="197">所在城市</td>
<td width="80">具体地址</td>
<td width="80">备注</td>
</tr>
<%
List<User> ls=(List<User>)session.getAttribute("userlist");
for(int i=0;i<ls.size();i++)
{
User u=ls.get(i);
%>
<tr>
<td><%=u.getId()%></td>
<td><%=u.getTname()%></td>
<td><%=u.getTtype()%></td>
<td><%=u.getTatt()%></td>
<td><%=u.getTkfsname()%></td>
<td><%=u.getTcity()%></td>
<td><%=u.getTaddress()%></td>
<td><%=u.getTbeizhu()%></td> </tr>
<%
}
%>
</table>
</body>
</html>
main.jsp
遇到的问题:
servlet的转发问题 。 可不可以不用Login.jsp 直接显示
jsp 实现查询功能的更多相关文章
- 033医疗项目-模块三:药品供应商目录模块——供货商药品目录t添加查询功能----------Dao层和Service层和Action层和调试
什么叫做供货商药品目录t添加查询功能?就是说我们前面的博客里面不是说供货商登录后看到了自己供应的药品了么如下: 现在供货商想要往里面添加别的药品,那么这个药品的来源就是卫生局提供的那个Ypxx表(药品 ...
- JavaWeb系统(增删改查、多条件查询功能)
该系统是一个简单的青年服务管理系统,主要包括了较完整的常用的增删改查以及多条件查询功能,对于初学者有很大帮助. 下面是相关的Java代码.jsp页面.以及数据库的创建和相关表的设计 java代码 首先 ...
- 通过维基API实现维基百科查询功能
通过英文维基的免费API,可以实现对维基百科的搜索查询或者标题全文查询等,尝试了一下通过title实现全文查询,返回的结果是wikitext格式,暂时不知道该如何应用,所以仅实现了查询功能,可以返回最 ...
- 创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段
创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段 添加查询功能 本文将实现通过Name查询用户信息. 首先更新GetAll方法以启用查询: public async ...
- MySQL 5.5开启慢查询功能
vim /etc/my.cnf [mysqld] slow-query-log = on # 开启慢查询功能 slow_query_log_file = /usr/local/mysql/data/s ...
- ASP.NET MVC系列:为视图添加查询功能
首先,在MoviesController里添加一个查询方法,代码如下 public ActionResult SearchIndex(string title) { //查询数据库中的电影表 var ...
- 完善ext.grid.panel中的查询功能(紧接上一篇)
今天的代码主要是实现,Ext.grid.panel中的查询,其实我也是一名extjs新手,开始想的实现方式是另外再创建一个新的grid类来存放查询出的数据(就是有几个分类查询就创建几个grid类),这 ...
- [Architecture Pattern] Repository实作查询功能
[Architecture Pattern] Repository实作查询功能 范例下载 范例程序代码:点此下载 问题情景 在系统的BLL与DAL之间,加入Repository Pattern的设计, ...
- RPM软件包管理的查询功能
以后大家升级rpm包的时候,不要用Uvh了! 我推荐用Fvh 前者会把没有安装过得包也给装上,后者只会更新已经安装的包 总结:未安装的加上小写p,已安装的不需要加p 查询q rpm {- ...
随机推荐
- NOIP201605玩具谜题-解题报告
NOIP201605玩具谜题-解题报告 2019-11- ...
- [开源] gnet: 一个轻量级且高性能的 Golang 网络库
Github 主页 https://github.com/panjf2000/gnet 欢迎大家围观~~,目前还在持续更新,感兴趣的话可以 star 一下暗中观察哦. 简介 gnet 是一个基于 Ev ...
- Zabbix 四 主动模式
本次的主机192.168.131.8 被动模式. 将zabbix4.4.4的源码包放过去,解压安装依赖准备编译安装,并创建zabbix账户. tar -xf zabbix-4.4.0.tar.gz & ...
- 网络安全-主动信息收集篇第二章-三层网络发现之ping
第三层网络扫描基于TCP/IP.ICMP协议. 优点:可路由.速度比较快 缺点:相对于二层网络扫描较慢,容易被边界防火墙过滤 所有扫描发现技术,都会有相应的对抗办法,所以无论是来自二层的网络扫描还是来 ...
- 模板(ac):启发式合并
首先说明一点:线段树合并不是启发式合并. 启发式合并的大概内容就是:把小的数据结构按照这个数据结构的正常插入方法,一个一个地暴力塞进去. 而线段树合并显然不是这个东西. 这道题的题解太烂了,所以耽误了 ...
- NOIP模拟 20
来自liu_runda的善意 T1 周 究级难题,不可做,咕了. T2 任 他为什么总强调没环啊? 他为什么总强调没环啊? 他为什么总强调没环啊? ...... QAQ 因为他总是棵树,所以点的数量 ...
- WeihanLi.Npoi 近期更新
WeihanLi.Npoi 近期更新 Intro 最近对我的 NPOI 扩展做了一些改变,一方面提高性能,一方面修复bug,增加一些新的功能来让它更加好用,前几天发布了 1.5.0 版本,下面来介绍一 ...
- postman-接口间数据传递
接口间数据传递 在我们做接口测试过程中会经常碰到使用上一个接口返回数据的情况,jmeter中可通过正则表达式提取,postman中如何提取呢?我们来看实例,这里使用的同一个接口来演示. 我们提取出 ...
- Python实现王者荣耀小助手(一)
简单来说网络爬虫,是指抓取万维网信息的程序或者脚本,Python在网络爬虫有很大优势,今天我们用Python实现获取王者荣耀相关数据,做一个小助手: 前期准备,环境搭建: Python2.7 sys模 ...
- 提交代码到github托管
廖雪峰官网:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000,感觉初学很棒的一个地 ...