www.pudn.com > Java_web_shipin_gongsi-.rar > SpDao.java, change:2011-05-15,size:2908b


package dao; 
 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
 
import bean.Lss; 
import bean.Sp; 
 
import common.DBConnection; 
 
public class SpDao { 
 
	public int addSp(Sp u){ 
		Connection con = DBConnection.getDBConnection(); 
		try { 
			PreparedStatement ps = con 
					.prepareStatement("insert into sp(spm,jg,dw,lssid) values(?,?,?,?)"); 
			ps.setString(1, u.getSpm()); 
			ps.setString(2, u.getJg()); 
			ps.setString(3, u.getDw()); 
			ps.setString(4, u.getLssid()); 
			int i= ps.executeUpdate(); 
			con.commit(); 
			DBConnection.close(con); 
			return i; 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		}		 
		return 0; 
	} 
	public int updateSp(Sp u){ 
		Connection con = DBConnection.getDBConnection(); 
		try { 
			PreparedStatement ps = con 
					.prepareStatement("update sp set spm=?,jg=?,dw=?,lssid=? where id=?"); 
			ps.setString(1, u.getSpm()); 
			ps.setString(2, u.getJg()); 
			ps.setString(3, u.getDw()); 
			ps.setString(4, u.getLssid()); 
			int i= ps.executeUpdate(); 
			con.commit(); 
			DBConnection.close(con); 
			return i; 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		}		 
		return 0;				 
	} 
	public int deleteSp(String id){ 
		Connection con = DBConnection.getDBConnection(); 
		try { 
			PreparedStatement ps = con 
					.prepareStatement("delete from  sp where id=?"); 
			ps.setString(1, id); 
			int i= ps.executeUpdate(); 
			con.commit(); 
			DBConnection.close(con); 
			return i; 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		}		 
		return 0;				 
	} 
	public ArrayList<Sp> getAllSp(){ 
		ArrayList<Sp> sList = new ArrayList<Sp>(); 
		Connection con = DBConnection.getDBConnection(); 
		try { 
			PreparedStatement ps = con.prepareStatement("select id,dw,jg,lssid,spm from sp"); 
			ResultSet rs = ps.executeQuery(); 
			while (rs.next()) { 
				Sp i=new Sp(); 
				i.setId(rs.getString("id")); 
				i.setDw(rs.getString("dw")); 
				i.setJg(rs.getString("jg")); 
				i.setLssid(rs.getString("lssid")); 
				i.setSpm(rs.getString("spm")); 
 
				sList.add(i); 
			} 
			DBConnection.close(con); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
			return null; 
		} 
		return sList;					 
	} 
	public Sp getSpInfoById(String id){ 
		Connection con = DBConnection.getDBConnection(); 
		try { 
			PreparedStatement ps = con.prepareStatement("select id,dw,jg,lssid,spm from sp where id=?"); 
			ps.setString(1, id); 
			ResultSet rs = ps.executeQuery(); 
			Sp i=new Sp(); 
			if(rs.next()) { 
				i.setId(rs.getString("id")); 
				i.setDw(rs.getString("dw")); 
				i.setJg(rs.getString("jg")); 
				i.setLssid(rs.getString("lssid")); 
				i.setSpm(rs.getString("spm")); 
			} 
			DBConnection.close(con); 
			return i; 
		} catch (SQLException e) { 
			e.printStackTrace(); 
			return null; 
		}				 
	}		 
}