java+SQLite:实现设计一个利用的图书管理系统
以下是一个简单的Java程序,用于创建一个图书管理系统的SQLite数据库和一个添加图书的简单示例。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class BookManagementSystem {
private Connection connect = null;
private String url = "jdbc:sqlite:bookstore.db"; // SQLite数据库文件路径
public BookManagementSystem() {
createBookStoreDatabase();
}
private void createBookStoreDatabase() {
try {
connect = DriverManager.getConnection(url);
// 创建图书表
String sql = "CREATE TABLE IF NOT EXISTS books " +
"(id INTEGER PRIMARY KEY, title TEXT, author TEXT, isbn TEXT)";
connect.prepareStatement(sql).executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
if (connect != null && !connect.isClosed()) {
connect.close();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
public void addBook(String title, String author, String isbn) {
try {
connect = DriverManager.getConnection(url);
String sql = "INSERT INTO books (title, author, isbn) VALUES (?, ?, ?)";
PreparedStatement statement = connect.prepareStatement(sql);
statement.setString(1, title);
statement.setString(2, author);
statement.setString(3, isbn);
statement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
if (connect != null && !connect.isClosed()) {
connect.close();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
public static void main(String[] args) {
BookManagementSystem bookSystem = new BookManagementSystem();
bookSystem.addBook("Java Programm
评论已关闭