手机系统增删改查(eclipse用Java语言调用数据库)

一、

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值