图书馆管理系统(对于数据库)

博客内容包含数据库连接配置文件 db.properties,DBUtils 用于数据库操作,Library 为实体类,LibrarySystem 是管理系统,整体围绕数据库连接和图书馆管理系统展开。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

db.properties:

url=jdbc:mysql://localhost:3306/soft?characterEncoding=UTF-8
user=root
password=
driver=com.mysql.jdbc.Driver

//数据库连接
DBUtils :

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

public class DBUtils {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;

   static{
       try {
           Properties properties = new Properties();
           ClassLoader classLoader = DBUtils.class.getClassLoader();
           URL res = classLoader.getResource("db.properties");
           String path = res.getPath();
           properties.load(new FileReader(path));
           //获取数据
           url = properties.getProperty("url");
           user = properties.getProperty("user");
           password = properties.getProperty("password");
           driver = properties.getProperty("driver");
           System.out.println(url + "  " + password + "  " + user);
           Class.forName("com.mysql.jdbc.Driver");
       } catch (IOException e) {
           e.printStackTrace();
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       }
   }

   public static Connection getConn() throws SQLException {
       return DriverManager.getConnection(url, user, password);
   }

    public static void close(PreparedStatement pst, Connection conn) {
        if( pst != null){
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if( conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            Connection conn = DBUtils.getConn();
            if (conn != null) {
                System.out.println("连接成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

//实体类
Library :

    public class Library {
    private int bId;
    private String bName;
    private String author;
    private String bStyle;
    private String publish;
    public Library() {
    }

    public Library(int bId, String bName, String author, String bStyle, String publish) {
        this.bId = bId;
        this.bName = bName;
        this.author = author;
        this.bStyle = bStyle;
        this.publish = publish;
    }

    public int getbId() {
        return bId;
    }

    public void setbId(int bId) {
        this.bId = bId;
    }

    public String getbName() {
        return bName;
    }

    public void setbName(String bName) {
        this.bName = bName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getbStyle() {
        return bStyle;
    }

    public void setbStyle(String bStyle) {
        this.bStyle = bStyle;
    }

    public String getPublish() {
        return publish;
    }

    public void setPublish(String publish) {
        this.publish = publish;
     }
    }

//管理系统
LibrarySystem :

import com.ndky.devil4.DbConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class LibrarySystem {
    private static Connection conn=null;
    private static PreparedStatement pst=null;
    Scanner sc=new Scanner(System.in);
    ResultSet rs=null;
    //初始界面
    public void print(Library library) {

        boolean flag = true;
        while (flag) {
            System.out.println("******************图书馆系统******************");
            System.out.println("*********1 添加图书信息      *****************");
            System.out.println("*********2 查看图书信息      *****************");
            System.out.println("*********3 修改图书信息      *****************");
            System.out.println("*********4 删除图书信息      *****************");
            System.out.println("*********5 退出              *****************");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    InsertBook(library);
                    break;
                case 2:
                    lookBook(library);
                    break;
                case 3:
                    UpdateBook(library);
                    break;
                case 4:
                    deleteBook(library);
                    break;
                case 5:
                    flag = false;
                    break;
                default:
                    System.out.println("请输入1-5");
                    break;
            }
        }
    }
//删除图书
    private void deleteBook(Library library) {
        try {
            conn = DbConn.GetConnection();
            String sql = "delete from "+ library.getClass().getSimpleName().toLowerCase() +  "  where bName=?";
            pst = conn.prepareStatement(sql);
            System.out.println("请输入书名:");
            library.setbName(sc.next());
            pst.setString(1, library.getbName());
            int i = pst.executeUpdate();
            System.out.println(i);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

//修改图书
    private void UpdateBook(Library library) {
        try {
            conn = DbConn.GetConnection();
            String sql = "update "+ library.getClass().getSimpleName().toLowerCase() +  " set bStyle=? where bName=?";
            pst = conn.prepareStatement(sql);
            System.out.println("请输入类型:");
            library.setbStyle(sc.next());
            System.out.println("请输入书名:");
            library.setbName(sc.next());
            pst.setString(1, library.getbStyle());
            pst.setString(2, library.getbName());
            int count = pst.executeUpdate();
            System.out.println(count);
            DBUtils.close(pst,conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
//查看图书
    private void lookBook(Library library) {
        try {
            conn = DbConn.GetConnection();
            String sql = "select * from "+ library.getClass().getSimpleName().toLowerCase();
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString("bId"));
                System.out.println(rs.getString("bName"));
                System.out.println(rs.getString("author"));
                System.out.println(rs.getString("bStyle"));
                System.out.println(rs.getString("publish"));
            }
            DBUtils.close(pst,conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
//增加图书
    public  void InsertBook(Library library) {
        try {
            conn = DBUtils.getConn();
            String sql = "insert into "+ library.getClass().getSimpleName().toLowerCase() +  " (bName,author,bStyle,publish) values(?,?,?,?)";
           /* String sql1 = "insert into " + library.getClass().getSimpleName().toLowerCase() + "(";
            String sql2 = ") values(";
            Field[] fields = library.getClass().getDeclaredFields();
            for (int i = 1; i < fields.length; i++) {
                fields[i].setAccessible(true);
                if (i == 1) {
                    sql1 = sql1 + fields[i];
                    sql2 = sql2 + "?";
                } else {
                    sql1 = sql1 + "," + fields[i];
                    sql2 = sql2 + ",?";
                }
            }
            String sql = sql1 + sql2 + ")";*/
            System.out.println(sql);
            pst = conn.prepareStatement(sql);
            System.out.println("请输入书名:");
            library.setbName(sc.next());
            System.out.println("请输入作者:");
            library.setAuthor(sc.next());
            System.out.println("请输入类型:");
            library.setbStyle(sc.next());
            System.out.println("请输入出版社:");
            library.setPublish(sc.next());
            pst.setString(1, library.getbName());
            pst.setString(2, library.getAuthor());
            pst.setString(3, library.getbStyle());
            pst.setString(4, library.getPublish());
            int i = pst.executeUpdate();
            System.out.println(i);
            DBUtils.close(pst,conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        Library library=new Library();
        LibrarySystem libarySystem = new LibrarySystem();
        libarySystem.print(library);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值