酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

Java代码实现:精准查询Tony的有效优惠券信息(含ID、名称、价格与状态)

```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(); } } } ``` ######[AI写代码神器 | 1076点数解答 | 2024-06-29 15:21:31]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]