当前位置: 首页 > 面试题库 >

您的SQL语法有误;检查与您的MariaDB服务器版本相对应的手册,以获取在'?'附近使用的正确语法。在第1行[重复]

钱华晖
2023-03-14
问题内容

我正在使用Java和建立库存系统JDBC。查询表时出现此错误。

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以获取在’?’附近使用的正确语法。在第1行

下面是代码

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将代码更改为以下代码

ResultSet rsOrders = stmt2.executeQuery();


 类似资料: