樱花视频在线观看-西西人体大胆4444ww张筱雨-久久网免费视频-国产99页-91高清视频在线-日日干夜夜干-91社区视频-中文高清av-久久成人国产-亚洲日韩欧洲乱码av夜夜摸-97人人射-亚洲视频观看-理论片亚洲-亚洲精品99999-免费能看的黄色片-精人妻无码一区二区三区-奇米影视播放器

游戲產(chǎn)業(yè)研究網(wǎng)

怎么用java操作數(shù)據(jù)庫實現(xiàn)(帶參數(shù))的增刪改查?求具體實例!一定要帶參數(shù)的哦!

import java.sql.*; /**課題:封裝數(shù)據(jù)庫的增刪改查的工具類的實現(xiàn)。 * * 假設相關數(shù)據(jù)庫的表結構如下: * 表名:user * 列名及屬性:id(int 自增),name(varchar(20)),tele(char(12)),birthday(date) * @author shy2850 */ public class UserDAO { Connection conn; public UserDAO(Connection conn) { this.conn = conn; } public int save(User user) throws SQLException { String sql = insert into user values(0,?,?,?); PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, user.getName()); pstmt.setString(2, user.getTele()); pstmt.setDate(3, user.getBirthday()); int n = pstmt.executeUpdate(); pstmt.close(); return n; } public int delete(User user) throws SQLException{ String sql = delete from user where id = +user.getId(); Statement stmt = conn.createStatement(); int n = stmt.executeUpdate(sql); stmt.close(); return n; } public int update(User user) throws SQLException{ String sql = update user set name=?, tele=?, birthday=? where id = +user.getId(); PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(2, user.getName()); pstmt.setString(3, user.getTele()); pstmt.setDate(4, user.getBirthday()); int n = pstmt.executeUpdate(sql); pstmt.close(); return n; } public User getUser(Integer id) throws SQLException{ String sql = select * from user where id = + id; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); User user = getUserFromResultSet(rs); rs.close(); stmt.close(); return user; } static User getUserFromResultSet(ResultSet rs) throws SQLException{ Integer id = rs.getInt(id); String name= rs.getString(name); String tele= rs.getString(tele); Date birthday = rs.getDate(birthday); return new User(id, name, tele, birthday); } } /** * 構建數(shù)據(jù)庫表的java類映射 */ class User{ private Integer id; private String name; private String tele; private Date birthday; public User() { } public User(Integer id, String name, String tele, Date birthday) { super(); this.id = id; this.name = name; this.tele = tele; this.birthday = birthday; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTele() { return tele; } public void setTele(String tele) { this.tele = tele; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }

衡阳市| 武安市| 犍为县| 临湘市| 开化县| 阳西县| 磐安县| 巨鹿县| 富宁县| 汝阳县| 涞水县| 雅安市| 都昌县| 连山| 阿图什市| 宜宾县| 绥中县| 和田县| 南乐县| 乌拉特前旗| 平顶山市| 固阳县| 儋州市| 孝义市| 巴楚县| 富裕县| 高州市| 安康市| 海城市| 武宁县| 仁怀市| 旅游| 朝阳县| 商丘市| 诏安县| 武定县| 手游| 凉城县| 余干县| 新蔡县| 四川省|