package com.micro.dao;
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.micro.util.DbManager;
import com.micro.bean.Sensor;
public class SensorDAO{
DbManager dbm = new DbManager();
public void insertSensor(Sensor sv) {
// TODO Auto-generated method stub
Connection conn = null;
Statement st = null;
conn = dbm.in();
try {
st = conn.createStatement();
String sql = "insert into sensor (sid,name,image,info) " +
"value ('"+sv.getSid()+"','"+sv.getName()+"','"+sv.getImage()+"','"+sv.getInfo()+"')";
st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
dbm.closeSt(st, conn);
}
}
public ArrayList<Sensor> selectSensor() {
// TODO Auto-generated method stub
ArrayList<Sensor> list = new ArrayList<Sensor>();
Connection conn= null;
Statement st = null;
ResultSet rs = null;
conn = dbm.in();
try {
st = conn.createStatement();
String sql = "select * from sensor";
rs = st.executeQuery(sql);
while (rs.next())
{
Sensor sv = new Sensor();
sv.setSid(rs.getString("sid"));
sv.setName(rs.getString("name"));
sv.setImage(rs.getString("image"));
sv.setInfo(rs.getString("info"));
sv.setId(rs.getInt("id"));
list.add(sv);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
dbm.closeAll(rs, st, conn);
}
return list;
}
public void delete(int id) {
// TODO Auto-generated method stub
Connection conn = null;
Statement st = null;
conn = dbm.in();
try {
st = conn.createStatement();
String sql = "delete from sensor where id = "+id;
st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
dbm.closeSt(st, conn);
}
}
public Sensor selectAll(int id) {
// TODO Auto-generated method stub
Sensor sv = new Sensor();
Connection conn = null;
Statement st = null;
ResultSet rs = null;
conn = dbm.in();
try {
st = conn.createStatement();
String sql = "select * from sensor where id = "+id;
rs = st.executeQuery(sql);
while (rs.next())
{
sv.setSid(rs.getString("sid"));
sv.setName(rs.getString("name"));
sv.setImage(rs.getString("image"));
sv.setInfo(rs.getString("info"));
sv.setId(id);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
dbm.closeAll(rs, st, conn);
}
return sv;
}
public void update(Sensor sv) {
// TODO Auto-generated method stub
Connection conn = null;
Statement st = null;
conn = dbm.in();
try {
st = conn.createStatement();
String sql = "update sensor set sid='"+sv.getSid()+"' , name='"+sv.getName()+"' ,image='"+sv.getImage()+"' , info='"+sv.getInfo()+"' where id="+sv.getId();
st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
dbm.closeSt(st, conn);
}
}
//public void save(Sensor sensor){
//
// Connection conn = DbConn.getConn();
//
// String sql = "insert into sensor values(?,?,?)";
//
// try {
//
// PreparedStatement ptst = conn.prepareStatement(sql);
//
//
// //ptst.setInt(1, sensor.getId());
// ptst.setString(1, sensor.getName());
// ptst.setString(2, sensor.getImage());
//
// ptst.setString(3, sensor.getInfo());
//
//
// ptst.executeUpdate();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//
//public void update1(Sensor sensor){
//
// Connection conn = DbConn.getConn();
//
// String sql = "update sensor set name = ?,image = ?,info = ? where id = ?";
//
// try {
//
// PreparedStatement ptst = conn.prepareStatement(sql);
//
//
// //ptst.setInt(1, sensor.getId());
// ptst.setString(1, sensor.getName());
// ptst.setString(2, sensor.getImage());
//
// ptst.setString(3, sensor.getInfo());
//
// ptst.executeUpdate();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// public List<Sensor> getList(){
// Connection conn = DbConn.getConn();
// String sql = "select id,name,image,info from sensor";
//
// List<Sensor> list = new ArrayList<Sensor>();
// try {
// PreparedStatement ptst = conn.prepareStatement(sql);
// System.out.println("得到的集合大小是:"+list.size());
// ResultSet rs = ptst.executeQuery();
// if(rs!=null){
// while(rs.next()){
// Sensor sensor = new Sensor();
// sensor.setId(rs.getInt("id"));
// sensor.setName(rs.getString("name"));
// sensor.setImage(rs.getString("image"));
// sensor.setInfo(rs.getString("info"));
// list.add(sensor);
// System.out.println();
// }
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// System.out.println("得到的集合大小是:"+list.size());
// return list;
// }
}