做一个图书管理系统java sqlserver
时间: 2023-10-29 11:05:16 浏览: 155
好的,下面是一个简单的图书管理系统的Java代码,使用的数据库是SQL Server。
首先,我们需要创建一个数据库,并在其中创建两个表:`book` 和 `borrow`。
```sql
CREATE DATABASE library;
USE library;
CREATE TABLE book (
id INT PRIMARY KEY,
title VARCHAR(50),
author VARCHAR(50),
publisher VARCHAR(50),
publish_date DATE,
price FLOAT,
quantity INT
);
CREATE TABLE borrow (
id INT PRIMARY KEY,
book_id INT,
user_id INT,
borrow_date DATE,
return_date DATE
);
```
然后,我们可以创建一个Java类 `Book` 来表示图书信息。
```java
public class Book {
private int id;
private String title;
private String author;
private String publisher;
private Date publishDate;
private float price;
private int quantity;
// 构造器和 getter/setter 略
}
```
接下来,我们可以创建一个 `Library` 类来实现图书管理系统的功能。
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Library {
private Connection conn;
public Library(String url, String username, String password) throws SQLException {
conn = DriverManager.getConnection(url, username, password);
}
public List<Book> searchBooks(String keyword) throws SQLException {
List<Book> books = new ArrayList<>();
String sql = "SELECT * FROM book WHERE title LIKE ? OR author LIKE ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + keyword + "%");
pstmt.setString(2, "%" + keyword + "%");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setTitle(rs.getString("title"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setPublishDate(rs.getDate("publish_date"));
book.setPrice(rs.getFloat("price"));
book.setQuantity(rs.getInt("quantity"));
books.add(book);
}
return books;
}
public void borrowBook(int bookId, int userId, Date borrowDate, Date returnDate) throws SQLException {
String sql = "INSERT INTO borrow (book_id, user_id, borrow_date, return_date) VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bookId);
pstmt.setInt(2, userId);
pstmt.setDate(3, borrowDate);
pstmt.setDate(4, returnDate);
pstmt.executeUpdate();
sql = "UPDATE book SET quantity = quantity - 1 WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bookId);
pstmt.executeUpdate();
}
public void returnBook(int bookId, int userId) throws SQLException {
String sql = "UPDATE borrow SET return_date = GETDATE() WHERE book_id = ? AND user_id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bookId);
pstmt.setInt(2, userId);
pstmt.executeUpdate();
sql = "UPDATE book SET quantity = quantity + 1 WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bookId);
pstmt.executeUpdate();
}
public void close() throws SQLException {
conn.close();
}
}
```
在 `Library` 类中,我们实现了三个方法:
- `searchBooks()`:根据关键字搜索图书信息。
- `borrowBook()`:借阅图书并更新数据库中的数据。
- `returnBook()`:归还图书并更新数据库中的数据。
最后,我们可以编写一个主程序来测试图书管理系统。
```java
import java.sql.*;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String url = "jdbc:sqlserver://localhost:1433;databaseName=library";
String username = "sa";
String password = "123456";
try (Library library = new Library(url, username, password)) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 搜索图书");
System.out.println("2. 借阅图书");
System.out.println("3. 归还图书");
System.out.println("4. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("请输入关键字:");
String keyword = scanner.next();
List<Book> books = library.searchBooks(keyword);
for (Book book : books) {
System.out.println(book.getTitle() + " " + book.getAuthor() + " " + book.getQuantity());
}
break;
case 2:
System.out.print("请输入图书编号:");
int bookId = scanner.nextInt();
System.out.print("请输入用户编号:");
int userId = scanner.nextInt();
System.out.print("请输入借阅日期(yyyy-MM-dd):");
String borrowDateStr = scanner.next();
Date borrowDate = Date.valueOf(borrowDateStr);
System.out.print("请输入归还日期(yyyy-MM-dd):");
String returnDateStr = scanner.next();
Date returnDate = Date.valueOf(returnDateStr);
library.borrowBook(bookId, userId, borrowDate, returnDate);
System.out.println("借阅成功!");
break;
case 3:
System.out.print("请输入图书编号:");
bookId = scanner.nextInt();
System.out.print("请输入用户编号:");
userId = scanner.nextInt();
library.returnBook(bookId, userId);
System.out.println("归还成功!");
break;
case 4:
return;
default:
System.out.println("无效操作,请重新选择!");
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
这样,我们就完成了一个简单的图书管理系统。当然,这只是一个示例,实际的系统需要考虑更多的功能和异常情况。
阅读全文