首先导包,然后链接数据库

在lib里面放你导入的包

在创建一个DBHerlper里面写入这些:

public class DBHelper {
    //定义三个常量,1.数据库的连接字符串  2.数据库账号  3.数据库密码
    public static final String url="jdbc:mysql://localhost:3306/";
    public static final String name="root";
    public static final String pwd="root";
//定义一个数据库链接对象。
    private static Connection conn;
    public static  Connection getConn(){
        //1.加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2.通过驱动管理器里面的getConnection的方法,得到一个数据库链接对象
            try {
               conn= DriverManager.getConnection(url,name,pwd);//返回的是一个数据库链接对象Connection,用上面定义的数据库链接对象conn来接受这个返回值对象
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //判断conn是否为空,如果有值则链接成功,如果没有值则链接失败
        return  conn;
    }
    //关闭数据库
    public void realease(Connection conn){
        //如果conn等于空,那么就关闭数据库。
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
//测试是否链接到了数据库
    public static void main(String[] args) {
    System.out.println(DBHelper.getConn()
    );
    }

}

 在定义一个构造方法:

public class Userinfo {
    private int id;
    private String name;
    private String pwd;
    private String nyr;
    private String lestnyr;
    private String cadress;
    private String email;
    private  String phone;
public Userinfo(){
    super();
}
//做添加的时候因为id自增所以调用这个构造方法
    public Userinfo(String name, String pwd, String nyr, String lestnyr, String cadress, String email, String phone) {
        this.name = name;
        this.pwd = pwd;
        this.nyr = nyr;
        this.lestnyr = lestnyr;
        this.cadress = cadress;
        this.email = email;
        this.phone = phone;
    }

    public Userinfo(int id, String name, String pwd, String nyr, String lestnyr, String cadress, String email, String phone) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
        this.nyr = nyr;
        this.lestnyr = lestnyr;
        this.cadress = cadress;
        this.email = email;
        this.phone = phone;
    }

    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 getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getNyr() {
        return nyr;
    }

    public void setNyr(String nyr) {
        this.nyr = nyr;
    }

    public String getLestnyr() {
        return lestnyr;
    }

    public void setLestnyr(String lestnyr) {
        this.lestnyr = lestnyr;
    }

    public String getCadress() {
        return cadress;
    }

    public void setCadress(String cadress) {
        this.cadress = cadress;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

然后定义一个抽象类:

public interface UserinfoDao {
      //查询所有用户,把查询的用户存储在list集合里面
public List<Userinfo> findAll();
      //根据用户的id查找某个用户
public Userinfo findUserById(int id);
     //删除
    public int deleteUserById(int id);
    //添加,添加是一个用户对象所以是一个Userinfo
    public int addUser(Userinfo ui);
    //修改,
    public int updateUser(Userinfo ui);

}
然后写详细的增删改查的代码:
public class Daoimpl implements UserinfoDao {
    //实现接口里面的方法
    public List<Userinfo> findAll() {
        //1,获取数据库连接对象
        Connection conn = DBHelper.getConn();
        //定义一个集合
        List<Userinfo> lists = new ArrayList<Userinfo>();
        //2.书写sql语句
        String sql = "select*from customer";
        //3.预编译sql语句得到perpstatement对象
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            //4.执行命令,查询之后的到结果集
            //查询的结果都在这个结果集里面。
            ResultSet rs = ps.executeQuery();
            //5,从结果集中读取数据
            while (rs.next()) {
                //通过有参的构造方法来完成对对象属性的赋值,值从结果集中读取出来
                Userinfo ui = new Userinfo(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8));
                //把读取出来的用户添加到集合中
                lists.add(ui);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return lists;
    }

    @Override
    //查找某个用户
    public Userinfo findUserById(int id) {

        //1,获取数据库连接对象
        Connection conn = DBHelper.getConn();
        //声明一个UserInfo为空。
        Userinfo ui = null;
        //2.书写sql语句
        String sql = "select*from customer where cid=?";
        //3.预编译sql语句得到perpstatement对象
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            //4.给占位符赋值
            ps.setInt(1, id);
            //5.执行命令,查询之后的到结果集
            //查询的结果都在这个结果集里面。
            ResultSet rs = ps.executeQuery();
            //6,从结果集中读取数据
            if (rs.next()) {//当next为true,意味着从结果集中有数据。找到了
                //通过有参的构造方法来完成对对象属性的赋值,值从结果集中读取出来
                ui = new Userinfo(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8));
                //把读取出来的用户添加到集合中
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return ui;
    }

    @Override
    //删除用户
    public int deleteUserById(int id) {
        int result = 0;
        //获取数据库链接对象
        Connection conn = DBHelper.getConn();
        String sql = "delet*from userinfo where cid=?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            result = ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }

    //添加对象
    public int addUser(Userinfo ui) {
        int result = 0;
        //获取数据库链接对象
        Connection conn = DBHelper.getConn();
        //如果id里面有自增则给id赋值,也就是多打一个问号
        String sql = "insert into customer values(?,?,?,?,?,?,?,?)";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, ui.getId());
            ps.setString(1, ui.getName());
            ps.setString(2, ui.getPwd());
            ps.setString(3, ui.getNyr());
            ps.setString(4, ui.getNyr());
            ps.setString(5, ui.getCadress());
            ps.setString(6, ui.getEmail());
            ps.setString(7, ui.getPhone());
            result = ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }

    //修改
    public int updateUser(Userinfo ui) {
        int result = 0;
        //获取数据库链接对象
        Connection conn = DBHelper.getConn();
        //如果id里面有自增则给id赋值,也就是多打一个问号
        String sql = "update customer set name=?,pwd=?,nyr=?,listnyr=?,cadress=?,email=?,phone=?where id=?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);

            ps.setString(1, ui.getName());
            ps.setString(2, ui.getPwd());
            ps.setString(3, ui.getNyr());
            ps.setString(4, ui.getLestnyr());
            ps.setString(5, ui.getCadress());
            ps.setString(6, ui.getEmail());
            ps.setString(7, ui.getPhone());
            ps.setInt(8, ui.getId());
            result = ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }
}

最后定义一个测试类来实现:

public class tee {
  static   UserinfoDao ui= new Daoimpl();
    public static void main(String[] args) {
       // test1();
        //test2();
       // test3();
        // test4();
        test5();
    }

    private static void test5() {
        Userinfo uid=ui.findUserById(3);
        if(uid!=null){
            uid.setName("张志伟");
            ui.updateUser(uid);
        }
    }

    private static void test4() {
     int result= ui.addUser(new Userinfo("张建超","123456","2021-11-20","2021-12-10","长垣县","115432447@qq.com","1254154"));
        if(result>0){
            System.out.println("添加成功");
        }
    }

    //删除的方法
    private static void test3() {
      int result= ui.deleteUserById(2);
      if(result>0){
          System.out.println("删除成功");
      }
    }

    //查找某个用户的方法
    private static void test2() {
        //用一个User info接受返回值
      Userinfo uid= ui.findUserById(2);
      System.out.println(uid.getName());
    }

    private static void test1() {

List<Userinfo> ulist=ui.findAll();
for(Userinfo u:ulist){
    System.out.println("真实姓名"+u.getName());
}
    }
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐