mvc版增删查改

这篇博客详细介绍了如何使用MVC架构进行数据库操作,包括增删查改的步骤。首先,从配置util包连接数据库开始,接着定义Student实体类,然后编写DAO和Biz层接口及实现。随后,创建servlet包处理HTTP请求,最后进行web.xml配置和界面布局,如index.jsp、add.jsp和update.jsp等。
摘要由CSDN通过智能技术生成

增删查改的大概安排

写增删查改分别 由三张表组成 ,由学生表,教师表,班级表, 有util entity dao biz Servlet 等包

第一步写util包先连接数据库里

面有DBAccess.java

/**
 * 提供了一组获得或关闭数据库对象的方法
 * 
 */
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(PreparedStatement ps) {
		if (null != ps) {
			try {
				ps.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 con, PreparedStatement ps, ResultSet rs) {
		close(rs);
		close(ps);
		close(con);
	}

	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.cj.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("数据库连接(关闭)成功");
	}
}

连接数据库还需要config.properties:

#mysql5
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost/mysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
user=root
pwd=123

第二步定义一个Student实体类

**
public class student {
	private int sid;
	private String sname;
	private String sjy;
	private String sbj;
	private String sah;
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getSjy() {
		return sjy;
	}
	public void setSjy(String sjy) {
		this.sjy = sjy;
	}
	public String getSbj() {
		return sbj;
	}
	public void setSbj(String sbj) {
		this.sbj = sbj;
	}
	public String getSah() {
		return sah;
	}
	public void setSah(String sah) {
		this.sah = sah;
	}
	
	public student() {}
	public student(int sid, String sname, String sjy, String sbj, String sah) {
		this.sid = sid;
		this.sname = sname;
		this.sjy = sjy;
		this.sbj = sbj;
		this.sah = sah;
	}
	
	public student(String sname, String sjy, String sbj, String sah) {
		this.sname = sname;
		this.sjy = sjy;
		this.sbj = sbj;
		this.sah = sah;
	}
	

}

后面还需要教师实体类,和班级实体类


public class sbj {
	private int bid;
	private String bname;
	public int getBid() {
		return bid;
	}
	public void setBid(int bid) {
		this.bid = bid;
	}
	public String getBname() {
		return bname;
	}
	public void setBname(String bname) {
		this.bname = bname;
	}
	public sbj(int bid, String bname) {
		super();
		this.bid = bid;
		this.bname = bname;
	}
	public sbj() {}
	

}


public class sjy {
	private int jid;
	private String jname;
	private String jsex;
	public int getJid() {
		return jid;
	}
	public void setJid(int jid) {
		this.jid = jid;
	}
	public String getJname() {
		return jname;
	}
	public void setJname(String jname) {
		this.jname = jname;
	}
	public String getJsex() {
		return jsex;
	}
	public void setJsex(String jsex) {
		this.jsex = jsex;
	}
	
	public sjy() {}
	public sjy(int jid, String jname, String jsex) {
		super();
		this.jid = jid;
		this.jname = jname;
		this.jsex = jsex;
	}
	
	public sjy(String jname, String jsex) {
		this.jname = jname;
		this.jsex = jsex;
	}
	

}

第三步写dao包

先写一个student接口

public interface IbjDao {
	public List<sbj> getAll();
	public List<sbj> getAll(String colName,String str);
	public sbj getAllByBid(String bid);
	public int addbj(sbj s);
	public int deletebj(String bid);
	public int updatebj(sbj s,String bid);

}

在写一个student的dao方法


public class StuDao implements IstuDao {
	Connection con = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	

	@Override
	public Li
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MVC模式的实现对数据库的增删改查 部分代码: package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import common.DBConnection; import bean.Contact; public class ContactDAO { public List getAllContact() throws Exception{ Connection conn=DBConnection.getConntion(); PreparedStatement ps=conn.prepareStatement("select * from Contact"); ResultSet rs=ps.executeQuery(); List list = new ArrayList(); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String phone = rs.getString("phone"); String address = rs.getString("address"); Contact c = new Contact(); c.setId(id); c.setName(name); c.setPhone(phone); c.setAddress(address); list.add(c); } rs.close(); ps.close(); conn.close(); return list; } public void addContact(String name,String phone,String address) throws Exception{ String sql = "insert into contact(id,name,phone,address) values(seq_contact.nextval,?,?,?)"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, phone); pstmt.setString(3, address); pstmt.executeUpdate(); } public void delContact(int id) throws Exception{ String sql = "delete from contact where id=?"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1, id); pstmt.executeUpdate(); } public Contact getContactById(int id) throws Exception{ String sql = "select * from Contact where id=?"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1, id); ResultSet rs = pstmt.executeQuery(); Contact c = null; while(rs.next()){ // int id = rs.getInt("id"); String name=rs.getString("name"); String p
好的,我可以为您讲解一下关于MVC架构和EF操作的增删查改MVC是一种软件架构模式,它将应用程序分为三个主要部分:模型(Model)、视图(View)和控制器(Controller)。模型表示应用程序的数据和业务逻辑,视图展示数据给用户,控制器接收用户输入并调用模型和视图进行处理。 EF(Entity Framework)是一个基于.NET Framework的ORM(对象关系映射)框架,它可以使开发人员使用.NET语言编写与数据库交互的代码,而不需要直接与数据库打交道。 下面是关于如何使用MVC架构和EF进行增删查改的示例代码: 1. 增加操作: ```c# [HttpPost] public ActionResult Create(Student student) { using (SchoolContext db = new SchoolContext()) { db.Students.Add(student); db.SaveChanges(); } return RedirectToAction("Index"); } ``` 2. 删除操作: ```c# public ActionResult Delete(int id) { using (SchoolContext db = new SchoolContext()) { Student student = db.Students.Find(id); db.Students.Remove(student); db.SaveChanges(); } return RedirectToAction("Index"); } ``` 3. 修改操作: ```c# [HttpPost] public ActionResult Edit(Student student) { using (SchoolContext db = new SchoolContext()) { db.Entry(student).State = EntityState.Modified; db.SaveChanges(); } return RedirectToAction("Index"); } ``` 4. 查询操作: ```c# public ActionResult Details(int id) { using (SchoolContext db = new SchoolContext()) { Student student = db.Students.Find(id); return View(student); } } ``` 以上是关于MVC架构和EF操作的增删查改的示例代码,希望能对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值