mvc的增删改查

本文档详细介绍了如何通过MVC模式进行数据库的增删改查操作,包括建立数据库连接、创建实体类、定义DAO方法、编写控制器、测试类以及配置mvc.xml和web.xml文件,最后展示了在JSP页面上实现的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.首先要建立连接

package com.zking.mvcplus.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 url;
 private static String driver;
 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("数据库连接(关闭)成功");
 }
}

2.建个连接到数据库实体类book


package com.zking.mvcplus.entity;
public class Book {
   
   
 private int bid;
 private String bname;
 private float price;
 @Override
 public String toString() {
   
   
  return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
 }
 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 float getPrice() {
   
   
  return price;
 }
 public void setPrice(float price) {
   
   
  this.price = price;
 }
 public Book(int bid, String bname, float price) 
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值