如何在B/S模式下,用java实现增删改查
答案:4 悬赏:10 手机版
解决时间 2021-11-17 11:41
- 提问者网友:听门外雪花风
- 2021-11-16 20:02
如何在B/S模式下,用java实现增删改查
最佳答案
- 五星知识达人网友:琴狂剑也妄
- 2021-11-16 21:28
随便搭个SSH框架什么的就可以轻松实现增删改查了。
全部回答
- 1楼网友:神的生死簿
- 2021-11-16 23:20
用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 phone=rs.getString("phone"); String address = rs.getString("address"); c = new Contact(); c.setId(id); c.setName(name); c.setPhone(phone); c.setAddress(address); } return c; } public void updateContact(int id,String name,String phone,String address) throws Exception{ String sql = "update contact set name=?,phone=?,address=? where id=?"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, phone); pstmt.setString(3,address); pstmt.setInt(4, id); pstmt.executeUpdate(); } }
- 2楼网友:独钓一江月
- 2021-11-16 23:08
这问题还用问,整个框架SSH或者SSM,连上数据库,你直接在底层自己写增删改的语句就行了
- 3楼网友:上分大魔王
- 2021-11-16 21:55
数据库 的干活
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯