代码各自放在什么文件位置
时间: 2024-12-25 16:30:03 浏览: 5
根据《作业六指导任务书》的内容,各个部分的代码应放置在以下文件中:
1. **JavaBean**:用于保存图书数据,包括ID、名称、价格。可以命名为 `Book.java`。
```java
public class Book {
private int id;
private String name;
private double price;
// 构造方法、getter 和 setter 方法
}
```
2. **JDBCUtils 工具类**:用于连接数据库与释放资源。可以命名为 `JDBCUtils.java`。
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCUtils {
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdatabase", "username", "password");
}
public static void closeResource(Connection conn, Statement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
3. **DAO 类**:封装对表 `books` 的添加、查询、删除等操作。可以命名为 `BookDAO.java`。
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class BookDAO {
public List<Book> queryAllBooks() {
List<Book> books = new ArrayList<>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement("SELECT * FROM books");
rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setPrice(rs.getDouble("price"));
books.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps, rs);
}
return books;
}
public Book queryBookById(int id) {
Book book = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement("SELECT * FROM books WHERE id = ?");
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setPrice(rs.getDouble("price"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps, rs);
}
return book;
}
public boolean deleteBookById(int id) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement("DELETE FROM books WHERE id = ?");
ps.setInt(1, id);
int rows = ps.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps, null);
}
return false;
}
public boolean addBook(Book book) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement("INSERT INTO books (name, price) VALUES (?, ?)");
ps.setString(1, book.getName());
ps.setDouble(2, book.getPrice());
int rows = ps.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps, null);
}
return false;
}
}
```
4. **测试类**:编写一个测试类 `Test.java` 来执行查询、删除、添加操作,并打印结果。
```java
public class Test {
public static void main(String[] args) {
BookDAO bookDAO = new BookDAO();
// 查询 ID 为 1 的图书信息并显示在控制台上
Book book = bookDAO.queryBookById(1);
System.out.println("查询到的图书信息: " + book);
// 删除 ID 为 1 的图书记录
boolean isDeleted = bookDAO.deleteBookById(1);
System.out.println("删除操作是否成功: " + isDeleted);
// 添加一本新的书
Book newBook = new Book();
newBook.setName("新书名");
newBook.setPrice(99.99);
boolean isAdded = bookDAO.addBook(newBook);
System.out.println("添加操作是否成功: " + isAdded);
// 打印新的 books 表在控制台上
List<Book> allBooks = bookDAO.queryAllBooks();
for (Book b : allBooks) {
System.out.println(b);
}
}
}
```
确保每个类都在合适的包中,并且项目的目录结构清晰合理。最后,将配置好的项目打包成一个压缩文件,以“学号 + 姓名”的格式命名并提交。例如:“2015010101 张三.rar”。
阅读全文