如何编写查询以显示名称包含'DM'的玩具?

问题描述 投票:0回答:1

这是我按下可运行程序时的代码,我什么也没收到(已连接)我想展示名称包含“ 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个回答
0
投票

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

要获取所有名称包含'DM'的玩具,您需要使用LIKE,因此将您的sql查询更新为这样:

String query = "SELECT ToyName, Price, color from Toy WHERE ToyName LIKE '%DM%'";
© www.soinside.com 2019 - 2024. All rights reserved.