import java.sql.*; /**課題:封裝數據庫的增刪改查的工具類的實現。 * * 假設相關數據庫的表結構如下: * 表名: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); } } /** * 構建數據庫表的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; } }
怎么用java操作數據庫實現(帶參數)的增刪改查?求具體實例!一定要帶參數的哦!
相關推薦
-
matlab中圖形釋放命令是什么
一、matlab中圖形釋放命令是什么 matlab中圖形關閉的命令是close,其用法有: close 關閉當前圖形 close(h) 關閉圖形h close name 關閉名為name的圖形 close all 關閉所有的、柄不隱藏的圖形 close
2025-06-18
admin
0 -
為什么我電腦上面有字看不見啊?
為什么我電腦上面有字看不見啊? 是不是任務欄也是黑色的?所有的東西只能看到圖標看不到字嗎? 如果是這個樣子的話,點擊桌名屬性,在外觀設置項里的色彩方案設置為正常就OK 了
2025-02-16
admin
0 -
css中,怎樣使一列的文字豎排顯示?高手指點,
一、css中,怎樣使一列的文字豎排顯示?高手指點, 一、使用writing-mode屬性 語法:writing-mode:lr-tb或writing-mode:tb-rl 參數: 1、lr-tb:從左向右,從上往下 2、tb-rl:從上往下,從右向左 運
2025-02-16
admin
0 -
delphi快捷鍵有哪些
一、delphi快捷鍵有哪些 1.SHIFT+鼠標左鍵 先選中任一控件,按鍵后可選中窗體(選中控件后按Esc效果一樣) 2.Shift+F8 調試時彈出CPU窗口。 3.Shift+F10 等于鼠標右鍵(Windows快捷鍵)。 4.Shitf+箭頭
2025-02-13
admin
0 -
怎么用HI隱藏文件
怎么用HI隱藏文件 attrib指令用于修改文件的屬性,文件的常見屬性有:只讀、存檔、隱藏和系統 只讀屬性是指文件只可以做讀的操作,不能對文件進行寫的操作,就是文件的寫保護。
2025-02-10
admin
0
