图书管理系统
1. mysql创建一个数据库,bookmanager,创建一个表book
2. 使用mvc模式
3. 创建视图层 View
4. 创建服务层 Service
5. 创建数据层 Dao
6. 创建模型层 Book
View类处理用户的输入调用service方法
package Book_Manager;
import java.util.Scanner;
public class View {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("======欢迎登录图书管理系统======");
while(true){
System.out.println("1.查看图书");
System.out.println("2.录入图书");
System.out.println("3.删除图书");
System.out.println("4.退出");
System.out.println("输入(1-4)");
int chioce=sc.nextInt();
Service service=new Service();
switch(chioce)
{
case 1:
service.display();
break;
case 2:
service.insert();
break;
case 3:
service.delete();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("请输入正确的操作数字");
}
System.out.println("======================================");
}
}
}
Service创建相对应的方法
package Book_Manager;
import java.util.Scanner;
public class Service {
Scanner sc=new Scanner(System.in);
Dao dao=new Dao();
public Service() {
}
public void display(){ //查看所有图书
dao.display();
}
public void insert()//插入图书
{
System.out.println("请输入书籍的编号和名称和价格");
int id = 0;
String name=null;
double price=0.0;
try{
id=sc.nextInt();
name=sc.next();
price=sc.nextDouble();
}
catch (Exception e)
{
System.out.println("请输入正确格式的图书");
}
Book book=new Book(id,name,price);
dao.insert(book);
}
public void delete()//删除图书
{
System.out.println("请输入删除的编号");
int id=sc.nextInt();
dao.delete(id);
}
}
```java
package Book_Manager;
import java.sql.*;
public class Dao {
Connection connection=null;
public Dao() {
try{
Class.forName("com.mysql.jdbc.Driver");
connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/bookmanager","root","zhyazj811303749");
}
catch (Exception e){
System.out.println("数据库连接失败!");
}
}
public void display(){ //查看所有图书
try{
String uri="select * from book";
Statement statement=connection.createStatement();
ResultSet resultSet=null;
resultSet=statement.executeQuery(uri);
while (resultSet.next())
{
System.out.printf("id:%d\tname:《%s》\t\tprice:¥%.2f\n",resultSet.getInt("id"),resultSet.getString("name"),resultSet.getDouble("price"));
}
}
catch(Exception e)
{
System.out.println("数据库查看失败!");
}
}
public void insert(Book book)//插入图书
{
int id=book.getId();
String name=book.getName();
double price=book.getPrice();
try{
PreparedStatement preparedStatement=connection.prepareStatement("insert into book value(?,?,?)");
preparedStatement.setInt(1,id);
preparedStatement.setString(2,name);
preparedStatement.setDouble(3,price);
int num=preparedStatement.executeUpdate();
if(num!=0){
System.out.println("插入成功!");
}
else{
System.out.println("插入失败!");
}
}
catch (Exception e)
{
System.out.println("插入失败!");
}
}
public void delete(int id)//删除图书
{
try {
PreparedStatement preparedStatement=connection.prepareStatement("delete from book where id=?");
preparedStatement.setInt(1,id);
int num=preparedStatement.executeUpdate();
if(num!=0){
System.out.println("删除成功!");
}
else System.out.println("删除失败!");
} catch (SQLException throwables) {
System.out.println("删除失败!");
}
}
}
Book只是一个模型
package Book_Manager;
public class Book {
private int id;
private String name;
private double price;
public Book(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
数据库的设计表