这个包的只负责实现数据库的操作
不采取其他操作
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 {
Dog dog = new Dog();
Connection conn = null;
// 连接数据库 张三
public Connection getConnection() {
String url = "jdbc:mysql://localhost:3306/test?"
+ "user=root&password=root&useUnicode=true&characterEncoding=UTF8";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} // 动态加载mysql驱动
return conn;
}
// 向数据库插入信息
public int zhuce(Dog dog) {
conn = this.getConnection();
String sql = "insert into dogs () values(" + dog.getNumber() + ",'" + dog.getName() + "','" + dog.getSex()
+ "'," + dog.getAge() + ",'" + dog.getStrain() + "','" + dog.getBingqingzhuangtai() + "')";
Statement st;
int result = 0;
try {
st = conn.createStatement();
result = st.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public int delete(int number) {
conn = this.getConnection();
String sql = "delete from dogs where number=" + number + "";
Statement st;
int result = 0;
try {
st = conn.createStatement();
result = st.executeUpdate(sql);
System.out.println(result);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public int genggai(String name, String bingqingzhuangtai) {
conn = this.getConnection();
String sql = "update dogs set bingqingzhuangtai='" + bingqingzhuangtai + "' where name='" + name + "'";
Statement st;
int result = 0;
try {
st = conn.createStatement();
result = st.executeUpdate(sql);
System.out.println(result);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public void chaxun(String bingqingzhuangtai) throws SQLException {
conn = this.getConnection();
String sql = "select * from dogs where bingqingzhuangtai='" + bingqingzhuangtai + "'";
Statement st;
ResultSet result;
st = conn.createStatement();
result = st.executeQuery(sql);
// System.out.println(result);
while (result.next()) {
System.out.println(result.getInt("number") + "\t" + result.getString("name") + "\t"
+ result.getString("sex") + "\t" + result.getInt("age") + "\t" + result.getString("strain") + "\t"
+ result.getString("bingqingzhuangtai"));
}
}
public void chaxun2(String name) throws SQLException {
conn = this.getConnection();
String sql = "select * from dogs where bingqingzhuangtai='" + name + "'";
Statement st;
ResultSet result;
st = conn.createStatement();
result = st.executeQuery(sql);
while (result.next()) {
System.out.println(result.getInt("number") + "\t" + result.getString("name") + "\t"
+ result.getString("sex") + "\t" + result.getInt("age") + "\t" + result.getString("strain") + "\t"
+ result.getString("bingqingzhuangtai"));
}
}
}
这个包则是用来存放数据的属性的包
例如这个动物Dogs类的属性
package com.zhidisoft.entity;
public class Dog {
private int number;
private String name;
private String sex;
private int age;
private String strain;
private String bingqingzhuangtai;
public String getBingqingzhuangtai() {
return bingqingzhuangtai;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public void setBingqingzhuangtai(String bingqingzhuangtai) {
this.bingqingzhuangtai = bingqingzhuangtai;
}
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;
}
}
还有一个包则是为了处理业务逻辑的包,此包不参与其他的事项
只是处理业务逻辑
package com.zhidisoft.UI;
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();
Dog dog=new Dog();
//专注于业务逻辑
System.out.println("欢迎来到宠物医院,请选择操作:1.注册 2.删除 3.更改信息 4.根据病例信息查询 5.根据宠物名查询信息");
Scanner input=new Scanner(System.in);
String choice=input.next();
if(choice.equals("1")){
//进行注册操作
//收集数据 调用数据库的插入方法实现注册
System.out.println("输入宠物编号:");
dog.setNumber(Integer.parseInt(input.next()));
System.out.println("输入宠物的名字:");
dog.setName(input.next());
System.out.println("输入宠物的性别:");
dog.setSex(input.next());
System.out.println("输入宠物的年龄:");
dog.setAge(Integer.parseInt(input.next()));
System.out.println("输入宠物品种:");
dog.setStrain(input.next());
System.out.println("宠物的症状:");
dog.setBingqingzhuangtai(input.next());
//调用dao层的代码实现数据的插入
dz.zhuce(dog);
}
if(choice.equals("2")){
System.out.println("输入将要删除的宠物编号:");
dog.setNumber(Integer.parseInt(input.next()));
dz.delete(dog.getNumber());
}
if (choice.equals("3")) {
System.out.println("输入将要更改的宠物姓名:");
dog.setName(input.next());
System.out.println("请输入要更改的病理状态");
dog.setBingqingzhuangtai((input.next()));
dz.genggai(dog.getName(), dog.getBingqingzhuangtai());
}
if (choice.equals("4")) {
System.out.println("请输入要查询的病理状态");
dog.setBingqingzhuangtai((input.next()));
dz.chaxun(dog.getBingqingzhuangtai());
}
if (choice.equals("5")) {
System.out.println("请输入要查询的宠物名称");
dog.setName((input.next()));
dz.chaxun2(dog.getName());
}
李四调用张三的代码
//Connection conn=dz.getConnection();
}
}
这只是一个初步的分层作业的雏形 ,这样做有利于高效率的完成项目