一、
package com.hm.bean;
import java.util.Objects;
public class Phone {
private int id;
private String name;
private String type;
private int runMemory;
private double size;
private double sal;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getRunMemory() {
return runMemory;
}
public void setRunMemory(int runMemory) {
this.runMemory = runMemory;
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
@Override
public String toString() {
return "Phone [id=" + id + ", name=" + name + ", type=" + type + ", runMemory=" + runMemory + ", size=" + size
+ ", sal=" + sal + "]";
}
//只要名字相同我们就认为是同一个对象
@Override
public int hashCode() {
return Objects.hash(name);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Phone other = (Phone) obj;
return Objects.equals(name, other.name);
}
}
二、
package com.hm.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.hm.bean.Phone;
import com.hm.util.DBUtil;
public class PhoneDao {
//针对于这个phone的增删改查
//添加操作
public int add(Phone p) {
//获取链接对象
Connection conn = DBUtil.getConn();
String sql="insert into phone values(null,?,?,?,?,?)";
int i=0;
try {
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, p.getName());
ps.setString(2, p.getType());
ps.setInt(3, p.getRunMemory());
ps.setDouble(4, p.getSize());
ps.setDouble(5, p.getSal());
i=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
//查询操作
//查询全部, 查按照名字模糊查询 按照类型查询 按照大小查询
public ArrayList<Phone> select(Phone p){
Connection conn=DBUtil.getConn();
String sql="select *from phone where 1=1";//条件为真查询所有
ArrayList<Object> params=new ArrayList<>();
if(p!=null) {
if(p.getName()!=null&&!p.getName().isEmpty()) {
sql+=" and name like ? ";
params.add("%"+p.getName()+"%");
}
if(p.getType()!=null&&!p.getType().isEmpty()) {
sql+=" and type=? ";
params.add(p.getType());
}
if(p.getSize()>0.1) {
sql+=" and size=? ";
params.add(p.getSize());
}
}
//处理SQL语句
ArrayList<Phone> li=new ArrayList<>();//用来存结果集的数据
try {
PreparedStatement ps=conn.prepareStatement(sql);
for(int i=0;i<params.size();i++) {
ps.setObject(i+1, params.get(i));
}
ResultSet rs=ps.executeQuery();
while(rs.next()) {//每遍历一次查到一次记录
Phone phone=new Phone();
phone.setId(rs.getInt("id"));//给手机ID赋值
phone.setName(rs.getString("name"));
phone.setType(rs.getString("type"));
phone.setRunMemory(rs.getInt("Running_memory"));
phone.setSize(rs.getDouble("size"));
phone.setSal(rs.getDouble("sal"));
li.add(phone);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return li;
}
//删除 根据ID删除数据
public int delete(int id) {
Connection conn=DBUtil.getConn();
String sql="delete from phone where id=?";
int i=0;
try {
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1, id);
i=ps.executeUpdate();//增加删除修改都是它
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
//修改
//修改前先查询
public Phone selectId(int id) {
Connection conn=DBUti