要求:

实现查询功能

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 实现查询功能的更多相关文章

  1. 033医疗项目-模块三:药品供应商目录模块——供货商药品目录t添加查询功能----------Dao层和Service层和Action层和调试

    什么叫做供货商药品目录t添加查询功能?就是说我们前面的博客里面不是说供货商登录后看到了自己供应的药品了么如下: 现在供货商想要往里面添加别的药品,那么这个药品的来源就是卫生局提供的那个Ypxx表(药品 ...

  2. JavaWeb系统(增删改查、多条件查询功能)

    该系统是一个简单的青年服务管理系统,主要包括了较完整的常用的增删改查以及多条件查询功能,对于初学者有很大帮助. 下面是相关的Java代码.jsp页面.以及数据库的创建和相关表的设计 java代码 首先 ...

  3. 通过维基API实现维基百科查询功能

    通过英文维基的免费API,可以实现对维基百科的搜索查询或者标题全文查询等,尝试了一下通过title实现全文查询,返回的结果是wikitext格式,暂时不知道该如何应用,所以仅实现了查询功能,可以返回最 ...

  4. 创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段

    创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段 添加查询功能 本文将实现通过Name查询用户信息. 首先更新GetAll方法以启用查询: public async ...

  5. MySQL 5.5开启慢查询功能

    vim /etc/my.cnf [mysqld] slow-query-log = on # 开启慢查询功能 slow_query_log_file = /usr/local/mysql/data/s ...

  6. ASP.NET MVC系列:为视图添加查询功能

    首先,在MoviesController里添加一个查询方法,代码如下 public ActionResult SearchIndex(string title) { //查询数据库中的电影表 var ...

  7. 完善ext.grid.panel中的查询功能(紧接上一篇)

    今天的代码主要是实现,Ext.grid.panel中的查询,其实我也是一名extjs新手,开始想的实现方式是另外再创建一个新的grid类来存放查询出的数据(就是有几个分类查询就创建几个grid类),这 ...

  8. [Architecture Pattern] Repository实作查询功能

    [Architecture Pattern] Repository实作查询功能 范例下载 范例程序代码:点此下载 问题情景 在系统的BLL与DAL之间,加入Repository Pattern的设计, ...

  9. RPM软件包管理的查询功能

    以后大家升级rpm包的时候,不要用Uvh了! 我推荐用Fvh 前者会把没有安装过得包也给装上,后者只会更新已经安装的包   总结:未安装的加上小写p,已安装的不需要加p   查询q    rpm {- ...

随机推荐

  1. wfi破解

    破解wifi步骤 1.准备字典(常见字典 数字组合.常用姓氏.汉字姓名+年份组合等等) 2.无线网卡 3.查看附近WiFi信息 前言 : 随着无线网络走进我们的生活,在方便了我们的同时又产生了许多的安 ...

  2. npm 学习笔记

    一.介绍 1.是什么 npm 全称是 Node Package Manager,即 Node 包管理工具. 但是发展到后来,并不仅是适用于 node.js 的包. 所以现在看 node_modules ...

  3. permu 莫队 总结

    由于每次询问静态区间里完整值域段的最大大小 貌似很好用莫队转移,所以考虑怎么转移 当给它扩展一个数时,就是给值域添加了一个值 这个值可能已经存在,也可能是新的 有的神仙做法是维护了一个并查集,然而我这 ...

  4. NOIP模拟测试13

    考得还算可以,T3还有提升空间(没看清题&&样例没过 拿了4分). 期望得分:80+40+0=120 实际得分:80+85+4=169 一脸黑线.....是数据比较水的原因,T2分都比 ...

  5. 考试T3麻将

    这题就是一个简单的暴力,但考试的时候不知道脑子在想什么,什么都没打出来,也许是我想的太多了... 这道题对于不会打麻将的人来说还是有点难理解规则的,我没说过我会打麻将,这里是题目链接. 20分思路,利 ...

  6. Android 开发中是否应该使用枚举?

    本文由咕咚发布在个人博客,转载请注明出处. 本文永久地址:https://gudong.name/2019/11/04/use-enum-or-not.html 在 Android 官方文档推出性能优 ...

  7. 从壹开始 [ Ids4实战 ] 之五 ║ 多项目集成统一认证中心的思考

    前言 哈喽大家好,好久都没有写文章了,这次又重新开始写技术文章了,半年前我还是一直保持每周都写文章的,后来是为了响应群友的号召,开始踏上了录制视频(https://www.bilibili.com/v ...

  8. Redis实战--Jedis实现分布式锁

    echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 分布式 ...

  9. 生信 - 从repeatmasker传送门过来的 blast

    以前有的是非完整时间写的博客,抽时间需要统一整理一下. 今天在重新装repeatmasker. 整个过程是这样的,有关联的事情有两个. 1. 装repeatmasker需要各种Prerequisite ...

  10. C语音中最简单的排序冒泡排序和选择排序代码实现(非指针)

    #include<stdio.h> int main() { int a[5] = { 2,5,7,3,-1 }; int n = sizeof(a) / sizeof(a[0]);//元 ...