主要配置
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 runman = new ArrayList();
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 runman = new ArrayList();
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;iif(a[i] == null || a[i].length() <= 0)
//循环判断,得到不为空的文本框的个数
num--;
}
if(num==0)
{
sql = "select * from information";
//如果没有输入,直接全量搜索
}
else if (num==1)
{
//如果只有一个查询条件,则不需要加入and
for(int i=0;iif(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;iif(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 runman = new ArrayList();
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 runman = new ArrayList();
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"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"%>
<%@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;iget (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%>