基于java开发图书管理系统sqlserver的代码
时间: 2023-10-11 21:05:55 浏览: 107
以下是一个简单的基于 Java 和 SQL Server 的图书管理系统的代码示例。实现了图书的添加、删除、修改和查询功能。
注意:本代码仅供参考,需要根据实际情况进行修改和完善。
```java
import java.sql.*;
public class BookManager {
private Connection conn;
public BookManager() {
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Library";
String user = "sa";
String password = "123456";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public void addBook(Book book) {
String sql = "INSERT INTO Book (title, author, publisher, price) VALUES (?, ?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, book.getTitle());
pstmt.setString(2, book.getAuthor());
pstmt.setString(3, book.getPublisher());
pstmt.setDouble(4, book.getPrice());
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteBook(int id) {
String sql = "DELETE FROM Book WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateBook(Book book) {
String sql = "UPDATE Book SET title = ?, author = ?, publisher = ?, price = ? WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, book.getTitle());
pstmt.setString(2, book.getAuthor());
pstmt.setString(3, book.getPublisher());
pstmt.setDouble(4, book.getPrice());
pstmt.setInt(5, book.getId());
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void queryBooks() {
String sql = "SELECT * FROM Book";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String author = rs.getString("author");
String publisher = rs.getString("publisher");
double price = rs.getDouble("price");
System.out.println(id + "\t" + title + "\t" + author + "\t" + publisher + "\t" + price);
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
BookManager manager = new BookManager();
// 添加图书
Book book1 = new Book("Java编程思想", "Bruce Eckel", "机械工业出版社", 89.00);
manager.addBook(book1);
// 修改图书
Book book2 = new Book(1, "Java编程思想(第二版)", "Bruce Eckel", "机械工业出版社", 99.00);
manager.updateBook(book2);
// 查询图书
manager.queryBooks();
// 删除图书
manager.deleteBook(1);
}
}
class Book {
private int id;
private String title;
private String author;
private String publisher;
private double price;
public Book(String title, String author, String publisher, double price) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.price = price;
}
public Book(int id, String title, String author, String publisher, double price) {
this.id = id;
this.title = title;
this.author = author;
this.publisher = publisher;
this.price = price;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getPublisher() {
return publisher;
}
public double getPrice() {
return price;
}
}
```
在这个示例中,我们使用了 JDBC 驱动程序连接到 SQL Server 数据库,并实现了添加、删除、修改和查询图书的功能。这个示例中的代码只是一个简单的示例,实际的应用中还需要考虑更多的因素,如异常处理、事务处理等。
阅读全文