【问题标题】:how to write the query to display the toys whose name contains 'DM'?如何编写查询以显示名称中包含“DM”的玩具?
【发布时间】:2020-07-24 15:29:42
【问题描述】:

这是我的代码,当我按下可运行文件时,我什么也没得到(已连接) 我想展示名称中包含“DM”的玩具

import java.sql.*;
public class Toy {
public static void main(String args[]) throws Exception {
String query = "SELECT ToyName, Price, color from Toy WHERE ToyName='DM '";
System.out.println("concected");
Connection c=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\96650\\Documents\\toy.accdb");
Statement stmt = c.createStatement();   
ResultSet rs = stmt.executeQuery(query);
while(rs.next()) {
            String desc = rs.getString("ToyName");
            String color = rs.getString("Color");
            double price=rs.getDouble("Price");
            System.out.println(desc+"  Color: "+color+"\n $"+price);}}}

【问题讨论】:

    标签: java eclipse jdbc


    【解决方案1】:

    我认为您的 SQL 查询是错误的。当前查询将选择带有toyName == 'DM ' 的玩具,我认为这不是您想要的。

    要获取名称中包含“DM”的所有玩具,您需要使用LIKE,因此将您的 sql 查询更新为如下所示:

    String query = "SELECT ToyName, Price, color from Toy WHERE ToyName LIKE '%DM%'";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-26
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 2021-02-12
      相关资源
      最近更新 更多