資源描述:
《JDBC實現(xiàn)增刪改查》由會員上傳分享,免費在線閱讀,更多相關內容在行業(yè)資料-天天文庫。
1、1.public?class?NoteDAOImpl?implements?NoteDAO?{??2.????//?增加操作??3.????public?void?insert(Note?note)?throws?Exception?{??4.????????String?sql?=?"INSERT?INTO?note(id,title,author,content)?VALUES(note_sequ.nextVal,?,?,?)"?;??5.????????PreparedStatement?pstmt?=?null
2、?;??6.????????DataBaseConnection?dbc?=?null?;??7.????????dbc?=?new?DataBaseConnection()?;??8.????????try?{??9.????????????pstmt?=?dbc.getConnection().prepareStatement(sql)?;??10.????????????pstmt.setString(1,note.getTitle())?;??11.????????????pstmt.setString(2,n
3、ote.getAuthor())?;??12.????????????pstmt.setString(3,note.getContent())?;??13.????????????pstmt.executeUpdate()?;??14.????????????pstmt.close()?;??15.????????}?catch?(Exception?e)?{??16.????????????//?System.out.println(e)?;??17.????????????throw?new?Exception("
4、操作中出現(xiàn)錯誤?。?!")?;??18.????????}?finally?{??19.????????????dbc.close()?;??1.????????}??2.????}??3.????//?修改操作??4.????public?void?update(Note?note)?throws?Exception?{??5.????????String?sql?=?"UPDATE?note?SET?title=?,author=?,content=??WHERE?id=?"?;??6.????????Prepare
5、dStatement?pstmt?=?null?;??7.????????DataBaseConnection?dbc?=?null?;??8.????????dbc?=?new?DataBaseConnection()?;??9.????????try?{??10.????????????pstmt?=?dbc.getConnection().prepareStatement(sql)?;??11.????????????pstmt.setString(1,note.getTitle())?;??12.???????
6、?????pstmt.setString(2,note.getAuthor())?;??13.????????????pstmt.setString(3,note.getContent())?;??14.????????????pstmt.setInt(4,note.getId())?;??15.????????????pstmt.executeUpdate()?;??16.????????????pstmt.close()?;??17.????????}?catch?(Exception?e)?{??18.?????
7、???????throw?new?Exception("操作中出現(xiàn)錯誤?。?!")?;??19.????????}?finally?{??1.????????????dbc.close()?;??2.????????}??3.????}??4.????//?刪除操作??5.????public?void?delete(int?id)?throws?Exception?{??6.????????String?sql?=?"DELETE?FROM?note?WHERE?id=?"?;??7.????????PreparedS
8、tatement?pstmt?=?null?;??8.????????DataBaseConnection?dbc?=?null?;??9.????????dbc?=?new?DataBaseConnection()?;??10.????????try?{??11.????????????pstmt?=?dbc.getConnec