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 {- ...
随机推荐
- AutoCad 二次开发 .net 之相同块的自动编号
主要步骤: 一.获取一个块的id: 其中oId就是了. 二.通过次oId获取块引用blkRef: 三.通过它获取所有相同的块引用的id集合: 四.通过步骤三的集合得到所有的块引用得到集合listBr: ...
- Go netpoll I/O 多路复用构建原生网络模型之源码深度解析
导言 Go 基于 I/O multiplexing 和 goroutine 构建了一个简洁而高性能的原生网络模型(基于 Go 的I/O 多路复用 netpoll),提供了 goroutine-per- ...
- 命运Ⅰ&命运Ⅱ
upd:为啥下面的相关博文都是各种退役记(这TM怎么就相关了) 竟然被卡线了,16名,我这几次考试也是炸到了一定境界了... 前三次模拟总榜rk1,第一次分机房rk4,第二次分机房rk11,第三次分机 ...
- 没NOIP了?
HSEZ李亮:“考虑一下来HSEZ当艺术生吧!"
- day7-format字符串格式化
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) print ...
- 每天一道算法题-leetcode136-只出现一次的数字
前言 打卡第一天 2019.10.26日打卡 算法,即解决问题的方法.同一个问题,使用不同的算法,虽然得到的结果相同,但是耗费的时间和资源是不同的.这就需要我们学习算法,找出哪个算法更好. 大家都知道 ...
- 如何设置HTML页面中文本的字体
字体属性介绍 CSS中的字体属性是干什么的呢?字体字体肯定和字体有关咯,就是设置HTML页面中文本的字体, CSS中常用的字体属性有几种呢,笔者给大家梳理了下,比较常用的一共有5种,今天我们就看看这5 ...
- ASP.NET Core 1.0: Using Entity Framework Core 1.0 - Transaction
跟Entity Framework之前的版本不同,Class DbContext不再有AcceptAllChanges()方法. 使用Transaction需要使用DbContext中的Databas ...
- nyoj 199-无线网络覆盖 (ceil())
199-无线网络覆盖 内存限制:64MB 时间限制:3000ms 特判: No 通过数:4 提交数:13 难度:3 题目描述: 我们的乐乐同学对于网络可算得上是情有独钟,他有一个计划,那就是用无线网覆 ...
- java中hashmap容量的初始化
HashMap使用HashMap(int initialCapacity)对集合进行初始化. 在默认的情况下,HashMap的容量是16.但是如果用户通过构造函数指定了一个数字作为容量,那么Hash会 ...