您的SQL语法有错误;查看与您的MariaDB服务器版本相对应的手册,以便在'附近使用正确的语法?'在第1行

时间:2018-04-15 11:08:28

标签: java jdbc mariadb

我正在使用JavaJDBC制作广告资源系统。我在查询表时遇到了这个错误。

  

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")){  
        }
    }

}

2 个答案:

答案 0 :(得分:0)

ResultSet rsOrders = stmt2.executeQuery(ordersquery);

stmt2是你的SQL查询然后你为什么要通过ordersquery将你的代码更改为以下代码

ResultSet rsOrders = stmt2.executeQuery();

答案 1 :(得分:0)

您似乎使用了错误的sytax来执行executeQuery()。它不期望参数。试试下面的代码。你应该很好

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();
                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")){

        }
    }

}