JDBC实现的增删改查

package com.michael.dao.impl;    

import java.sql.Connection;    
import java.sql.PreparedStatement;    
import java.sql.ResultSet;    
import java.sql.SQLException;    
import java.sql.Statement;    
import java.util.ArrayList;    
import java.util.List;    

import com.michael.dao.LinkDao;    
import com.michael.util.ConnectionUtil;    
import com.michael.util.SQLConstants;    
import com.michael.vo.Link;    

public class LinkDaoImpl implements LinkDao,SQLConstants{    

        public void add(Link l) {    
                Connection conn = new ConnectionUtil().openConnection();    
                try {    
                        PreparedStatement pstmt = conn.prepareStatement(ADD_LINK_SQL);    
                        pstmt.setString(1, l.getName());    
                        pstmt.setString(2, l.getUrl());    
                        pstmt.executeUpdate();    
                } catch (SQLException e) {    
                        e.printStackTrace();    
                }finally{    
                        try {    
                                conn.close();    
                        } catch (SQLException e) {    
                                e.printStackTrace();    
                        }    
                }    
        }    

        public void delete(String[] ids) {    
                Connection conn = new ConnectionUtil().openConnection();    
                try {    
                        PreparedStatement pstmt = conn.prepareStatement(DELETE_LINK_SQL); 

   
                        if(ids!=null&&ids.length>0)    
                        for(int i=0;i<ids.length;i++){    
                                pstmt.setInt(1, Integer.parseInt(ids[i]));    
                                pstmt.executeUpdate();                                 
                        }    

                } catch (SQLException e) {    
                        e.printStackTrace();    
                }finally{    
                        try {    
                                conn.close();    
                        } catch (SQLException e) {    
                                e.printStackTrace();    
                        }    
                }    
        }    

        public Link get(int id) {    
                Connection conn = new ConnectionUtil().openConnection();    
                try {    
                        PreparedStatement pstmt = conn.prepareStatement(GET_LINK_SQL);    
                        pstmt.setInt(1, id);    
                        ResultSet rs = pstmt.executeQuery();    
                        if(rs.next()){    
                                //int id = rs.getInt(1);    
                                String name = rs.getString(2);    
                                String url = rs.getString(3);    
                                Link l = new Link();    
                                l.setId(id);    
                                l.setName(name);    
                                l.setUrl(url);    
                                return l;    
                        }    
                } catch (SQLException e) {    
                        e.printStackTrace();    
                }finally{    
                        try {    
                                conn.close();    
                        } catch (SQLException e) {    
                                e.printStackTrace();    
                        }    
                }    
                return null;    
        }    

        public List list() {    
                Connection conn = new ConnectionUtil().openConnection();    
                try {    
                        Statement stmt = conn.createStatement();    
                        ResultSet rs = stmt.executeQuery(QUERY_LINK_SQL);    
                        List list = new ArrayList();    
                        while(rs.next()){    
                                int id = rs.getInt(1);    
                                String name = rs.getString(2);    
                                String url = rs.getString(3);    
                                Link l = new Link();    
                                l.setId(id);    
                                l.setName(name);    
                                l.setUrl(url);    
                                list.add(l);    
                        }    
                        return list;    
                } catch (SQLException e) {    
                        e.printStackTrace();    
                }finally{    
                        try {    
                                conn.close();    
                        } catch (SQLException e) {    
                                e.printStackTrace();    
                        }    
                }    
                return null;    
        }    

        public void update(Link l) {    
                Connection conn = new ConnectionUtil().openConnection();    
                try {    
                        PreparedStatement pstmt = conn.prepareStatement(UPDATE_LINK_SQL); 

   
                        pstmt.setString(1, l.getName());    
                        pstmt.setString(2, l.getUrl());    
                        pstmt.setInt(3, l.getId());    
                        pstmt.executeUpdate();    
                } catch (SQLException e) {    
                        e.printStackTrace();    
                }finally{    
                        try {    
                                conn.close();    
                        } catch (SQLException e) {    
                                e.printStackTrace();    
                        }    
                }    
        }    

} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值