import java.sql.*; public class DBUtil { private static final String DRIVER = "com.mysql.jdbc.Driver"; private static final String URL = "jdbc:mysql://localhost:3306/warehouse"; private static final String USERNAME = "root"; private static final String PASSWORD = "123456"; public static Connection getConnection() { Connection conn = null; try { Class.forName(DRIVER); conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (Exception e) { e.printStackTrace(); } return conn; } }
import java.sql.*; public class WarehouseDAO { public List<Warehouse> searchByName(String name) { List<Warehouse> warehouses = new ArrayList<>(); String sql = "SELECT * FROM warehouse WHERE name = ?"; try(Connection conn = DBUtil.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql); ) { stmt.setString(1, name); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { Warehouse warehouse = new Warehouse(); warehouse.setId(rs.getInt("id")); warehouse.setName(rs.getString("name")); warehouse.setType(rs.getString("type")); // 其他属性的赋值 warehouses.add(warehouse); } } } catch (SQLException e) { e.printStackTrace(); } return warehouses; } }
import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; public class WarehouseExporter { public void exportToExcel(List<Warehouse> warehouses, String filename) { try (Workbook workbook = new XSSFWorkbook()) { Sheet sheet = workbook.createSheet("Warehouse Data"); // 创建表头 Row headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue("ID"); headerRow.createCell(1).setCellValue("名称"); headerRow.createCell(2).setCellValue("类型"); // 其他属性的设置 int rowNum = 1; for (Warehouse warehouse : warehouses) { Row row = sheet.createRow(rowNum++); row.createCell(0).setCellValue(warehouse.getId()); row.createCell(1).setCellValue(warehouse.getName()); row.createCell(2).setCellValue(warehouse.getType()); // 其他属性的设置 } // 将数据写入文件 try (FileOutputStream outputStream = new FileOutputStream(filename)) { workbook.write(outputStream); } } catch (IOException e) { e.printStackTrace(); } } }
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.List; public class WarehouseExporter { public void exportToCSV(List<Warehouse> warehouses, String filename) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) { // 写入表头 writer.write("ID,名称,类型 "); // 写入数据 for (Warehouse warehouse : warehouses) { writer.write(warehouse.getId() + "," + warehouse.getName() + "," + warehouse.getType() + " "); } } catch (IOException e) { e.printStackTrace(); } } }
以上がJavaを使用して倉庫管理システムのバッチクエリおよびデータエクスポート機能を実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。