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 {- ...
随机推荐
- oracle数据库锁表,什么SQL引起了锁表?ORACLE解锁的方法
--查询数据库锁表记录 select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.l ...
- [考试反思]0801NOIP模拟测试11
8月开门红. 放假回来果然像是神志不清一样. 但还是要接受这个事实. 嗯,说好听点,并列rank#7. 说难听点,垃圾rank#18. 都不用粘人名就知道我是哪一个吧... 因为图片不能太长,所以就不 ...
- 『题解』UVa11324 The Largest Clique
原文地址 Problem Portal Portal1:UVa Portal2:Luogu Portal3:Vjudge Description Given a directed graph \(\t ...
- LCD 调试总结
(1) 液晶显示模式 并行:MCU接口.RGB接口.Vysnc接口 串行:SPI接口.MDDI接口 (2) 屏幕颜色 实质上即为色阶的概念.色阶是表示手机液晶显示屏亮度强弱的指数标准,也就是通常所说的 ...
- JavaScript 弹出框:警告(alert)、确认(confirm)的简单写法
onclick="javascript:return window.confirm('message')"
- lqb 基础练习 数列排序 (sort的使用)
基础练习 数列排序 时间限制:1.0s 内存限制:512.0MB 问题描述 给定一个长度为n的数列,将这个数列按从小到大的顺序排列.1<=n<=200 输入格式 第一行为一个整 ...
- poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35999 Accepted: 13145 ...
- nyoj 833-取石子(七) (摆成一圈,取相邻)
833-取石子(七) 内存限制:64MB 时间限制:1000ms 特判: No 通过数:16 提交数:32 难度:1 题目描述: Yougth和Hrdv玩一个游戏,拿出n个石子摆成一圈,Yougth和 ...
- nyoj 217-a letter and a number (char)
217-a letter and a number 内存限制:64MB 时间限制:3000ms 特判: No 通过数:4 提交数:5 难度:1 题目描述: we define f(A) = 1, f( ...
- W5500
W5500 芯片是一款韩国全硬件TCP/IP协议栈以太网接口芯片, 最近发现我们国内也有和W5500 芯片一样芯片 介绍给大家 如下图 :