数据库图书馆管理系统源代码csdn
时间: 2024-01-16 07:00:39 浏览: 81
数据库图书馆管理系统源代码CSDN(中国软件开发网)是一个广受开发者欢迎的技术交流平台,提供了丰富的数据库图书馆管理系统源代码资源。以下是一个简单的示例代码:
```java
import java.sql.*;
import java.util.Scanner;
public class LibraryManagementSystem {
static final String DB_URL = "jdbc:mysql://localhost/library_management";
static final String USER = "root";
static final String PASS = "password";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
System.out.println("欢迎使用图书馆管理系统");
System.out.println("请选择操作:");
System.out.println("1. 添加书籍");
System.out.println("2. 删除书籍");
System.out.println("3. 查询书籍");
System.out.print("请输入操作编号:");
int option = scanner.nextInt();
switch (option) {
case 1:
System.out.print("请输入书籍名称:");
String bookName = scanner.next();
String sql = "INSERT INTO books (name) VALUES ('" + bookName + "')";
stmt.executeUpdate(sql);
System.out.println("书籍添加成功");
break;
case 2:
System.out.print("请输入书籍ID:");
int bookId = scanner.nextInt();
sql = "DELETE FROM books WHERE id = " + bookId;
stmt.executeUpdate(sql);
System.out.println("书籍删除成功");
break;
case 3:
sql = "SELECT * FROM books";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println("ID: " + id + ", 书籍名称: " + name);
}
rs.close();
break;
default:
System.out.println("无效的操作编号");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}
```
以上示例代码实现了一个简单的图书馆管理系统,提供了添加、删除和查询书籍的功能。通过与MySQL数据库的交互实现了对图书馆的基本管理操作。详细的源代码可以在CSDN上进行搜索和查看。
阅读全文