```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class QueryTonyCoupon { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/db_name"; String username = "username"; String password = "password"; String customerName = "Tony"; try { // 创建数据库连接 Connection conn = DriverManager.getConnection(url, username, password); // 创建 SQL 查询语句 String sql = "SELECT id, name, discount_price, status FROM coupon WHERE customer_name = ? AND status = 'valid'"; // 创建 PreparedStatement 对象 PreparedStatement pstmt = conn.prepareStatement(sql); // 设置参数 pstmt.setString(1, customerName); // 执行查询 ResultSet rs = pstmt.executeQuery(); // 遍历查询结果并输出 while (rs.next()) { int couponId = rs.getInt("id"); String couponName = rs.getString("name"); double discountPrice = rs.getDouble("discount_price"); String couponStatus = rs.getString("status"); System.out.println("优惠券ID: " + couponId + ", 优惠券名称: " + couponName + ", 优惠价格: " + discountPrice + ", 优惠状态: " + couponStatus); } // 关闭资源 rs.close(); pstmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` [2024-06-29 15:21:31 | AI写代码神器 | 1076点数解答]