java.sql.SQLSyntaxErrorException:Select语句不起作用

时间:2019-07-24 03:11:46

标签: java mysql jdbc

我的选择语句显示错误

  

java.sql.SQLSyntaxErrorException:您的SQL错误   句法;检查与您的MySQL服务器版本相对应的手册   在第1行的“食物条件”附近使用正确的语法。

我的参数值为object = "food"columns[] = {"foodName, foodPrice, condition"} 虽然此功能仍可用于我的登录

我已经使用了不同版本的mySQL,并且还在该语句的后面加上了方括号,但仍然无法使用

public static String[][] checkDatabase(String object, String[] columns){
        Connection myCon = null;
        Statement myStm = null;
        ResultSet myRs = null;
        try {
            myCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/softwaredesignorder", "root","SeanLink_11");
            myStm = myCon.createStatement();
            String temp = "select ";
            for (int i = 0; i < columns.length; i++) {
                if (i < columns.length - 1) {
                    temp = temp + columns[i] + ", ";
                } else {
                    temp = temp + columns[i];
                }
            }
            temp = temp + " from " + object;
            //PreparedStatement pStm = myCon.prepareStatement(temp);
            //pStm.execute();
            myRs = myStm.executeQuery(temp);
            myRs.last();
            String[][] returner = new String[myRs.getRow()][columns.length];
            myRs.beforeFirst();
            int row = 0;
            while (myRs.next()){
                int length = columns.length, col = 0;

                while (length != 0) {
                    try {
                        returner[row][col] = myRs.getString(columns[col]);
                    } catch (IllegalArgumentException e1) {
                        try {
                            returner[row][col] = Integer.toString(myRs.getInt(columns[col]));
                        } catch (IllegalArgumentException e2) {
                            try {
                                returner[row][col] = Double.toString(myRs.getDouble(columns[col]));
                            } catch (IllegalArgumentException e3) {
                                try {
                                    Date date = myRs.getDate(columns[col]);
                                    DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");  
                                    returner[row][col] = dateFormat.format(date); 
                                } catch (IllegalArgumentException e4) {
                                    System.err.println("Could not retrieve data");
                                } catch (Exception e) {
                                    System.err.println(e);
                                }
                            }
                        }
                    }
                    col += 1;
                    length -= 1;
                }
                row += 1;
            }

            return returner;
        } catch (SQLException exSQL) {
            System.err.println(exSQL);
        }
        return null;
    };

我希望输出具有整个表中的数据,但只有选定的列。

3 个答案:

答案 0 :(得分:0)

我相信您的问题是,condition是大多数SQL语言中的保留工作,如https://dev.mysql.com/doc/refman/5.5/en/keywords.html#keywords-5-5-detailed-C中所述,这就是为什么您的代码在某些情况下可以工作的原因,是因为您并不总是拥有名为condition的列,则需要使用反引号对其进行转义。

答案 1 :(得分:0)

您是否尝试输出返回的查询?我认为这对您的查询生成器有问题。只需 System.out.println(temp)并在查询MySQL之前检查查询语法。

答案 2 :(得分:0)

“条件”是MySQL中的保留字。您可以通过在选择的列表项两边加上引号来避免此错误:

for (int i = 0; i < columns.length; i++) {
   String column = "\"" + colunms[i] + "\"";
   if (i < columns.length - 1) {
        temp = temp + column + ", ";
    } else {
        temp = temp + column;
    }
}