本文介绍了您的SQL语法有误;检查与您的MariaDB服务器版本相对应的手册,以获取在'?'附近使用的正确语法.在第1行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaJDBC创建清单系统.查询表时出现此错误.

I'm making an inventory system using Java and JDBC. I have got this error some how when querying a table.

下面是代码

public static void SearchUser() throws SQLException{        
    String ordersquery = "SELECT * FROM orders WHERE submittedBy = ?";      
    User userDetails = UserController.getUserDetails(username);//gets the details from user tables          
    if (userDetails != null){           
        System.out.println("----Menu----");
        System.out.println();
        System.out.println("1. View Orders Made By This User");
        System.out.println("2. View Most Expensive Parts Currently Taken Out By This User");
        System.out.println();
        System.out.println("9. Go Back To Main Menu");
        choice = input.nextLine();  
        if (choice.equals("1")){
            try (
                    PreparedStatement stmt2 = conn.prepareStatement(ordersquery);

                    ){  
                stmt2.setInt(1, userDetails.getUserId());
                ResultSet rsOrders = stmt2.executeQuery(ordersquery);   
                if (rsOrders != null){
                    while (rsOrders.next()){
                        Order orderDetails = new Order(rsOrders.getInt("orderId"), userDetails.getUserId(), rsOrders.getInt("totalItems"), rsOrders.getInt("totalPrice"));
                        System.out.println("-------------------------------------");
                        Order.print(orderDetails);
                    }
                }
            }catch (SQLException e){
                System.err.println(e);
            }
        }else if (choice.equals("2")){  
        }
    }

}

推荐答案

ResultSet rsOrders = stmt2.executeQuery(ordersquery);

stmt2是您的SQL查询,为什么要传递ordersquery将您的代码更改为以下代码

stmt2 is you sql query then why are you passing ordersquery change your code to below code

ResultSet rsOrders = stmt2.executeQuery();

这篇关于您的SQL语法有误;检查与您的MariaDB服务器版本相对应的手册,以获取在'?'附近使用的正确语法.在第1行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 00:01