JDBC 实现增删改查
来源:互联网 发布:联通光纤网络测速软件 编辑:程序博客网 时间:2024/05/04 17:40
- public class NoteDAOImpl implements NoteDAO {
- // 增加操作
- public void insert(Note note) throws Exception {
- String sql = "INSERT INTO note(id,title,author,content) VALUES(note_sequ.nextVal,?,?,?)" ;
- PreparedStatement pstmt = null ;
- DataBaseConnection dbc = null ;
- dbc = new DataBaseConnection() ;
- try {
- pstmt = dbc.getConnection().prepareStatement(sql) ;
- pstmt.setString(1,note.getTitle()) ;
- pstmt.setString(2,note.getAuthor()) ;
- pstmt.setString(3,note.getContent()) ;
- pstmt.executeUpdate() ;
- pstmt.close() ;
- } catch (Exception e) {
- // System.out.println(e) ;
- throw new Exception("操作中出现错误!!!") ;
- } finally {
- dbc.close() ;
- }
- }
- // 修改操作
- public void update(Note note) throws Exception {
- String sql = "UPDATE note SET title=?,author=?,content=? WHERE id=?" ;
- PreparedStatement pstmt = null ;
- DataBaseConnection dbc = null ;
- dbc = new DataBaseConnection() ;
- try {
- pstmt = dbc.getConnection().prepareStatement(sql) ;
- pstmt.setString(1,note.getTitle()) ;
- pstmt.setString(2,note.getAuthor()) ;
- pstmt.setString(3,note.getContent()) ;
- pstmt.setInt(4,note.getId()) ;
- pstmt.executeUpdate() ;
- pstmt.close() ;
- } catch (Exception e) {
- throw new Exception("操作中出现错误!!!") ;
- } finally {
- dbc.close() ;
- }
- }
- // 删除操作
- public void delete(int id) throws Exception {
- String sql = "DELETE FROM note WHERE id=?" ;
- PreparedStatement pstmt = null ;
- DataBaseConnection dbc = null ;
- dbc = new DataBaseConnection() ;
- try {
- pstmt = dbc.getConnection().prepareStatement(sql) ;
- pstmt.setInt(1,id) ;
- pstmt.executeUpdate() ;
- pstmt.close() ;
- } catch (Exception e) {
- throw new Exception("操作中出现错误!!!") ;
- } finally {
- dbc.close() ;
- }
- }
- // 按ID查询,主要为更新使用
- public Note queryById(int id) throws Exception {
- Note note = null ;
- String sql = "SELECT id,title,author,content FROM note WHERE id=?" ;
- PreparedStatement pstmt = null ;
- DataBaseConnection dbc = null ;
- dbc = new DataBaseConnection() ;
- try {
- pstmt = dbc.getConnection().prepareStatement(sql) ;
- pstmt.setInt(1,id) ;
- ResultSet rs = pstmt.executeQuery() ;
- if(rs.next()) {
- note = new Note() ;
- note.setId(rs.getInt(1)) ;
- note.setTitle(rs.getString(2)) ;
- note.setAuthor(rs.getString(3)) ;
- note.setContent(rs.getString(4)) ;
- }
- rs.close() ;
- pstmt.close() ;
- } catch (Exception e) {
- throw new Exception("操作中出现错误!!!") ;
- } finally {
- dbc.close() ;
- }
- return note ;
- }
- // 查询全部
- public List queryAll() throws Exception {
- List all = new ArrayList() ;
- String sql = "SELECT id,title,author,content FROM note" ;
- PreparedStatement pstmt = null ;
- DataBaseConnection dbc = null ;
- dbc = new DataBaseConnection() ;
- try {
- pstmt = dbc.getConnection().prepareStatement(sql) ;
- ResultSet rs = pstmt.executeQuery() ;
- while(rs.next()) {
- Note note = new Note() ;
- note.setId(rs.getInt(1)) ;
- note.setTitle(rs.getString(2)) ;
- note.setAuthor(rs.getString(3)) ;
- note.setContent(rs.getString(4)) ;
- all.add(note) ;
- }
- rs.close() ;
- pstmt.close() ;
- } catch (Exception e) {
- System.out.println(e) ;
- throw new Exception("操作中出现错误!!!") ;
- } finally {
- dbc.close() ;
- }
- return all ;
- }
- // 模糊查询
- public List queryByLike(String cond) throws Exception {
- List all = new ArrayList() ;
- String sql = "SELECT id,title,author,content FROM note WHERE title LIKE ? or AUTHOR LIKE ? or CONTENT LIKE ?" ;
- PreparedStatement pstmt = null ;
- DataBaseConnection dbc = null ;
- dbc = new DataBaseConnection() ;
- try {
- pstmt = dbc.getConnection().prepareStatement(sql) ;
- pstmt.setString(1,"%"+cond+"%") ;
- pstmt.setString(2,"%"+cond+"%") ;
- pstmt.setString(3,"%"+cond+"%") ;
- ResultSet rs = pstmt.executeQuery() ;
- while(rs.next()) {
- Note note = new Note() ;
- note.setId(rs.getInt(1)) ;
- note.setTitle(rs.getString(2)) ;
- note.setAuthor(rs.getString(3)) ;
- note.setContent(rs.getString(4)) ;
- all.add(note) ;
- }
- rs.close() ;
- pstmt.close() ;
- } catch (Exception e) {
- System.out.println(e) ;
- throw new Exception("操作中出现错误!!!") ;
- } finally {
- dbc.close() ;
- }
- return all ;
- }
- };
http://talentb.blog.51cto.com/874996/187710
- JDBC 实现增删改查
- Swing+JDBC实现增删查改
- 使用jdbc实现增删改查示例
- jdbc 实现增删改查例子
- JDBC 实现 数据库的增删改查
- JDBC实现数据库增删改查
- JDBC连接mysql实现增删改查
- JDBC实现增删查改操作
- 【java基础】JDBC实现增删改查
- JDBC实现增删查改实例
- JDBC实现数据库的增删改查
- JDBC实现增删改查功能
- Jdbc 增删改查
- jdbc增删改查
- JDBC 增删改查
- jdbc增删改查
- jdbc增删查改
- JDBC 增删改查
- 如何抵御U盘病毒?
- JDBC连接
- 公司
- 我所知道的一点关于busybox配置选项注意
- MyEclipse8.x 配置 Hadoop 插件
- JDBC 实现增删改查
- 大型互联网站解决海量数据的常见策略
- Flash player 11新增的Drawing API
- 基于ArcGIS10.0和Oracle10g的空间数据管理平台十三(C#开发)-空间数据导出
- Android中的长度单位详解(dp、sp、px、in、pt、mm)
- IOCP中CompletionKey(完成键)与Overlapped(重叠结构体)的区别
- Android之Activity生命周期详细介绍,新手必必必看
- VTCP 10.X
- Python多线程Socket程序例子