java-数据库连接,分层实现增删改查测试

时间:2022-09-12 15:20:52

成员属性类:

public class Dog {
  private int number;
  private String name;
  private String strain;
  private String sex;
  private int age;
  private String matters;

  public int getNumber() {
    return number;
  }
  public void setNumber(int number) {
    this.number = number;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getStrain() {
    return strain;
  }
  public void setStrain(String strain) {
    this.strain = strain;
  }
  public String getSex() {
   return sex;
  }
  public void setSex(String sex) {
    this.sex = sex;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getMatters() {
    return matters;
  }
  public void setMatters(String matters) {
    this.matters = matters;
  }
}

 

 

逻辑方法类:

package com.zhidisoft.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.zhidisoft.entity.Dog;

//用来实现数据库的操作 连接、释放数据库 通用的增删改查
public class DatabaseAction {
  Connection conn = null;
  //连接数据库 
  public Connection getConnection(){
    String url = "jdbc:mysql://127.0.0.1:3306/test?" + "user=root&password=root&useUnicode=true&characterEncoding=UTF8";
    try {
      Class.forName("com.mysql.jdbc.Driver");//声明驱动的jar包
      conn = DriverManager.getConnection(url);//按照url提供的路径连接服务器
    } catch (ClassNotFoundException | SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }// 动态加载mysql驱动
    return conn;
  }
  //向数据库插入信息
  public int zhuce(Dog dog){
    conn=this.getConnection();//调用连接数据库方法,连接服务器
    String sql="insert into Dog()

       values("+dog.getNumber()+",'"+dog.getName()+"','"+dog.getStrain()+"','"+dog.getSex()+"',"+dog.getAge()+",'"+dog.getMatters()+"')";
    Statement st;
    int result=0;
    try {
      st = conn.createStatement();
      result=st.executeUpdate(sql);//执行sql语句,返回结果为int类型,受影响的行数
      conn.close();//关闭数据库连接
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return result;
  }

  public int shanchu(int number){
    conn=this.getConnection();
    String sql = "delete from Dog where number = "+number;
    Statement st;
    int result = 0;
    try {
      st = conn.createStatement();
      result = st.executeUpdate(sql);
      conn.close();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return result;
  }

  public int genggai(String name,String matters){
    conn=this.getConnection();
    String sql = "update Dog set matters ='"+matters+"' where name = '"+name+"'";
    Statement st;
    int result = 0;
    try {
      st = conn.createStatement();
      result = st.executeUpdate(sql);
      conn.close();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return result;
  }

  public void chazhao(Dog dog){
     conn=this.getConnection();
     String sql = "select * from Dog";
     Statement st;
     try {
      st = conn.createStatement();
      ResultSet rs = st.executeQuery(sql);
      while (rs.next()) {//遍历输出查询记录
        System.out.println("编号:"+rs.getInt("number")+"\t姓名 "+rs.getString("name")+"\t品种:"+rs.getString("strain")+"\t性

          别:"+rs.getString("sex")+"\t年龄:"+rs.getInt("age")+"\t病情:"+rs.getString("matters"));
      }
        conn.close();
     } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
  }

}

 

测试类:控制器:根据不同的功能调用不同的方法

 

import java.sql.SQLException;
import java.util.Scanner;import com.zhidisoft.dao.DatabaseAction;
import com.zhidisoft.entity.Dog;

public class Test {
  public static void main(String[] args) throws SQLException {
    DatabaseAction dz=new DatabaseAction();//实例化方法类
    //专注于业务逻辑
    System.out.println("欢迎来到宠物医院,请选择操作:1.注册 2.删除 3.更改 4.查询");
    @SuppressWarnings("resource")
    Scanner input=new Scanner(System.in);
    Dog dog = new Dog();
    String choice=input.next();
    if(choice.equals("1")){
      //进行注册操作 
      //收集数据 调用数据库的插入方法实现注册
      System.out.println("输入宠物的编号:");
      dog.setNumber(Integer.valueOf(input.next()));
      System.out.println("输入宠物的名字:");
      dog.setName(input.next());
      System.out.println("输入宠物的品种:");
      dog.setStrain(input.next());
      System.out.println("输入宠物的性别:");
      dog.setSex(input.next());
      System.out.println("输入宠物的年龄:");
      dog.setAge(Integer.valueOf(input.next()));
      System.out.println("输入宠物的病情:");
      dog.setMatters(input.next());

      //调用dao层的代码实现数据的插入
      int result=dz.zhuce(dog);
      if (result == 1) {//受影响行数为1,插入成功
        System.out.println("信息注册成功");
      }else{
        System.out.println("注册不成功,请检查输入信息是否完善");
      }
    }

    if (choice.equals("2")) {
      System.out.println("请输入要删除的编号:");
      dog.setNumber(Integer.valueOf(input.next()));
      int result = dz.shanchu(dog.getNumber());
      if (result != 0) {//有删除的记录,受影响行数不为零
        System.out.println("编号相对应狗信息删除成功");
      }else{
        System.out.println("未找到此编号的狗信息");
      }
    }

    if (choice.equals("3")) {
      System.out.println("请输入要更病情的狗的名字");
      dog.setName(input.next());
      System.out.println("请输入要更改的病情");
      dog.setMatters(input.next());
      int result = dz.genggai(dog.getName(), dog.getMatters());
      if (result == 0) {//修改成功,受影响条目不为零
        System.out.println("没有找到名字叫做"+dog.getName()+"的狗");
      }else{
        System.out.println("更改成功");
      }
    }

    if (choice.equals("4")) {//调用查询方法,遍历输出所有结果
      dz.chazhao(dog);    
    }
  }
}