Java Web 增删查改操作

本文介绍了如何使用Java Web进行数据库操作,包括创建实体类封装属性,通过DAO连接SQL Server数据库,实现增删查改方法,利用service层处理异常,以及在web层使用servlet进行展示和交互。最后,应用了JSP页面进行前端展示。

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

 

最先创建一个实体包:entity,用于封装属性

我是在实体类包里面创建了一个houseInfo的java文件,代码如下:

package entity;

public class houseInfo {
	private String hname;
	private String htype;
	private String hcontent;
	private String htime;
	private double hprice;
	private String himage;
	public String getHname() {
		return hname;
	}
	public void setHname(String hname) {
		this.hname = hname;
	}
	public String getHcontent() {
		return hcontent;
	}
	public void setHcontent(String hcontent) {
		this.hcontent = hcontent;
	}
	public String getHtype() {
		return htype;
	}
	public void setHtype(String htype) {
		this.htype = htype;
	}
	public String getHtime() {
		return htime;
	}
	public void setHtime(String htime) {
		this.htime = htime;
	}
	public double getHprice() {
		return hprice;
	}
	public void setHprice(double hprice) {
		this.hprice = hprice;
	}
	public String getHimage() {
		return himage;
	}
	public void setHimage(String himage) {
		this.himage = himage;
	}
	@Override
	public String toString() {
		return "houseInfo [hname=" + hname + ", htype=" + htype + ", hcontent=" + hcontent + ", htime=" + htime
				+ ", hprice=" + hprice + ", himage=" + himage + "]";
	}
	
	
	
}

 

通过sqlserver连接数据库

然后,创建一个连接SQL server数据库 的dao包,实现对数据库增删改操作方法

 

public class houseDao {
	static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	static final String url = "jdbc:sqlserver://127.0.0.1:1433;database=houseDB;";
	static {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    

    public List<houseInfo> query1() throws SQLException{//对数据库进行查询
		List<houseInfo>list=new ArrayList<houseInfo>();
		String sql="select hname,htype,hcontent,htime,hprice,himage from houseInfo";
		try(Connection conn=DriverManager.getConnection(url,"sa","123456");
				PreparedStatement pstmt=conn.prepareStatement(sql)){
			try(ResultSet rs = pstmt.executeQuery()){
				while(rs.next()) {
					houseInfo ci = new houseInfo();
					houseInfo hi&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值