mvc增删改查

本文档详细介绍了如何使用MVC框架进行数据库的增删改查操作,涵盖了从导入相关库到配置数据库连接,再到DAO层、DispatcherServlet的设置,以及自定义标签如checkboxTag、SelectTag、PageTag的使用,最后展示了jsp页面studentEdit.jsp的设计和mvc.xml的配置过程。
摘要由CSDN通过智能技术生成

在这里插入图片描述
先导入架包
在这里插入图片描述
连接数据库
DBAccess

package com.yq.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 提供了一组获得或关闭数据库对象的方法
 * 
 */
public class DBAccess {
	private static String driver;
	private static String url;
	private static String user;
	private static String password;

	static {// 静态块执行一次,加载 驱动一次
		try {
			InputStream is = DBAccess.class
					.getResourceAsStream("config.properties");

			Properties properties = new Properties();
			properties.load(is);

			driver = properties.getProperty("driver");
			url = properties.getProperty("url");
			user = properties.getProperty("user");
			password = properties.getProperty("pwd");

			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获得数据连接对象
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		try {
			Connection conn = DriverManager.getConnection(url, user, password);
			return conn;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public static void close(ResultSet rs) {
		if (null != rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Statement stmt) {
		if (null != stmt) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Connection conn) {
		if (null != conn) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Connection conn, Statement stmt, ResultSet rs) {
		close(rs);
		close(stmt);
		close(conn);
	}

	public static boolean isOracle() {
		return "oracle.jdbc.driver.OracleDriver".equals(driver);
	}

	public static boolean isSQLServer() {
		return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
	}
	
	public static boolean isMysql() {
		return "com.mysql.jdbc.Driver".equals(driver);
	}

	public static void main(String[] args) {
		Connection conn = DBAccess.getConnection();
		DBAccess.close(conn);
		System.out.println("isOracle:" + isOracle());
		System.out.println("isSQLServer:" + isSQLServer());
		System.out.println("isMysql:" + isMysql());
		System.out.println("数据库连接(关闭)成功");
	}
}

在这里插入代码片

StudentDao

package com.yq.dao;

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.yq.entity.Student;
import com.yq.entity.Teacher;
import com.yq.util.BaseDao;
import com.yq.util.DBAccess;
import com.yq.util.PageBean;
import com.yq.util.StringUtils;

public class StudentDao extends BaseDao<Student>{
	/**
	 * 用户查询方法(带分页的)
	 * @param book
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Student> list(Student stu, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql = "select * from student where true ";
		String name=stu.getName();
		String tname=stu.getTeacher();
		String banji=stu.getBanji();
		String hobby=stu.getHobby();
		int id = stu.getId();
		if(StringUtils.isNotBlank(name)) {
			sql += " and name like '%"+name+"%'";
		}
//		if(StringUtils.isNotBlank(tname)) {
//			sql += " and teacher like '%"+tname+"%'";
//		}
//		if(StringUtils.isNotBlank(banji)) {
//			sql += " and banji like '%"+banji+"%'";
//		}
//		if(StringUtils.isNotBlank(hobby)) {
//			sql += " and hobby like '%"+hobby+"%'";
//		}
		if(id != 0) {
			sql += " and id ="+id;
		}
		return super.executeQuery(sql, Student.class, pageBean);
	}
	/**
	 * 拿到教师表里面的所有数据
	 * @return
	 */
	public List<Teacher> listtea() {
		Connection con=null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Teacher> list=new ArrayList<>();
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("select * from teacher");
			rs=ps.executeQuery();
			while(rs.next()) {
				Teacher tea=new Teacher();
				tea.setTid(rs.getInt("tid"));
				tea.setName(rs.getString("name"));
				list.add(tea);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	/**
	 * 拿到爱好表里面的所有数据
	 * @return
	 */
	public List<Hobby> listhobby() {
		Connection con=null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Hobby> list=new ArrayList<>();
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("select * from hobby");
			rs=ps.executeQuery();
			while(rs.next()) {
				Hobby tea=new Hobby();
				tea.setId(rs.getInt("id"));
				tea.setName(rs.getString("name"));
				list.add(tea);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	/**
	 * 拿到所有班级表的数据
	 * @return
	 */
	public List<Banji> listbanji() {
		Connection con=null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Banji> list=new ArrayList<>();
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("select * from banji");
			rs=ps.executeQuery();
			while(rs.next()) {
				Banji bj=new Banji();
				bj.setCid(rs.getInt("cid"));
				bj.setName(rs.getString("name"));
				list.add(bj);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	
	/**
	 * 修改方法
	 * @param book
	 * @return
	 * @throws SQLException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public int edit (Student stu) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAcc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值