用java代码设计一个无图形界面的简单的图书管理系统,要求服务器端管理数据库,管理相应书籍
时间: 2024-03-22 08:40:14 浏览: 13
好的,以下是一个简单的Java图书管理系统的代码实现,使用了MySQL作为数据库:
服务器端代码:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.*;
public class BookServer {
private static final int PORT = 8888;
private static final String DB_URL = "jdbc:mysql://localhost:3306/book";
private static final String USER = "root";
private static final String PASS = "123456";
public static void main(String[] args) {
try {
// 创建ServerSocket,监听端口
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("服务器启动,监听端口:" + PORT);
while (true) {
// 等待客户端连接
Socket socket = serverSocket.accept();
System.out.println("客户端已连接:" + socket.getInetAddress());
// 创建输入输出流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// 处理客户端请求
String request = in.readLine();
String response = "";
if (request.startsWith("add_book")) {
// 添加图书
String[] params = request.split(",");
if (params.length == 5) {
String name = params[1];
String author = params[2];
String publisher = params[3];
String isbn = params[4];
if (addBook(name, author, publisher, isbn)) {
response = "添加成功";
} else {
response = "添加失败";
}
} else {
response = "参数错误";
}
} else if (request.startsWith("delete_book")) {
// 删除图书
String[] params = request.split(",");
if (params.length == 2) {
int id = Integer.parseInt(params[1]);
if (deleteBook(id)) {
response = "删除成功";
} else {
response = "删除失败";
}
} else {
response = "参数错误";
}
} else if (request.startsWith("query_book")) {
// 查询图书
String[] params = request.split(",");
if (params.length == 2) {
int id = Integer.parseInt(params[1]);
Book book = queryBook(id);
if (book != null) {
response = book.toString();
} else {
response = "查询失败";
}
} else {
response = "参数错误";
}
} else if (request.startsWith("update_book")) {
// 修改图书
String[] params = request.split(",");
if (params.length == 6) {
int id = Integer.parseInt(params[1]);
String name = params[2];
String author = params[3];
String publisher = params[4];
String isbn = params[5];
if (updateBook(id, name, author, publisher, isbn)) {
response = "修改成功";
} else {
response = "修改失败";
}
} else {
response = "参数错误";
}
} else {
response = "未知请求";
}
// 发送响应
out.println(response);
// 关闭输入输出流和Socket
in.close();
out.close();
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 添加图书
private static boolean addBook(String name, String author, String publisher, String isbn) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
pstmt = conn.prepareStatement("INSERT INTO book_info(name, author, publisher, isbn) VALUES (?, ?, ?, ?)");
pstmt.setString(1, name);
pstmt.setString(2, author);
pstmt.setString(3, publisher);
pstmt.setString(4, isbn);
int result = pstmt.executeUpdate();
return result > 0;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 删除图书
private static boolean deleteBook(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
pstmt = conn.prepareStatement("DELETE FROM book_info WHERE id = ?");
pstmt.setInt(1, id);
int result = pstmt.executeUpdate();
return result > 0;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 查询图书
private static Book queryBook(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
pstmt = conn.prepareStatement("SELECT * FROM book_info WHERE id = ?");
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
String author = rs.getString("author");
String publisher = rs.getString("publisher");
String isbn = rs.getString("isbn");
return new Book(id, name, author, publisher, isbn);
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 修改图书
private static boolean updateBook(int id, String name, String author, String publisher, String isbn) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
pstmt = conn.prepareStatement("UPDATE book_info SET name = ?, author = ?, publisher = ?, isbn = ? WHERE id = ?");
pstmt.setString(1, name);
pstmt.setString(2, author);
pstmt.setString(3, publisher);
pstmt.setString(4, isbn);
pstmt.setInt(5, id);
int result = pstmt.executeUpdate();
return result > 0;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class Book {
private int id;
private String name;
private String author;
private String publisher;
private String isbn;
public Book(int id, String name, String author, String publisher, String isbn) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.isbn = isbn;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public String getPublisher() {
return publisher;
}
public String getIsbn() {
return isbn;
}
public String toString() {
return "编号:" + id + ",书名:" + name + ",作者:" + author + ",出版社:" + publisher + ",ISBN号:" + isbn;
}
}
```
客户端代码:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class BookClient {
private static final String SERVER_IP = "localhost";
private static final int SERVER_PORT = 8888;
public static void main(String[] args) {
try {
// 创建Socket,连接服务器
Socket socket = new Socket(SERVER_IP, SERVER_PORT);
System.out.println("连接服务器成功:" + socket.getInetAddress());
// 创建输入输出流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// 发送请求
out.println("query_book,1"); // 查询编号为1的图书
// 接收响应
String response = in.readLine();
System.out.println("服务器响应:" + response);
// 关闭输入输出流和Socket
in.close();
out.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
以上代码实现了一个简单的Java图书管理系统,可以根据实际需求进行扩展和优化。