编写一个应用程序,用预处理方式在该数据库中实现增加、删除和修改数据以及查询的基本功能。
时间: 2024-03-10 17:43:42 浏览: 67
好的,我可以给你提供一个基于Java语言的MySQL数据库操作的示例代码。在这个示例中,我们使用了JDBC连接MySQL数据库,并使用预处理语句来执行增删改查操作。
请注意,在运行这个示例代码之前,你需要先创建一个名为Books的数据库,并在其中创建一个名为book_message的表,该表包含五个字段:book_name、author、publisher、publish_time和price。
```java
import java.sql.*;
public class BooksDatabase {
// 数据库连接信息
private static final String URL = "jdbc:mysql://localhost:3306/Books";
private static final String USER = "root";
private static final String PASSWORD = "123456";
// 增加数据
public static void addBook(String bookName, String author, String publisher, String publishTime, double price) {
try(Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO book_message (book_name, author, publisher, publish_time, price) VALUES (?, ?, ?, ?, ?)")){
stmt.setString(1, bookName);
stmt.setString(2, author);
stmt.setString(3, publisher);
stmt.setString(4, publishTime);
stmt.setDouble(5, price);
int rows = stmt.executeUpdate();
if(rows > 0) {
System.out.println("添加成功!");
}else {
System.out.println("添加失败!");
}
}catch(SQLException e) {
e.printStackTrace();
}
}
// 删除数据
public static void deleteBook(String bookName) {
try(Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement stmt = conn.prepareStatement("DELETE FROM book_message WHERE book_name = ?")){
stmt.setString(1, bookName);
int rows = stmt.executeUpdate();
if(rows > 0) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
}catch(SQLException e) {
e.printStackTrace();
}
}
// 修改数据
public static void updateBook(String bookName, String author, String publisher, String publishTime, double price) {
try(Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement stmt = conn.prepareStatement("UPDATE book_message SET author = ?, publisher = ?, publish_time = ?, price = ? WHERE book_name = ?")){
stmt.setString(1, author);
stmt.setString(2, publisher);
stmt.setString(3, publishTime);
stmt.setDouble(4, price);
stmt.setString(5, bookName);
int rows = stmt.executeUpdate();
if(rows > 0) {
System.out.println("修改成功!");
}else {
System.out.println("修改失败!");
}
}catch(SQLException e) {
e.printStackTrace();
}
}
// 查询数据
public static void queryBook(String bookName) {
try(Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM book_message WHERE book_name = ?")){
stmt.setString(1, bookName);
ResultSet rs = stmt.executeQuery();
if(rs.next()) {
String author = rs.getString("author");
String publisher = rs.getString("publisher");
String publishTime = rs.getString("publish_time");
double price = rs.getDouble("price");
System.out.println("书名:" + bookName);
System.out.println("作者:" + author);
System.out.println("出版社:" + publisher);
System.out.println("出版时间:" + publishTime);
System.out.println("价格:" + price);
}else {
System.out.println("查询失败!");
}
}catch(SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// 测试增加数据
addBook("Java程序设计", "张三", "清华大学出版社", "2020-01-01", 59.9);
// 测试删除数据
deleteBook("Java程序设计");
// 测试修改数据
updateBook("Java程序设计", "李四", "人民邮电出版社", "2020-01-01", 69.9);
// 测试查询数据
queryBook("Java程序设计");
}
}
```
这个示例程序中,我们定义了四个方法:addBook、deleteBook、updateBook和queryBook,分别用于增加、删除、修改和查询数据。在main方法中,我们调用这些方法来进行测试。
当你运行这个示例程序时,它会先尝试向book_message表中插入一条新数据,然后删除该数据,接着修改该数据,最后查询该数据。在执行每个操作后,程序会输出操作的结果。
阅读全文