图书管理系统java连接数据库实现增删改查
时间: 2024-01-05 19:03:59 浏览: 60
以下是一个简单的图书管理系统的Java代码示例,用于连接数据库并实现增删改查功能:
1. 连接数据库:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
private static final String URL = "jdbc:mysql://localhost:3306/library";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public static Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
2. 添加图书:
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class BookDao {
public int addBook(Book book) {
int flag = 0;
Connection conn = null;
PreparedStatement pst = null;
try {
conn = DBConnection.getConn();
String sql = "INSERT INTO books (title, author, price) VALUES (?, ?, ?)";
pst = conn.prepareStatement(sql);
pst.setString(1, book.getTitle());
pst.setString(2, book.getAuthor());
pst.setDouble(3, book.getPrice());
flag = pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.close(conn);
}
return flag;
}
}
```
3. 删除图书:
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class BookDao {
public int deleteBook(int id) {
int flag = 0;
Connection conn = null;
PreparedStatement pst = null;
try {
conn = DBConnection.getConn();
String sql = "DELETE FROM books WHERE id = ?";
pst = conn.prepareStatement(sql);
pst.setInt(1, id);
flag = pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.close(conn);
}
return flag;
}
}
```
4. 修改图书信息:
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class BookDao {
public int updateBook(Book book) {
int flag = 0;
Connection conn = null;
PreparedStatement pst = null;
try {
conn = DBConnection.getConn();
String sql = "UPDATE books SET title = ?, author = ?, price = ? WHERE id = ?";
pst = conn.prepareStatement(sql);
pst.setString(1, book.getTitle());
pst.setString(2, book.getAuthor());
pst.setDouble(3, book.getPrice());
pst.setInt(4, book.getId());
flag = pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.close(conn);
}
return flag;
}
}
```
5. 查询图书:
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BookDao {
public List<Book> getAllBooks() {
List<Book> bookList = new ArrayList<>();
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
conn = DBConnection.getConn();
String sql = "SELECT * FROM books";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String author = rs.getString("author");
double price = rs.getDouble("price");
Book book = new Book(id, title, author, price);
bookList.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.close(conn);
}
return bookList;
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)