項目依賴包:jdbc數據庫包html
jsp發出請求到web--java
web接收請求並匹配請求對應的servlet--web
servlet調用數據庫dao層操做數據庫--sql
若是有數據傳遞,放到request或者session中數據庫
重點:servlet會執行doService()方法來判斷調用doGet()或者doPost() 服務器
一、新建web項目session
二、新建實體類 User.javaoracle
1 package com.mart.bean;
2
3 public class User {
4
5 //屬性
6 private Integer id;
7 private String uname;
8 private String upass;
9 private String tname;
10 private String utype;
11 //屬性訪問
12 public Integer getId() {
13 return id;
14 }
15 public void setId(Integer id) {
16 this.id = id;
17 }
18 public String getUname() {
19 return uname;
20 }
21 public void setUname(String uname) {
22 this.uname = uname;
23 }
24 public String getUpass() {
25 return upass;
26 }
27 public void setUpass(String upass) {
28 this.upass = upass;
29 }
30 public String getTname() {
31 return tname;
32 }
33 public void setTname(String tname) {
34 this.tname = tname;
35 }
36 public String getUtype() {
37 return utype;
38 }
39 public void setUtype(String utype) {
40 this.utype = utype;
41 }
42
43 //構造
44 public User() {
45 super();
46 // TODO Auto-generated constructor stub
47 }
48 public User(Integer id, String uname, String upass, String tname,
49 String utype) {
50 super();
51 this.id = id;
52 this.uname = uname;
53 this.upass = upass;
54 this.tname = tname;
55 this.utype = utype;
56 }
57 public User(String uname, String upass, String tname, String utype) {
58 super();
59 this.uname = uname;
60 this.upass = upass;
61 this.tname = tname;
62 this.utype = utype;
63 }
64 //更新操做改善,不加name更新
65 public User(Integer id, String upass, String tname, String utype) {
66 super();
67 this.id = id;
68 this.upass = upass;
69 this.tname = tname;
70 this.utype = utype;
71 }
72
73 //tostring
74 @Override
75 public String toString() {
76 return "User [id=" + id + ", uname=" + uname + ", upass=" + upass
77 + ", tname=" + tname + ", utype=" + utype + "]";
78 }
79
80
81 }
三、編寫操做接口 UserDao.javaapp
package com.mart.dao;
import java.util.List;
import com.mart.bean.User;
public interface UserDao {
/**
* @param u 待添加的用戶
* @return 0-添加失敗 1-添加成功
*/
public Integer addUser(User u);
/**
* @param id 待刪除用戶編號
* @return 0-刪除失敗 1-刪除成功
*/
public Integer delUser(Integer id);
/**
* @param u 要添加的用戶
* @return 0-更新失敗 1-更新成功
*/
public Integer updUser(User u);
/**
* @param id 待查找的用戶編號
* @return 要查找的用戶
*/
public User findUserById(Integer id);
/**
* @return 所有用戶列表
*/
public List<User> FindAllUser();
}
四、編寫數據庫操做工具類 DBUtil.javajsp
package com.mart.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
//獲取oracle數據庫鏈接
public Connection getCurrentConnection(){
//初始返回值
Connection conn = null;
try {
//加載驅動
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl",
"mart", "java");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
五、編寫接口實現 UserDaoImpl.java
package com.mart.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mart.bean.User;
import com.mart.dao.UserDao;
import com.mart.util.DBUtil;
public class UserDaoImpl implements UserDao{
@Override
public Integer addUser(User u) {
// TODO Auto-generated method stub
int res=-1;
//鏈接數據庫
DBUtil dbUtil = new DBUtil();
Connection conn = dbUtil.getCurrentConnection();
//添加語句
PreparedStatement ps = null;
String sql = "insert into mart_user values(seq_mart_user.nextval,?,?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, u.getUname());
ps.setString(2, u.getUpass());
ps.setString(3, u.getTname());
ps.setString(4, u.getUtype());
//執行
res = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(ps!=null)ps.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return res;
}
@Override
public Integer delUser(Integer id) {
// TODO Auto-generated method stub
int res=-1;
//鏈接數據庫
DBUtil dbUtil = new DBUtil();
Connection conn = dbUtil.getCurrentConnection();
//刪除語句
PreparedStatement ps = null;
String sql = "delete from mart_user where id=?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
//執行
res = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(ps!=null)ps.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return res;
}
@Override
public Integer updUser(User u) {
// TODO Auto-generated method stub
int res=-1;
//鏈接數據庫
DBUtil dbUtil = new DBUtil();
Connection conn = dbUtil.getCurrentConnection();
//更新語句
PreparedStatement ps = null;
String sql = "update mart_user set upass=?,tname=?,utype=? where id=?";
try {
ps = conn.prepareStatement(sql);
// ps.setString(1, u.getUname());--完善更改,不設置名字
ps.setString(1, u.getUpass());
ps.setString(2, u.getTname());
ps.setString(3, u.getUtype());
ps.setInt(4, u.getId());
//執行
res = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(ps!=null)ps.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return res;
}
@Override
public User findUserById(Integer id) {
// TODO Auto-generated method stub
User u = null;
//鏈接數據庫
DBUtil dbUtil = new DBUtil();
Connection conn = dbUtil.getCurrentConnection();
//查找語句
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from mart_user where id=?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
//獲取結果
rs = ps.executeQuery();
rs.next();
u = new User(rs.getInt("id"), rs.getString("uname"), rs.getString("upass"),
rs.getString("tname"), rs.getString("utype"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null)rs.close();
if(ps!=null)ps.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return u;
}
@Override
public List<User> FindAllUser() {
// TODO Auto-generated method stub
List<User> uList = new ArrayList<User>();
//鏈接數據庫
DBUtil dbUtil = new DBUtil();
Connection conn = dbUtil.getCurrentConnection();
//查找語句
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from mart_user";
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
//獲取結果
while(rs.next()){
uList.add(new User(rs.getInt("id"), rs.getString("uname"), rs.getString("upass"),
rs.getString("tname"), rs.getString("utype")));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null)rs.close();
if(ps!=null)ps.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return uList;
}
}
六、編寫測試文件,測試是否底層數據操做成功 UserTest.java
package com.mart.test;
import java.util.ArrayList;
import java.util.List;
import com.mart.bean.User;
import com.mart.dao.impl.UserDaoImpl;
public class UserTest {
//主測試
public static void main(String[] args) {
//實例化實現
UserDaoImpl udi = new UserDaoImpl();
// addTest(udi);
// delTest(udi);
// updTest(udi);
// findTest(udi);
// findAllTest(udi);
}
//添加測試
private static void addTest(UserDaoImpl udi){
//設置添加對象
User u = new User("wangwu", "0002", "王五", "0");
Integer res = udi.addUser(u);
if(res>0){
System.out.println("添加成功");
}else{
System.out.println("添加失敗");
}
}
//刪除測試
private static void delTest(UserDaoImpl udi){
//設置刪除編號
int id = 2;
int res = udi.delUser(id);
if(res>0){
System.out.println("刪除成功");
}else{
System.out.println("刪除失敗");
}
}
//更新測試
private static void updTest(UserDaoImpl udi){
//設置更新對象
User u = new User(9, "tiqi", "123", "田七", "0");
Integer res = udi.updUser(u);
if(res>0){
System.out.println("更新成功");
}else{
System.out.println("更新失敗");
}
}
//id查找測試
private static void findTest(UserDaoImpl udi){
//設置查找id
int id = 6;
User u =udi.findUserById(id);
System.out.println(u);
}
//所有查找
private static void findAllTest(UserDaoImpl udi){
//新建保存集
List<User> uList = new ArrayList<User>();
uList = udi.FindAllUser();
for (User user : uList) {
System.out.println(user);
}
}
}
七、編寫jsp顯示界面,僅以用戶顯示界面爲例 show.jsp
<%@page import="com.mart.bean.User"%>
<%@page import="com.mart.dao.impl.UserDaoImpl"%>
<%@page import="com.mart.dao.UserDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>用戶顯示</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">
</head>
<body>
<div class="top" style="width: 800px;height: 50px">
<h3>用戶顯示界面</h3>
</div>
<div class="center" style="width: 800px;height: auto;">
<table border="1">
<tr>
<th>用戶編號</th>
<th>用戶帳戶</th>
<th>用戶密碼</th>
<th>真實名字</th>
<th>用戶類型</th>
<th>用戶操做</th>
</tr>
<tr>
<%
//實例化接口
UserDao udi = new UserDaoImpl();
List<User> uList = new ArrayList<User>();
uList = udi.FindAllUser();
for(User u : uList){
%>
<tr>
<td>
<%=u.getId() %>
</td>
<td>
<%=u.getUname() %>
</td>
<td>
<%=u.getUpass() %>
</td>
<td>
<%=u.getTname() %>
</td>
<td>
<%=u.getUtype().equals("0")?"普通":"管理員" %>
</td>
<td>
<a href="upd.jsp?id=<%=u.getId() %>&uname=<%=u.getUname()%>&
upass=<%=u.getUpass() %>&tname=<%=u.getTname()%>&
utype=<%=u.getUtype() %>">更新</a>
/<a href="../del.do?id=<%=u.getId() %>">刪除</a>
</td>
</tr>
<%
}
%>
</tr>
<tr>
<td colspan="6" align="center">
<a href="./add.jsp">用戶添加</a>
</td>
</tr>
</table>
</div>
</body>
</html>
八、編寫servlet文件 ShowAction.java
package com.mart.controller;
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 com.mart.bean.User;
import com.mart.dao.UserDao;
import com.mart.dao.impl.UserDaoImpl;
public class ShowAction extends HttpServlet {
//解析請求方法
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("用戶展現界面:com.mart.controller.show");
//獲取用戶列表
UserDao udi = new UserDaoImpl();
List<User> uList = udi.FindAllUser();
//傳遞用戶列表到前斷
//此處用session則服務器過載
request.setAttribute("uList", uList);
//轉到顯示界面
//不能重定向,會丟失數據
response.sendRedirect("user/show.jsp");
}
//響應請求方法
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* get和post行爲相同時,直接設置內部調用便可
*/
this.doGet(request, response);
}
}
九、在web.xml裏註冊servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>淘寶商城servlet-xml配置</display-name>
<servlet>
<description>這是個人用戶顯示界面,顯示所有用戶</description>
<display-name>user_show</display-name>
<servlet-name>ShowAction</servlet-name>
<servlet-class>com.mart.controller.ShowAction</servlet-class>
</servlet>
<servlet>
<description>這是個人用戶添加界面</description>
<display-name>user_add</display-name>
<servlet-name>AddAction</servlet-name>
<servlet-class>com.mart.controller.AddAction</servlet-class>
</servlet>
<servlet>
<description>這是個人用戶刪除界面</description>
<display-name>user_delete</display-name>
<servlet-name>DeleteAction</servlet-name>
<servlet-class>com.mart.controller.DeleteAction</servlet-class>
</servlet>
<servlet>
<description>這是個人用戶更新界面</description>
<display-name>user_update</display-name>
<servlet-name>UpdateAction</servlet-name>
<servlet-class>com.mart.controller.UpdateAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowAction</servlet-name>
<url-pattern>/show.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AddAction</servlet-name>
<url-pattern>/add.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DeleteAction</servlet-name>
<url-pattern>/del.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UpdateAction</servlet-name>
<url-pattern>/upd.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>