这什么情况,本来就不是很理解,为什么没报错却连不上数据库,帮我看看,学的不是很好
下面是数据库
import java.sql.*;
import javax.swing.*;
public class BookManagementSystem {
private static final String DB_NAME = "图书数据";
private static final String DB_URL = "jdbc:mysql://localhost:3306/图书数据";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "12345";
private static Connection connection;
public static void main(String[] args) {
try {
connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
createBookTable();
int choice;
do {
choice = Integer.parseInt(JOptionPane.showInputDialog(
"图书信息管理\n" +
"查询请按 1\n" +
"添加请按 2\n" +
"修改请按 3\n" +
"删除请按 4\n" +
"退出请按 0\n" +
"输入选项:"));
switch (choice) {
case 0:
System.exit(0);
break;
case 1:
showBooks();
break;
case 2:
addBook();
break;
case 3:
modifyBook();
break;
case 4:
deleteBook();
break;
default:
System.out.println("输入错误");
break;
}
} while (true);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
private static void createBookTable() throws SQLException {
String createTableQuery = "CREATE TABLE IF NOT EXISTS books (" +
"id INT AUTO_INCREMENT PRIMARY KEY," +
"title VARCHAR(255) NOT NULL," +
"author VARCHAR(255) NOT NULL," +
"price DECIMAL(10, 2) NOT NULL" +
");";
try (Statement statement = connection.createStatement()) {
statement.executeUpdate(createTableQuery);
}
}
private static void showBooks() throws SQLException {
String selectQuery = "SELECT * FROM books;";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectQuery)) {
StringBuilder output = new StringBuilder();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String title = resultSet.getString("title");
String author = resultSet.getString("author");
double price = resultSet.getDouble("price");
output.append("ID: ").append(id).append("\n");
output.append("Title: ").append(title).append("\n");
output.append("Author: ").append(author).append("\n");
output.append("Price: ").append(price).append("\n");
output.append("\n");
}
JOptionPane.showMessageDialog(null, output.toString(), "查询结果", JOptionPane.INFORMATION_MESSAGE);
}
}
private static void addBook() throws SQLException {
String title = JOptionPane.showInputDialog("输入图书标题");
String author = JOptionPane.showInputDialog("输入图书作者");
double price = Double.parseDouble(JOptionPane.showInputDialog("输入图书价格"));
String insertQuery = "INSERT INTO books (title, author, price) VALUES (?, ?, ?);";
try (PreparedStatement statement = connection.prepareStatement(insertQuery)) {
statement.setString(1, title);
statement.setString(2, author);
statement.setDouble(3, price);
int rowsAffected = statement.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(null, "添加成功!", "结果", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "添加失败!", "结果", JOptionPane.ERROR_MESSAGE);
}
}
}
private static void modifyBook() throws SQLException {
int bookId = Integer.parseInt(JOptionPane.showInputDialog("输入需要修改的图书ID"));
String title = JOptionPane.showInputDialog("输入修改后的图书标题");
String author = JOptionPane.showInputDialog("输入修改后的图书作者");
double price = Double.parseDouble(JOptionPane.showInputDialog("输入修改后的图书价格"));
String updateQuery = "UPDATE books SET title = ?, author = ?, price = ? WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(updateQuery)) {
statement.setString(1, title);
statement.setString(2, author);
statement.setDouble(3, price);
statement.setInt(4, bookId);
int rowsAffected = statement.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(null, "修改成功!", "结果", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "修改失败!", "结果", JOptionPane.ERROR_MESSAGE);
}
}
}
private static void deleteBook() throws SQLException {
int bookId = Integer.parseInt(JOptionPane.showInputDialog("输入需要删除的图书ID"));
String deleteQuery = "DELETE FROM books WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteQuery)) {
statement.setInt(1, bookId);
int rowsAffected = statement.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(null, "删除成功!", "结果", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "删除失败!", "结果", JOptionPane.ERROR_MESSAGE);
}
}
}
}
检查一下你项目的jar文件哦,看是否缺少或放错了位置