package com.dao; import com.bean.electric_addBean; import com.db.DBHelper; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class electriDao { //获取列表 public List GetList(String strwhere,String strorder){ String sql="select * from electric"; if(!(isInvalid(strwhere))) { sql+=" where "+strwhere; }if(!(isInvalid(strorder))) { sql+=" order by "+strorder; } Statement stat = null; ResultSet rs = null; Connection conn = new DBHelper().getConn(); List list=new ArrayList(); try{ stat = conn.createStatement(); rs = stat.executeQuery(sql); while(rs.next()){ electric_addBean cnbean=new electric_addBean(); cnbean.setElectric_ID(rs.getInt("Electric_ID")); cnbean.setElectric_Dormitory_Number(rs.getString("Electric_Dormitory_Number")); cnbean.setElectric_Residual(rs.getString("Electric_Residual")); list.add(cnbean); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); if (stat != null) stat.close(); if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } } return list; } public electric_addBean GetBean(int id){ String sql="select * from electric where Electric_ID="+id; Statement stat = null; ResultSet rs = null; Connection conn = new DBHelper().getConn(); electric_addBean cnbean=new electric_addBean(); try{ stat = conn.createStatement(); rs = stat.executeQuery(sql); while(rs.next()){ // cnbean.setTeacher_ID(rs.getInt("Teacher_ID")); cnbean.setElectric_ID(rs.getInt("Electric_ID")); cnbean.setElectric_Dormitory_Number(rs.getString("Electric_Dormitory_Number")); cnbean.setElectric_Residual(rs.getString("Electric_Residual")); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); if (stat != null) stat.close(); if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } } return cnbean; } public void Add(electric_addBean cnbean){ String sql="insert into electric ("; sql+="Electric_ID,Electric_Dormitory_Number,Electric_Residual"; sql+=") values("; sql+="'"+cnbean.getElectric_ID()+"','"+cnbean.getElectric_Dormitory_Number()+"','"+cnbean.getElectric_Residual()+"'"; sql+=")"; Statement stat = null; ResultSet rs = null; Connection conn = new DBHelper().getConn(); try{ stat = conn.createStatement(); stat.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); if (stat != null) stat.close(); if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } //修改 public void Update(electric_addBean cnbean){ String sql="update electric set "; //sql+="Electric_Domitory_Number='"+cnbean.getElectric_Dormitory_Number()+"',"; sql+="Electric_Residual='"+cnbean.getElectric_Residual()+"'"; // sql+="Student_Password='"+cnbean.getStudent_Password()+"',"; sql+=" where Electric_ID='"+cnbean.getElectric_ID()+"'"; Statement stat = null; ResultSet rs = null; Connection conn = new DBHelper().getConn(); try{ stat = conn.createStatement(); stat.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); if (stat != null) stat.close(); if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } public electric_addBean GetAllBean(int id){ String sql="select * from electric where Electric_ID="+id; Statement stat = null; ResultSet rs = null; Connection conn = new DBHelper().getConn(); electric_addBean cnbean=new electric_addBean(); try{ stat = conn.createStatement(); rs = stat.executeQuery(sql); while(rs.next()){ cnbean.setElectric_ID(rs.getInt("Electric_ID")); cnbean.setElectric_Dormitory_Number(rs.getString("Electric_Dormitory_Number")); cnbean.setElectric_Residual(rs.getString("Electric_Residual")); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); if (stat != null) stat.close(); if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } } return cnbean; } private boolean isInvalid(String value) { return (value == null || value.length() == 0); } }