赞
踩
主要配置
oracle
tomcat服务器
新建javaweb项目
1、打开项目,选择javaEE
2、点击‘File–New,选择Dynamic web Project
3、填写项目名称等信息,点finish
ORACLE部分
oracle完全卸载重装
电脑若已经安装过oracle但是没有卸载干净,会影响重新安装
具体可看原文链接http://blog.csdn.net/svygh123/article/details/50948036
添加JDBC.jar
右键新建的javaweb项目->build path->add external archives
本次使用的是oracle11,导入ojdbc.jar
tomcat配置
window->preference
user.java文件
package modle;
public class user {
private String pn_name;//跑男姓名
private String pn_number;//跑男编号,主键
private String pn_sex;//跑男性别
private String pn_able;//跑男能力
private int pn_winnum;//跑男获胜次数
private int allcount;//跑男总人数
public String getPn_name() {
return pn_name;
}
public int getAllcount() {
return allcount;
}
public void setAllcount(int allcount) {
this.allcount = allcount;
}
public void setPn_name(String pn_name) {
this.pn_name = pn_name;
}
public String getPn_number() {
return pn_number;
}
public void setPn_number(String pn_number) {
this.pn_number = pn_number;
}
public String getPn_sex() {
return pn_sex;
}
public void setPn_sex(String pn_sex) {
this.pn_sex = pn_sex;
}
public String getPn_able() {
return pn_able;
}
public void setPn_able(String pn_able) {
this.pn_able = pn_able;
}
public int getPn_winnum() {
return pn_winnum;
}
public void setPn_winnum(int pn_winnum) {
this.pn_winnum = pn_winnum;
}
}
就是定义的javabean,简单的get set方法
JDBCDao.java文件
package jdbcconnet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import modle.user;
public class JDBCDao {
public static void main(String[] args)
{
//在这里调用各种方法;
}
///////////////////////////////////////查询所有信息
public static List searchall()
{
List<user> runman = new ArrayList<user>();
Connection con = null;//创建一个数据库连接
PreparedStatement pre = null;//创建预编译语句对象
ResultSet result = null;//创建一个结果集对象
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("开始尝试连接数据库");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "test1";
String password = "guanxx";
con = DriverManager.getConnection(url, user, password);
System.out.println("连接成功");
String sql = "select * from information ";//预编译语句,?代表参数
pre = con.prepareStatement(sql);
result = pre.executeQuery();//执行查询,括号不需要再加参数
while (result.next())
{
user u = new user();
//创建user对象,将返回结果添加到新建的runman的list中
u.setPn_name(result.getString("pn_name"));
u.setPn_number(result.getString("pn_number"));
u.setPn_sex(result.getString("pn_sex"));
u.setPn_able(result.getString("pn_able"));
u.setPn_winnum(result.getInt("pn_winnum"));
runman.add(u);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
try
{
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("数据库连接已关闭,数据打印如下:");
}
catch(Exception e)
{
e.printStackTrace();
}
}
return runman;
}
///////////分页查询需要调用的方法,相较于searchall方法只是sql语句的改变
public static List searchallindifferencepage(int pagesize,int pageindex)
{
//pagesize为每页显示的条数,pageindex为当前页数
int startnum = pagesize*(pageindex-1)+1;//数据库查询起始行
int endnum = pagesize*pageindex;//数据库查询结束行
List<user> runman = new ArrayList<user>();
Connection con = null;//创建一个数据库连接
PreparedStatement pre = null;//创建预编译语句对象
ResultSet result = null;//创建一个结果集对象
String sql = "select * from (select rownum as r,t.* from (select a.* from information a order by pn_number desc) t where rownum<="+endnum+") where r>="+startnum+"";
//三层嵌套,获取起始行和结束行之间的数据,可添加排序
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("开始尝试连接数据库");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "test1";
String password = "guanxx";
con = DriverManager.getConnection(url, user, password);
System.out.println("连接成功");
pre = con.prepareStatement(sql);
result = pre.executeQuery();//执行查询,括号不需要加参数
while (result.next())
{
user u = new user();
u.setPn_name(result.getString("pn_name"));
u.setPn_number(result.getString("pn_number"));
u.setPn_sex(result.getString("pn_sex"));
u.setPn_able(result.getString("pn_able"));
u.setPn_winnum(result.getInt("pn_winnum"));
runman.add(u);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
try
{
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("数据库连接已关闭,数据打印如下:");
}
catch(Exception e)
{
e.printStackTrace();
}
}
return runman;
}
////////////////////////获取表格总行数,用于分页时计算最大页数
public static int getcount()
{
int count = 0;
Connection con = null;//创建一个数据库连接
PreparedStatement pre = null;//创建预编译语句对象
ResultSet result = null;//创建一个结果集对象
String sql = "select count(*) from information";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("开始尝试连接数据库");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "test1";
String password = "guanxx";
con = DriverManager.getConnection(url, user, password);
pre = con.prepareStatement(sql);
result = pre.executeQuery();
while(result.next()){
count=result.getInt(1);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
try
{
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return count;
}
//////////////////////跟getcount()方法一起使用,得到最大页数
public int getTotalPage(int pageSize){
int totalPage=getcount();
return (totalPage%pageSize==0)?(totalPage/pageSize):(totalPage/pageSize+1);
}
////////////////////////////////按条件查询
public static List searchsome(String id1,String id2,String id3,String id4,String id5)
{
String sql = "select * from information ";
//因为需要判断哪些输入框没有输入值,所以sql语句需要动态添加
int num = 5;
//有5个需要判断是否输入的文本框
String getid1 = id1;
String getid2 = id2;
String getid3 = id3;
String getid4 = id4;
String getid5 = id5;
String [] a = {getid1,getid2,getid3,getid4,getid5};
String [] pnforsql = {"pn_name","pn_number","pn_sex","pn_able","pn_winnum"};
for(int i=0;i<a.length;i++)
{
if(a[i] == null || a[i].length() <= 0)
//循环判断,得到不为空的文本框的个数
num--;
}
if(num==0)
{
sql = "select * from information";
//如果没有输入,直接全量搜索
}
else if (num==1)
{
//如果只有一个查询条件,则不需要加入and
for(int i=0;i<a.length;i++)
{
if(a[i] == null || a[i].length() <= 0)
{
}
else
{
sql = "select * from information where "+pnforsql[i]+" like '"+a[i]+"'";
//循环判断哪个字段不为空,加入sql语句;
}
}
}
else
{
//一个以上的查询条件,需要使用and,先全部添加,再去掉最后一个
String sql1 = "";
for(int i=0;i<a.length;i++)
{
if(a[i] == null || a[i].length() <= 0)
{
}
else
{
sql1 = sql1+pnforsql[i]+" like '"+a[i]+"'"+" and ";
}
}
sql1 = " where "+sql1.substring(0,sql1.length()-4);
//去掉sql后面4位;
sql = sql+sql1;
}
List<user> runman = new ArrayList<user>();
Connection con = null;//创建一个数据库连接
PreparedStatement pre = null;//创建预编译语句对象
ResultSet result = null;//创建一个结果集对象
String sql111 = "select * from information";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("开始尝试连接数据库");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "test1";
String password = "guanxx";
con = DriverManager.getConnection(url, user, password);
System.out.println("连接成功");
pre = con.prepareStatement(sql);
result = pre.executeQuery();//执行查询,括号不需要再加参数
while (result.next())
{
user u = new user();
u.setPn_name(result.getString("pn_name"));
u.setPn_number(result.getString("pn_number"));
u.setPn_sex(result.getString("pn_sex"));
u.setPn_able(result.getString("pn_able"));
u.setPn_winnum(result.getInt("pn_winnum"));
runman.add(u);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
try
{
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("数据库连接已关闭,数据打印如下:");
}
catch(Exception e)
{
e.printStackTrace();
}
}
return runman;
}
///////////查询一个跑男信息,用于修改时先将值至于前台页面;
public static List searchaone(String i)
{
//i为前台点击修改时传过来的pn_number信息
String win = "'"+i+"'";
List<user> runman = new ArrayList<user>();
Connection con = null;//创建一个数据库连接
PreparedStatement pre = null;//创建预编译语句对象
ResultSet result = null;//创建一个结果集对象
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("开始尝试连接数据库");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "test1";
String password = "guanxx";
con = DriverManager.getConnection(url, user, password);
System.out.println("连接成功");
String sql = "select * from information where pn_name like"+win+""; //预编译语句,?代表参数
pre = con.prepareStatement(sql);
result = pre.executeQuery();//执行查询,括号不需要再加参数
while (result.next())
{
user u = new user();
u.setPn_name(result.getString("pn_name"));
u.setPn_number(result.getString("pn_number"));
u.setPn_sex(result.getString("pn_sex"));
u.setPn_able(result.getString("pn_able"));
u.setPn_winnum(result.getInt("pn_winnum"));
runman.add(u);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
try
{
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("数据库连接已关闭,数据打印如下:");
}
catch(Exception e)
{
e.printStackTrace();
}
}
return runman;
}
////////////////////////修改,先将查询到的一个跑男信息置于前台页面,没有修改的也将其值传过来进行更新;
public static void update(String id1,String id2,String id3,String id4,String id5)
{
Connection con = null;//创建一个数据库连接
PreparedStatement pre = null;//创建预编译语句对象
ResultSet result = null;//创建一个结果集对象
String pnname = "'"+id1+"'";
String pnnumber = "'"+id2+"'";
String pnsex = "'"+id3+"'";
String pnable = "'"+id4+"'";
String pnwinnum = "'"+id5+"'";
String sql = "update information set pn_name ="+pnname+", pn_sex="+pnsex+", pn_able="+pnable+",pn_winnum="+pnwinnum+"where pn_number="+pnnumber+"";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("开始尝试连接数据库");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "test1";
String password = "guanxx";
con = DriverManager.getConnection(url, user, password);
System.out.println("连接成功,准备更新数据");
pre = con.prepareStatement(sql);
result = pre.executeQuery();
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
try
{
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("更新成功:");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
/////批量删除,前台检查CheckBox的勾选状态,将勾选的行信息的pn_number存于数组传参过来
public static void deletelots(String i[])
{
Connection con = null;//创建一个数据库连接
PreparedStatement pre = null;//创建预编译语句对象
ResultSet result = null;//创建一个结果集对象
String [] win = i;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("开始尝试连接数据库");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "test1";
String password = "guanxx";
con = DriverManager.getConnection(url, user, password);
System.out.println("连接成功,准备删除");
if (win.length>0){
for(int j=0;j<win.length;j++){
String sql = "delete from information where pn_number= '"+win[j]+"'";
System.out.println(sql);
pre = con.prepareStatement(sql);
result = pre.executeQuery();
}
}
System.out.println("删除成功:");
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
try
{
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("删除成功:");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
////////////////////////插入数据时候使用
public static void insert(String id1,String id2,String id3,String id4,String id5)
{
Connection con = null;//创建一个数据库连接
PreparedStatement pre = null;//创建预编译语句对象
ResultSet result = null;//创建一个结果集对象
String pnname = "'"+id1+"'";
String pnnumber = "'"+id2+"'";
String pnsex = "'"+id3+"'";
String pnable = "'"+id4+"'";
String pnwinnum = "'"+id5+"'";
String sql = "insert into information (pn_name,pn_number,pn_sex,pn_able,pn_winnum) values ("+pnname+","+pnnumber+","+pnsex+","+pnable+","+pnwinnum+")";
System.out.println(sql);
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("开始尝试连接数据库");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "test1";
String password = "guanxx";
con = DriverManager.getConnection(url, user, password);
System.out.println("连接成功,准备插入数据");
pre = con.prepareStatement(sql);
result = pre.executeQuery();
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
try
{
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("插入成功:");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
/////////////////单条记录删除
public static void deletenum(String i)
{
Connection con = null;//创建一个数据库连接
PreparedStatement pre = null;//创建预编译语句对象
ResultSet result = null;//创建一个结果集对象
String win = i;
String sql = "delete from information where pn_number="+win+"";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("开始尝试连接数据库");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "test1";
String password = "guanxx";
con = DriverManager.getConnection(url, user, password);
System.out.println("连接成功,准备删除");
pre = con.prepareStatement(sql);
// pre.setInt(1, i);
// pre.executeUpdate();
result = pre.executeQuery();
System.out.println("删除成功:");
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
try
{
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("删除成功:");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
FindallServlet.java文件
package hellotest;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modle.user;
import jdbcconnet.JDBCDao;
public class FindallServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try
{
JDBCDao jdbcdao = new JDBCDao();
int pagesize = 5;//每页显示的条数
int totalpages = jdbcdao.getTotalPage(pagesize);//获取最大页数
int pageIndex = 0;
if(request.getParameter("pageIndex")==null)
{
pageIndex = 1;
}
else
{
pageIndex = Integer.valueOf(request.getParameter("pageIndex"));
if(pageIndex<1)
{
pageIndex = 1;
}
else if (pageIndex>totalpages)
{
pageIndex = totalpages;
}
}
List list = jdbcdao.searchallindifferencepage(pagesize, pageIndex);
//severlet核心代码?
request.setAttribute("list", list);//request设置传输数据的tag("list")和对象(list);
request.setAttribute("pageIndex", pageIndex);
request.setAttribute("totalpages", totalpages);
}
catch(Exception e)
{
e.printStackTrace();
}
//请求转发到指定URL,是服务器端跳转
request.getRequestDispatcher("runman-list.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
此处为了实现分页功能,使用了searchallindifferencepage(pagesize, pageIndex)方法,如果不需要分页,可以直接调用searchall方法;
runman_list.jsp文件(主页面)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.List"%>
<%@page import="modle.user"%>
<%@page import="jdbcconnet.JDBCDao"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>所有跑男信息</title>
</head>
<body>
<%
int pageIndex = (Integer)request.getAttribute("pageIndex");
//获取上面request.setAttribute("pageIndex", pageIndex);
int totalpages = (Integer)request.getAttribute("totalpages");
%>
<table align="center" width="500" border="1" height="180"
bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
<tr bgcolor="white">
<td align="center" colspan="7">
<h2>列举所有的跑男的信息</h2>
</td>
</tr>
<tr align="center" bgcolor="#e1ffc1">
<td><b>姓名</b></td>
<td><b>特工编号</b></td>
<td><b>性别</b></td>
<td><b>能力</b></td>
<td><b>胜出次数</b></td>
<td><b>修改</b></td>
<td><b>删除</b></td>
</tr>
<%
// 获取跑男信息集合
List list = (List) request.getAttribute("list");
// 判断集合是否有效
if (list == null || list.size() < 1) {
out.print("没有数据!");
} else
{
// 遍历跑男集合中的数据
for (int i=0;i<list.size();i++)
{
user us= (user)list.get(i);
%>
<tr align="center" bgcolor="white">
<td><%=us.getPn_name()%></td>
<td><%=us.getPn_number()%></td>
<td><%=us.getPn_sex()%></td>
<td><%=us.getPn_able()%></td>
<td><%=us.getPn_winnum()%></td>
<td >
<a href="UpdateServlet?num=<%=us.getPn_name()%>">修改</a>
</td>
<td><a href="DeleteServlet?id=<%=us.getPn_number()%>">删除</a> </td>
</tr>
<%
}
}
%>
<form name="insert" method="post" action="insert.jsp">
<input type="submit" bgcolor="white" value="插入新数据" align="bottom" colspan="7">
</form>
<form name="search" method="post" action="Searchsome.jsp">
<input type="submit" bgcolor="white" value="按条件查询" align="bottom" colspan="7">
</form>
</table>
<a href="FindallServlet?pageIndex=1">首页</a>
<a href="FindallServlet?pageIndex=<%=pageIndex=pageIndex-1 %>">上一页</a>
<a href="FindallServlet?pageIndex=<%=pageIndex=pageIndex+2 %>">下一页</a>
<a href="FindallServlet?pageIndex=<%=totalpages%>">末页</a>
<br/>
<p style="color:red"">当前页数:<%=pageIndex-1%></p>
</body>
</html>
<form name="search" method="post" action="Searchsome.jsp"> <input type="submit" bgcolor="white" value="按条件查询" align="bottom" colspan="7"> </form> 按条件查询以及插入新数据为跳转按钮
- 1
- 2
- 3
- 4
分页查询展示如下:
点击修改时:
UpdateServlet.java
package hellotest;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modle.user;
import jdbcconnet.JDBCDao;
public class UpdateServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
String num = String.valueOf(request.getParameter("num"));
String id = new String(num.getBytes("iso8859-1"),"utf8");
//获取前台传值并转码;
JDBCDao jdbc = new JDBCDao();
List list = jdbc.searchaone(id);
user us= (user)list.get(0);
request.setAttribute("Pn_name", us.getPn_name());
request.setAttribute("Pn_number", us.getPn_number());
request.setAttribute("Pn_sex", us.getPn_sex());
request.setAttribute("Pn_able", us.getPn_able());
request.setAttribute("Pn_winnum", us.getPn_winnum());
request.getRequestDispatcher("UpdateServlet.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
// super.doPost(request, response);
}
}
主要是为了将行信息展示到前台,方便修改更新;
UpdateServlet.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>修改信息</title>
</head>
<body>
<table align="center" width="500" border="1" height="90"
bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
<tr align="center" bgcolor="#e1ffc1">
<td><h2>姓名</h2></td>
<td><h2>特工编号</h2></td>
<td><h2>性别</h2></td>
<td><h2>能力</h2></td>
<td><h2>胜出次数</h2></td>
</tr>
<form name="thisform" method="post" action="UpdateFinshServlet">
<tr align="center" bgcolor="white">
<td>
<input type="text" id="Pn_name" name="Pn_name" value="${Pn_name}"/>
</td>
<td>
<input type="text" id="Pn_number" name="Pn_number" value="${Pn_number}"/>
</td>
<td>
<input type="text" id="Pn_sex" name="Pn_sex" value="${Pn_sex}"/>
</td>
<td>
<input type="text" id="Pn_able" name="Pn_able" value="${Pn_able}"/>
<td>
<input type="text" id="Pn_winnum" name="Pn_winnum" value="${Pn_winnum}"/>
</td>
</tr>
<input type="button" bgcolor="white" value="确认修改" align="bottom" colspan="7" onclick="sel()" >
//点击监听
</form>
//点击监听函数
<script language="javascript">
function sel(){
document.thisform.submit();
}
</script>
</table>
</body>
</html>
点击确认修改的时候需要设置一下点击监听,调用form.submit提交后台;
更改需要更改的信息,点击“确认修改”,指定到UpdateFinshServlet
UpdateFinshServlet.java
package hellotest;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jdbcconnet.JDBCDao;
public class UpdateFinshServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
//int id = Integer.valueOf(request.getParameter("id"));
String id1 = String.valueOf(request.getParameter("Pn_name"));
String Pn_name = new String(id1.getBytes("iso8859-1"),"utf8");
String id2 = String.valueOf(request.getParameter("Pn_number"));
String Pn_number = new String(id2.getBytes("iso8859-1"),"utf8");
String id3 = String.valueOf(request.getParameter("Pn_sex"));
String Pn_sex = new String(id3.getBytes("iso8859-1"),"utf8");
String id4 = String.valueOf(request.getParameter("Pn_able"));
String Pn_able = new String(id4.getBytes("iso8859-1"),"utf8");
String id5 = String.valueOf(request.getParameter("Pn_winnum"));
String Pn_winnum = new String(id5.getBytes("iso8859-1"),"utf8");
System.out.println(Pn_winnum+Pn_name+Pn_number+Pn_able);
JDBCDao.update(Pn_name, Pn_number, Pn_sex, Pn_able, Pn_winnum);
// jdbc.update(id1, id2, id3, id4, id5);
response.sendRedirect("FindallServlet");
// super.doGet(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doPost(request, response);
doGet(request, response);
}
}
跳转回findall,更新输出信息;
单个删除
DeleteServlet .java
package hellotest;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jdbcconnet.JDBCDao;
public class DeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
String id = request.getParameter("id");
JDBCDao jdbc = new JDBCDao();
jdbc.deletenum(id);
response.sendRedirect("FindallServlet");
// super.doGet(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doPost(request, response);
doGet(request, response);
}
}
简单的删除然后跳转findall
插入新数据
InsertServlet.java
package hellotest;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jdbcconnet.JDBCDao;
public class InsertServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
String id1 = String.valueOf(request.getParameter("Pn_name"));
String Pn_name = new String(id1.getBytes("iso8859-1"),"utf8");
String id2 = String.valueOf(request.getParameter("Pn_number"));
String Pn_number = new String(id2.getBytes("iso8859-1"),"utf8");
String id3 = String.valueOf(request.getParameter("Pn_sex"));
String Pn_sex = new String(id3.getBytes("iso8859-1"),"utf8");
String id4 = String.valueOf(request.getParameter("Pn_able"));
String Pn_able = new String(id4.getBytes("iso8859-1"),"utf8");
String id5 = String.valueOf(request.getParameter("Pn_winnum"));
String Pn_winnum = new String(id5.getBytes("iso8859-1"),"utf8");
System.out.println(Pn_winnum+Pn_name+Pn_number+Pn_able);
JDBCDao.insert(Pn_name, Pn_number, Pn_sex, Pn_able, Pn_winnum);
response.sendRedirect("FindallServlet");
// super.doGet(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doPost(request, response);
doGet(request, response);
}
}
insert.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.List"%>
<%@page import="modle.user"%>
<%@page import="jdbcconnet.JDBCDao"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>插入页面</title>
</head>
<body>
<table align="center" width="500" border="1" height="90"
bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
<tr align="center" bgcolor="#e1ffc1">
<td><h2>姓名</h2></td>
<td><h2>特工编号</h2></td>
<td><h2>性别</h2></td>
<td><h2>能力</h2></td>
<td><h2>胜出次数</h2></td>
</tr>
<form name="thisform" method="post" action="InsertServlet">
<tr align="center" bgcolor="white">
<td>
<input type="text" id="Pn_name" name="Pn_name" value=""/>
</td>
<td>
<input type="text" id="Pn_number" name="Pn_number" value=""/>
</td>
<td>
<input type="text" id="Pn_sex" name="Pn_sex" value=""/>
</td>
<td>
<input type="text" id="Pn_able" name="Pn_able" value=""/>
<td>
<input type="text" id="Pn_winnum" name="Pn_winnum" value=""/>
</td>
</tr>
<input type="button" bgcolor="white" value="确认插入" align="bottom" colspan="7" onclick="sel()" >
</form>
<script language="javascript">
function sel(){
document.thisform.submit();
}
</script>
</table>
</body>
</html>
主页面点击插入新数据后跳转到insert.jsp,输入信息点击确认输入之后跳转InsertServlet后台执行插入,再跳转FindallServlet
点击按条件查询:
SearchsomeServlet .java
package hellotest;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jdbcconnet.JDBCDao;
public class SearchsomeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
String id1 = String.valueOf(request.getParameter("Pn_name"));
String Pn_name = new String(id1.getBytes("iso8859-1"),"utf8");
String id2 = String.valueOf(request.getParameter("Pn_number"));
String Pn_number = new String(id2.getBytes("iso8859-1"),"utf8");
String id3 = String.valueOf(request.getParameter("Pn_sex"));
String Pn_sex = new String(id3.getBytes("iso8859-1"),"utf8");
String id4 = String.valueOf(request.getParameter("Pn_able"));
String Pn_able = new String(id4.getBytes("iso8859-1"),"utf8");
String id5 = String.valueOf(request.getParameter("Pn_winnum"));
String Pn_winnum = new String(id5.getBytes("iso8859-1"),"utf8");
System.out.println(Pn_name+Pn_number+Pn_sex+Pn_able+Pn_winnum);
String [] a = {Pn_name,Pn_number,Pn_sex,Pn_able,Pn_winnum};
if(a[1] == null || a[1].length() <= 0);
{
System.out.println("Pn_number==null");
}
JDBCDao jdbc = new JDBCDao();
try
{
List list = jdbc.searchsome(Pn_name,Pn_number,Pn_sex,Pn_able,Pn_winnum);
request.setAttribute("list", list);
}
catch(Exception e)
{
e.printStackTrace();
}
//请求转发到指定URL,是服务器端跳转
request.getRequestDispatcher("Searchsome.jsp").forward(request, response);
// super.doGet(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doPost(request, response);
doGet(request, response);
}
}
Searchsome.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.List"%>
<%@page import="modle.user"%>
<%@page import="jdbcconnet.JDBCDao"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>查询页面</title>
<script type="text/javascript">
function CheckAll(elementsA, elementsB) {
for (i = 0; i < elementsA.length; i++) {
if(elementsA[i].checked == true)
{
elementsA[i].checked = false;
}
else
elementsA[i].checked = true;
}
if (elementsB.checked == false) {
for (j = 0; j < elementsA.length; j++) {
elementsA[j].checked = false;
}
}
}
//判断用户是否选择了要删除的记录,如果是,则提示“是否删除”;否则提示“请选择要删除的记录”
function checkdel(delid, formname) {
var flag = false;
for (i = 0; i < delid.length; i++) {
if (delid[i].checked) {
flag = true;
break;
}
}
if (!flag) {
alert("请选择要删除的记录!");
return false;
} else {
if (confirm("确定要删除吗?")) {
formname.submit();
}
}
}
</script>
</head>
<body>
<table align="center" width="500" border="1" height="90"
bordercolor="white" bgcolor=black cellpadding="1" cellspacing="1">
<tr align="center" bgcolor="#e1ffc1">
<td><h2>姓名</h2></td>
<td><h2>特工编号</h2></td>
<td><h2>性别</h2></td>
<td><h2>能力</h2></td>
<td><h2>胜出次数</h2></td>
<td><h2>删除</h2></td>
</tr>
<form name="thisform" method="post" action="SearchsomeServlet">
<tr align="center" bgcolor="white">
<td>
<input type="text" id="Pn_name" name="Pn_name" value="${Pn_name}"/>
</td>
<td>
<input type="text" id="Pn_number" name="Pn_number" value="${Pn_number}"/>
</td>
<td>
<input type="text" id="Pn_sex" name="Pn_sex" value="${Pn_sex}"/>
</td>
<td>
<input type="text" id="Pn_able" name="Pn_able" value="${Pn_able}"/>
<td>
<input type="text" id="Pn_winnum" name="Pn_winnum" value="${Pn_winnum}"/>
</td>
<td>
<input type="text" />
</td>
</tr>
<input type="button" bgcolor="white" value="按条件查询" align="bottom" colspan="7" onclick="sel()" >
</form>
<form name="frm" method="post" action="DeleteAllServlet">
<%
// 获取跑男信息集合
List list = (List) request.getAttribute("list");
// 判断集合是否有效
if (list == null || list.size() < 1) {
out.print("没有数据!");
} else
{
// 遍历跑男集合中的数据
for (int i=0;i<list.size();i++)
{
user us= (user)list.get(i);
%>
<tr align="center" bgcolor="white">
<td><%=us.getPn_name()%></td>
<td><%=us.getPn_number()%></td>
<td><%=us.getPn_sex()%></td>
<td><%=us.getPn_able()%></td>
<td><%=us.getPn_winnum()%></td>
<td><input name="delid" type="checkbox"
class="noborder" value="<%=us.getPn_number()%>"></td>
<%
}
}
%>
<footer>
<input name="checkbox" type="checkbox" class="noborder"
onClick="CheckAll(frm.delid,frm.checkbox)"> [全选/反选] [
<a style="color:red;cursor:pointer;" onClick="checkdel(frm.delid,frm)">删除</a>]
<div id="ch" style="display: none">
<input name="delid" type="checkbox" value="0">
</div>
<!--层ch用于放置隐藏的checkbox控件,因为当表单中只是一个checkbox控件时,应用javascript获得其length属性值为undefine-->
</footer>
</form>
<script language="javascript">
function sel(){
document.thisform.submit();
}
</script>
</table>
</body>
</html>
这里除了查询还加入了全选以及删除的CheckBox,便于查询出相应的条件删除;
查询功能是先前台展示,输入值点击查询之后,跳转SearchsomeServlet.java 处理数据,将数据展示回Searchsome.jsp,点击全选框等处理函数在Searchsome.jsp中,点击删除的时候,跳转到DeleteAllServlet.java处理选中的数据,DeleteAllServlet.java处理完后跳转回FindallServlet.java中。
DeleteAllServlet.java
package hellotest;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jdbcconnet.JDBCDao;
public class DeleteAllServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
String ID[]=request.getParameterValues("delid");
for(int i=0;i<ID.length;i++)
{
System.out.println(ID[i]);
}
JDBCDao jdbc = new JDBCDao();
jdbc.deletelots(ID);
response.sendRedirect("FindallServlet");
// super.doGet(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doPost(request, response);
doGet(request, response);
}
}
最后,新建servelet的时候,记得到WebContent->WEB-INF->LIB->web.xml文件下注册
<servlet>
<servlet-name>FindallServlet</servlet-name>
<servlet-class>hellotest.FindallServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FindallServlet</servlet-name>
<url-pattern>/FindallServlet</url-pattern>
</servlet-mapping>
时间比较紧,很多代码没有优化,不过应该比较容易理解
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。