简单Java Web项目 只含增删改查

大一写的项目,闲着也是闲着,码一下吧,只提供部分代码,仅用于学习交流使用,禁止转载,

运行效果部分如下
在这里插入图片描述
在这里插入图片描述
项目结构图如下图右边项目资源管理器,项目名:Lab
在这里插入图片描述

第一步:连接数据库

package com.txl.utils;
//此文件用来借助jdbc连接数据库
import java.sql.*;

public class DBUtils {
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public Connection getConnection() {
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/txl?characterEncoding=utf-8&useSSL=false", "root", "123");//数据库连接的钥匙
			return connection;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
		try {
			if (resultSet != null) {
				resultSet.close();
			}
			if (statement != null) {
				statement.close();
			}
			if (connection != null) {
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

第二步 创建实体类(model)以及实体类的具体实现类

package com.txl.entity;//定义 联系人的   相关变量

public class Txl {
	public int getTxl_id() {
		return txl_id;
	}


	public void setTxl_id(int txl_id) {
		this.txl_id = txl_id;
	}


	public String getTxl_name() {
		return txl_name;
	}


	public void setTxl_name(String txl_name) {
		this.txl_name = txl_name;
	}


	public String getTxl_phone() {
		return txl_phone;
	}


	public void setTxl_phone(String txl_phone) {
		this.txl_phone = txl_phone;
	}


	public String getTxl_qq() {
		return txl_qq;
	}


	public void setTxl_qq(String txl_qq) {
		this.txl_qq = txl_qq;
	}


	public String getTxl_address() {
		return txl_address;
	}


	public void setTxl_address(String txl_address) {
		this.txl_address = txl_address;
	}


	public int getTxl_jinji() {
		return txl_jinji;
	}


	public void setTxl_jinji(int txl_jinji) {
		this.txl_jinji = txl_jinji;
	}


	public String getTxl_dizhi() {
		return txl_dizhi;
	}


	public void setTxl_dizhi(String txl_dizhi) {
		this.txl_dizhi = txl_dizhi;
	}


	public String getTxl_time() {
		return txl_time;
	}


	public void setTxl_time(String txl_time) {
		this.txl_time = txl_time;
	}


	public String getTxl_if() {
		return txl_if;
	}


	public void setTxl_if(String txl_if) {
		this.txl_if = txl_if;
	}


	private int txl_id;
	private String txl_name;
	private String txl_phone;
	private String txl_qq;
	private String txl_address;
	private int txl_jinji;
	private String txl_dizhi;
	private String txl_time;
	private String txl_if;
	

	public Txl() {

	}

	
	@Override
	public String toString() {
		return "Txl{" + "txl_id=" + txl_id + ", txl_name='" + txl_name + '\'' + ", txl_phone='" + txl_phone + '\''
				+ ", txl_qq='" + txl_qq + '\'' + ", txl_address='" + txl_address + '\'' + ", txl_jinji=" + txl_jinji
				+ '}';
	}
}

package com.txl.dao;

import com.txl.entity.Txl;
import com.txl.utils.DBUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TxlDao extends DBUtils {
	public List<Txl> getAllTxl() /*获取所有联系人*/{
		String sql = "select * from txl";
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Txl> txls = new ArrayList<>();
		try {
			preparedStatement = getConnection().prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Txl txl = new Txl();
				txl.setTxl_id(resultSet.getInt("txl_id"));
				txl.setTxl_name(resultSet.getString("txl_name"));
				txl.setTxl_phone(resultSet.getString("txl_phone"));
				txl.setTxl_qq(resultSet.getString("txl_qq"));
				txl.setTxl_address(resultSet.getString("txl_address"));
			
				txl.setTxl_dizhi(resultSet.getString("txl_dizhi"));
				txl.setTxl_time(resultSet.getString("txl_time"));
				txl.setTxl_if(resultSet.getString("txl_if"));
				txls.add(txl);
			}
			return txls;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		} finally {
			closeAll(getConnection(), preparedStatement, resultSet);
		}
		return null;
	}

	public Txl getTxlById(String id) {
		String sql = "select * from txl where txl_id = ?";
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Txl txl = null;
		try {
			preparedStatement = getConnection().prepareStatement(sql);
			preparedStatement.setInt(1, Integer.parseInt(id));
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				txl = new Txl();
				txl.setTxl_id(resultSet.getInt("txl_id"));
				txl.setTxl_name(resultSet.getString("txl_name"));
				txl.setTxl_phone(resultSet.getString("txl_phone"));
				txl.setTxl_qq(resultSet.getString("txl_qq"));
				txl.setTxl_address(resultSet.getString("txl_address"));
		
				txl.setTxl_dizhi(resultSet.getString("txl_dizhi"));
				txl.setTxl_time(resultSet.getString("txl_time"));
				txl.setTxl_if(resultSet.getString("txl_if"));
			}
			return txl;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		} finally {
			closeAll(getConnection(), preparedStatement, resultSet);
		}
		return null;
	}

	public int delTxlById(String id) {
		String sql = "delete from txl where txl_id = ?";
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = getConnection().prepareStatement(sql);
			preparedStatement.setInt(1, Integer.parseInt(id));
			return preparedStatement.executeUpdate();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		} finally {
			closeAll(getConnection(), preparedStatement, null);
		}
		return 0;
	}

	public int updateTxlById(String id, String name, String phone, String qq, String address) {
		String sql = "update txl set txl_name = ?,txl_phone=?,txl_qq=?,txl_address=? where txl_id = ?";
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = getConnection().prepareStatement(sql);
			preparedStatement.setString(1, name);
			preparedStatement.setString(2, phone);
			preparedStatement.setString(3, qq);
			preparedStatement.setString(4, address);
			preparedStatement.setInt(5, Integer.parseInt(id));
			return preparedStatement.executeUpdate();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		} finally {
			closeAll(getConnection(), preparedStatement, null);
		}
		return 0;
	}

	public int updateTxlToJinJiById(String id) {
		String sql = "update txl set txl_jinji = 1 where txl_id = ?";
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = getConnection().prepareStatement(sql);
			preparedStatement.setInt(1, Integer.parseInt(id));
			return preparedStatement.executeUpdate();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		} finally {
			closeAll(getConnection(), preparedStatement, null);
		}
		return 0;
	}

	public int addTxl(String name, String phone, String qq, String address, String dizhi,String time,String ifs ) {
		String sql = "insert into txl(txl_name,txl_phone,txl_qq,txl_address,txl_dizhi,txl_time,txl_if) values(?,?,?,?,?,?,?)";
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = getConnection().prepareStatement(sql);
			preparedStatement.setString(1, name);
			preparedStatement.setString(2, phone);
			preparedStatement.setString(3, qq);
			preparedStatement.setString(4, address);
			preparedStatement.setString(5, dizhi);
			preparedStatement.setString(6, time);
			preparedStatement.setString(7, ifs);
			
			return preparedStatement.executeUpdate();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		} finally {
			closeAll(getConnection(), preparedStatement, null);
		}
		return 0;
	}

	public int insertDelTxl(Txl txl) {
		String sql = "insert into deltxl(txl_id,txl_name,txl_phone,txl_qq,txl_address,txl_jinji) values(?,?,?,?,?,?)";
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = getConnection().prepareStatement(sql);
			preparedStatement.setInt(1, txl.getTxl_id());
			preparedStatement.setString(2, txl.getTxl_name());
			preparedStatement.setString(3, txl.getTxl_phone());
			preparedStatement.setString(4, txl.getTxl_qq());
			preparedStatement.setString(5, txl.getTxl_address());
			preparedStatement.setInt(6, txl.getTxl_jinji());
			return preparedStatement.executeUpdate();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		} finally {
			closeAll(getConnection(), preparedStatement, null);
		}
		return 0;
	}

	public List<Txl> getAllDelTxl() {
		String sql = "select * from deltxl";
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Txl> txls = new ArrayList<>();
		try {
			preparedStatement = getConnection().prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Txl txl = new Txl();
				txl.setTxl_id(resultSet.getInt("txl_id"));
				txl.setTxl_name(resultSet.getString("txl_name"));
				txl.setTxl_phone(resultSet.getString("txl_phone"));
				txl.setTxl_qq(resultSet.getString("txl_qq"));
				txl.setTxl_address(resultSet.getString("txl_address"));
				txl.setTxl_jinji(resultSet.getInt("txl_jinji"));
				txls.add(txl);
			}
			return txls;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		} finally {
			closeAll(getConnection(), preparedStatement, resultSet);
		}
		return null;
	}

	public Txl searchTxl(String name) {
		String sql = "select * from txl where txl_name like ?";
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Txl txl = null;
		try {
			preparedStatement = getConnection().prepareStatement(sql);
			preparedStatement.setString(1, "%" + name + "%");
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				txl = new Txl();
				txl.setTxl_id(resultSet.getInt("txl_id"));
				txl.setTxl_name(resultSet.getString("txl_name"));
				txl.setTxl_phone(resultSet.getString("txl_phone"));
				txl.setTxl_qq(resultSet.getString("txl_qq"));
				txl.setTxl_address(resultSet.getString("txl_address"));
				txl.setTxl_jinji(resultSet.getInt("txl_jinji"));
			}
			return txl;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		} finally {
			closeAll(getConnection(), preparedStatement, resultSet);
		}
		return null;
	}

}

第三步 编写jsp页面用于浏览器显示,用户输入数据后转到对应的servlet进行业务逻辑处理

举例修改部分

<%@ page import="com.txl.entity.Txl"%>
<%@ page import="com.txl.dao.TxlDao"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>修改</title>
</head>
<body style="text-align: center;">
	<%
		request.setCharacterEncoding("utf-8");
	response.setContentType("text/html;charset=utf-8");
	String id = request.getParameter("id");
	Txl txl = new TxlDao().getTxlById(id);
	%>
	<form action="./Update" method="post">
		<h1>更新</h1>
		<input type="hidden" name="txl_id" value="<%=txl.getTxl_id()%>"><br />
		书名:<input type="text" name="txl_name" placeholder="名字"
			value="<%=txl.getTxl_name()%>"><br /> 电话号码:<input type="text"
			name="txl_phone" placeholder="电话号码" value="<%=txl.getTxl_phone()%>"><br />
		QQ:<input type="text" name="txl_qq" placeholder="QQ"
			value="<%=txl.getTxl_qq()%>"><br /> 借书人:<input type="text"
			name="txl_address" placeholder="地址" value="<%=txl.getTxl_address()%>"><br />
		<input type="submit" value="更新">
	</form>
</body>
</html>

第四步 编写servlet 实现页面显示和逻辑处理的代码分离

举例修改部分

package com.txl.controller;

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 java.io.IOException;

import com.txl.dao.TxlDao;

@WebServlet(name = "Update", urlPatterns = "/Update")
public class Update extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String id = request.getParameter("txl_id");
		String name = request.getParameter("txl_name");
		String phone = request.getParameter("txl_phone");
		String qq = request.getParameter("txl_qq");
		String address = request.getParameter("txl_address");
		int res = new TxlDao().updateTxlById(id, name, phone, qq, address);
		if (res == 0) {
			response.getWriter().print("<script>alert('更新失败!');window.location.href='./index.jsp';</script>");
		} else if (res == 1) {
			response.getWriter().print("<script>alert('更新成功!');window.location.href='./index.jsp';</script>");
		}
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值