图书管理系统java和sql
时间: 2023-12-19 17:05:45 浏览: 79
基于JAVA和SQL-Server图书管理系统课程设计
5星 · 资源好评率100%
针对图书管理系统,Java和SQL都是非常重要的技术。Java可以用于开发系统的后端,而SQL则可以用于管理系统的数据库。下面是一个简单的图书管理系统的Java和SQL实现的例子:
Java代码:
```java
public class Book {
private int id;
private String name;
private String author;
private String publisher;
private double price;
private int stock;
// 构造函数
public Book(int id, String name, String author, String publisher, double price, int stock) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.price = price;
this.stock = stock;
}
// getter和setter方法
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 String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
public class BookDao {
private Connection conn;
// 构造函数
public BookDao(Connection conn) {
this.conn = conn;
}
// 添加图书
public boolean addBook(Book book) {
String sql = "INSERT INTO book (id, name, author, publisher, price, stock) VALUES (?, ?, ?, ?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, book.getId());
pstmt.setString(2, book.getName());
pstmt.setString(3, book.getAuthor());
pstmt.setString(4, book.getPublisher());
pstmt.setDouble(5, book.getPrice());
pstmt.setInt(6, book.getStock());
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// 删除图书
public boolean deleteBook(int id) {
String sql = "DELETE FROM book WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// 修改图书信息
public boolean updateBook(Book book) {
String sql = "UPDATE book SET name = ?, author = ?, publisher = ?, price = ?, stock = ? WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, book.getName());
pstmt.setString(2, book.getAuthor());
pstmt.setString(3, book.getPublisher());
pstmt.setDouble(4, book.getPrice());
pstmt.setInt(5, book.getStock());
pstmt.setInt(6, book.getId());
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// 查询图书信息
public Book queryBook(int id) {
String sql = "SELECT * FROM book WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
Book book = new Book(rs.getInt("id"), rs.getString("name"), rs.getString("author"), rs.getString("publisher"), rs.getDouble("price"), rs.getInt("stock"));
return book;
} else {
return null;
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
public class Main {
public static void main(String[] args) {
// 连接数据库
String url = "jdbc:mysql://localhost:3306/bookstore";
String user = "root";
String password = "123456";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 添加图书
BookDao bookDao = new BookDao(conn);
Book book = new Book(1, "Java编程思想", "Bruce Eckel", "机械工业出版社", 99.0, 100);
bookDao.addBook(book);
// 查询图书
Book book1 = bookDao.queryBook(1);
System.out.println(book1.getName());
// 修改图书
book.setPrice(88.0);
bookDao.updateBook(book);
// 删除图书
bookDao.deleteBook(1);
// 关闭数据库连接
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
SQL代码:
```sql
CREATE DATABASE bookstore;
USE bookstore;
CREATE TABLE book (
id INT PRIMARY KEY,
name VARCHAR(50),
author VARCHAR(50),
publisher VARCHAR(50),
price DOUBLE,
stock INT
);
```
阅读全文