生成一个用Java图书管理系统结合数据库代码
时间: 2024-06-08 18:07:30 浏览: 21
很抱歉,我目前并不支持生成代码功能,但我可以为您提供相关的Java图书管理系统结合数据库的参考代码。
首先,在Java中使用MySQL数据库,需要下载并安装MySQL JDBC驱动程序。可以在以下链接中下载:https://dev.mysql.com/downloads/connector/j/
接下来,我们来创建一个名为"BookManager"的Java项目。在该项目中,我们创建一个名为"Book"的Java类,来定义图书对象的属性和方法:
```java
public class Book {
private int id;
private String title;
private String author;
private String publisher;
private int year;
private double price;
public Book(int id, String title, String author, String publisher, int year, double price) {
this.id = id;
this.title = title;
this.author = author;
this.publisher = publisher;
this.year = year;
this.price = price;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getPublisher() {
return publisher;
}
public int getYear() {
return year;
}
public double getPrice() {
return price;
}
public String toString() {
return "ID: " + id + " Title: " + title + " Author: " + author + " Publisher: " + publisher + " Year: " + year + " Price: " + price;
}
}
```
接下来,我们创建一个名为"Database"的Java类,来实现与MySQL数据库的连接和操作:
```java
import java.sql.*;
public class Database {
private Connection connection;
private Statement statement;
private ResultSet resultSet;
public void connect() {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookmanager", "root", "password");
statement = connection.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void addBook(Book book) {
try {
String query = "INSERT INTO books (id, title, author, publisher, year, price) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, book.getId());
preparedStatement.setString(2, book.getTitle());
preparedStatement.setString(3, book.getAuthor());
preparedStatement.setString(4, book.getPublisher());
preparedStatement.setInt(5, book.getYear());
preparedStatement.setDouble(6, book.getPrice());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void updateBook(Book book) {
try {
String query = "UPDATE books SET title=?, author=?, publisher=?, year=?, price=? WHERE id=?";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, book.getTitle());
preparedStatement.setString(2, book.getAuthor());
preparedStatement.setString(3, book.getPublisher());
preparedStatement.setInt(4, book.getYear());
preparedStatement.setDouble(5, book.getPrice());
preparedStatement.setInt(6, book.getId());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteBook(int id) {
try {
String query = "DELETE FROM books WHERE id=?";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Book getBook(int id) {
try {
String query = "SELECT * FROM books WHERE id=?";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
int bookId = resultSet.getInt("id");
String title = resultSet.getString("title");
String author = resultSet.getString("author");
String publisher = resultSet.getString("publisher");
int year = resultSet.getInt("year");
double price = resultSet.getDouble("price");
Book book = new Book(bookId, title, author, publisher, year, price);
return book;
}
preparedStatement.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void displayBooks() {
try {
String query = "SELECT * FROM books";
resultSet = statement.executeQuery(query);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String title = resultSet.getString("title");
String author = resultSet.getString("author");
String publisher = resultSet.getString("publisher");
int year = resultSet.getInt("year");
double price = resultSet.getDouble("price");
Book book = new Book(id, title, author, publisher, year, price);
System.out.println(book.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
在该类中,我们定义了以下方法:
- connect():连接到MySQL数据库。
- disconnect():断开与MySQL数据库的连接。
- addBook():向数据库中添加图书。
- updateBook():更新数据库中的图书信息。
- deleteBook():从数据库中删除图书。
- getBook():根据图书ID从数据库中获取图书信息。
- displayBooks():显示数据库中所有图书的信息。
最后,我们创建一个名为"Main"的Java类,来测试我们的Java图书管理系统:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Database database = new Database();
database.connect();
while (true) {
System.out.println("1. Add book");
System.out.println("2. Update book");
System.out.println("3. Delete book");
System.out.println("4. Get book");
System.out.println("5. Display books");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter book id: ");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter book author: ");
String author = scanner.nextLine();
System.out.print("Enter book publisher: ");
String publisher = scanner.nextLine();
System.out.print("Enter book year: ");
int year = scanner.nextInt();
System.out.print("Enter book price: ");
double price = scanner.nextDouble();
Book book = new Book(id, title, author, publisher, year, price);
database.addBook(book);
break;
case 2:
System.out.print("Enter book id: ");
int updateId = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter book title: ");
String updateTitle = scanner.nextLine();
System.out.print("Enter book author: ");
String updateAuthor = scanner.nextLine();
System.out.print("Enter book publisher: ");
String updatePublisher = scanner.nextLine();
System.out.print("Enter book year: ");
int updateYear = scanner.nextInt();
System.out.print("Enter book price: ");
double updatePrice = scanner.nextDouble();
Book updateBook = new Book(updateId, updateTitle, updateAuthor, updatePublisher, updateYear, updatePrice);
database.updateBook(updateBook);
break;
case 3:
System.out.print("Enter book id: ");
int deleteId = scanner.nextInt();
database.deleteBook(deleteId);
break;
case 4:
System.out.print("Enter book id: ");
int getBookId = scanner.nextInt();
Book getBook = database.getBook(getBookId);
if (getBook != null) {
System.out.println(getBook.toString());
} else {
System.out.println("Book not found");
}
break;
case 5:
database.displayBooks();
break;
case 6:
database.disconnect();
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}
```
在该类中,我们使用Scanner类来获取用户输入,并根据用户选择调用数据库类中的相应方法。
这就是一个简单的Java图书管理系统结合MySQL数据库的实现。需要注意的是,这只是一个简单的示例代码,实际开发中可能需要更多的功能和更严格的数据校验。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)