package com.huai.dbc;
import java.sql.DriverManager;
public class DBConnection {
private final String Driver = "com.mysql.jdbc.Driver";
private final String url = "jdbc:mysql://localhost:3306/test";
private java.sql.Connection con;
public DBConnection()throws Exception{
Class.forName(Driver);
con = DriverManager.getConnection(url, "root", "mysql");
}
public java.sql.Connection getConnection(){
return this.con;
}
public void close()throws Exception{
if(this.con != null){
this.con.close();
}
}
// public static void main(String args[]){
// try {
// new Connection().getConnection();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
}
package com.huai.dao;
import java.util.*;
import com.huai.vo.*;
/*
* 数据库操作的接口,里面的方法分别是增删改查
*/
public interface IDAOImpl {
public ArrayList
public boolean deleteData(String keyWord)throws Exception;
public boolean updateData(User user)throws Exception;
public boolean insertData(User user)throws Exception;
}
package com.huai.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.huai.dao.IDAOImpl;
import com.huai.vo.User;
/*
* 此类实现数据库的操作接口
* 注意:在此还没有实现数据库连接的实例化(对象),
* 只是提供了一个构造放法,以便在后面的实例化后传入
*/
public class UserDAOImpl implements IDAOImpl{
Connection con = null;
PreparedStatement state = null;
public UserDAOImpl(Connection con) {
this.con = con;
}
@Override
public ArrayList
String sql = "select * from member where hobby like ? or name like ?";
ResultSet rs = null;
ArrayList
this.state = con.prepareStatement(sql);
//百分号表示代表未知的字符
this.state.setString(1, "%"+keyWord+"%");
this.state.setString(2, "%"+keyWord+"%");
rs = this.state.executeQuery();
while(rs.next()){
User user = new User();
user.setId(rs.getString(1));
user.setName(rs.getString(2));
user.setSex(rs.getString(3));
user.setHobby(rs.getString(4));
users.add(user);
}
return users;
}
@Override
public boolean deleteData(String keyWord) throws Exception {
boolean flag = false;
String sql = "delete from member where id = ?";
this.state = con.prepareStatement(sql);
this.state.setString(1, keyWord);
int i = this.state.executeUpdate();
if(i != 0){
flag = true;
}
return flag;
}
@Override
public boolean updateData(User user) throws Exception {
boolean flag = false;
String sql = "update member set name=?, sex=?, hobby=? where id=?";
this.state = con.prepareStatement(sql);
this.state.setString(1, user.getName());
this.state.setString(2, user.getSex());
this.state.setString(3, user.getHobby());
this.state.setString(4, user.getId());
int i = this.state.executeUpdate();
if(i != 0){
flag = true;
}
return flag;
}
@Override
public boolean insertData(User user) throws Exception {
boolean flag = false;
String sql = "insert into member(id, name, sex, hobby) values(?, ?, ?, ?)";
this.state = con.prepareStatement(sql);
this.state.setString(1, user.getId());
this.state.setString(2, user.getName());
this.state.setString(3, user.getSex());
this.state.setString(4, user.getHobby());
int i = this.state.executeUpdate();
if(i != 0){
flag = true;
}
return flag;
}
}
package com.huai.dao.impl;
import java.util.ArrayList;
import com.huai.dao.IDAOImpl;
import com.huai.dbc.DBConnection;
import com.huai.vo.User;
/*
* 这个类和UserDAOImpl都实现了IDAOImpl接口
* 但是这个类是UserDAOImpl的代理类,目的是在大的项目中更好地进行代码的管理
*/
public class UserDAOImplProxy implements IDAOImpl{
UserDAOImpl userDAOImpl = null;
DBConnection dBConnection = null;
public UserDAOImplProxy()throws Exception{
dBConnection = new DBConnection();
userDAOImpl = new UserDAOImpl(dBConnection.getConnection());
}
@Override
public ArrayList
ArrayList
try{
list = userDAOImpl.queryData(keyWord);
}catch(Exception e){
throw e;
}
return list;
}
@Override
public boolean deleteData(String keyWord) throws Exception {
return userDAOImpl.deleteData(keyWord);
}
@Override
public boolean updateData(User user) throws Exception {
return userDAOImpl.updateData(user);
}
@Override
public boolean insertData(User user) throws Exception {
return userDAOImpl.insertData(user);
}
}
package com.huai.dao.factory;
import com.huai.dao.impl.UserDAOImplProxy;
public class DaoFactory {
public static UserDAOImplProxy getUserDAOImplProxy() throws Exception{
return new UserDAOImplProxy();
}
}
package com.huai.servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.huai.dao.factory.DaoFactory;
import com.huai.dao.impl.UserDAOImplProxy;
import com.huai.vo.User;
//此处是新建servlet文件是自动加上的,如果没有它,需要在项目中增加web.xml文件,并且进行相关的映射配置
//否则的话jsp文件找不到这个MyServlet文件
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
UserDAOImplProxy userDAOImplProxy = null;
public MyServlet() {
try {
userDAOImplProxy = DaoFactory.getUserDAOImplProxy();
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("gbk");
//status表示的是hidden域,目的是辨清是哪张表所发出的命令
String status = request.getParameter("status");
if ("search".equals(status)) {
try {
ArrayList
list = userDAOImplProxy.queryData(request
.getParameter("keyWord"));
request.setAttribute("users", list);
request.getRequestDispatcher("searchPage.jsp").forward(request,
response);
} catch (Exception e) {
e.printStackTrace();
}
} else if ("insert".equals(status)) {
String info = "";
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String hobby = request.getParameter("hobby");
if (name == null || sex == null || hobby == null || "".equals(name)
|| "".equals(sex) || "".equals(hobby)) {
info = "The information is not completed";
} else {
User user = new User();
user.setName(name);
user.setSex(sex);
user.setHobby(hobby);
try {
if (userDAOImplProxy.insertData(user)) {
info = "succeed to insert!";
} else {
info = "failed to insert!";
}
} catch (Exception e) {
e.printStackTrace();
}
}
request.setAttribute("info", info);
request.getRequestDispatcher("insertPage.jsp").forward(request,
response);
} else if ("delete".equals(status)) {
String keyWord = request.getParameter("id");
try {
String info = "";
if (userDAOImplProxy.deleteData(keyWord)) {
info = "succeed to delete!";
} else {
info = "failed to delete!";
}
request.setAttribute("info", info);
//注意:这里的是内部跳转,也就是服务器端的跳转。外在表现是地址栏没变。好处就是提高性能
request.getRequestDispatcher("searchPage.jsp").forward(request,
response);
} catch (Exception e) {
e.printStackTrace();
}
} else if ("update".equals(status)) {
String info = "";
String path = null;
String id = request.getParameter("id");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String hobby = request.getParameter("hobby");
if (name == null || sex == null || hobby == null || "".equals(name)
|| "".equals(sex) || "".equals(hobby)) {
info = "The information is not completed";
path = "updatePage.jsp";
} else {
User user = new User();
user.setId(id);
user.setName(name);
user.setSex(sex);
user.setHobby(hobby);
try {
if (userDAOImplProxy.updateData(user)) {
info = "succeed to update!";
path = "searchPage.jsp";
} else {
info = "failed to update!";
path = "updatePage.jsp";
}
} catch (Exception e) {
e.printStackTrace();
}
}
request.setAttribute("info", info);
request.getRequestDispatcher(path).forward(request,
response);
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
package com.huai.vo;
public class User {
private String id;
private String name;
private String sex;
private String hobby;
public User(){
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="GB18030"%>
<%@ page language="java" contentType="text/html; charset=gbk "
pageEncoding="GB18030"%>
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="GB18030"%>
<%@page import="java.util.*" %>
id | name | sex | hobby | ||
${de.id} | ${de.name} | ${de.sex} | ${de.hobby} | 修改 | 删除 |
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
num | id | name | sex | hobby | 修改 | 删除 |