简体   繁体   English

当查询在php myadmin中工作时,JDBC抛出语法错误

[英]JDBC is throwing grammar error when the query is working in php myadmin

I am working on java website using jdbc for mysql. 我正在使用jdbc for mysql在Java网站上工作。 Here is my simple function to fetch data from sql, 这是我从sql获取数据的简单函数,

@Override
    public List fetchMusic(String _uname, String _chnl_name) throws SQLException {

        String sql = "SELECT * FROM `channel_songs` WHERE `uid` = " +
                     " (SELECT `id` FROM `user` WHERE BINARY `uname` = ?)"+
                     " AND `chnl_id` = (SELECT `id` FROM `channel` WHERE BINARY `chnl_name` = ?);";

        Connection conn = dataSource.getConnection();

        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setString(1, _uname);
        ps.setString(2, _chnl_name);

        List<Map<String, Object>> musicListsByChnl = jdbcTemplate.queryForList(String.valueOf(ps));


        return musicListsByChnl;

    }

So, when I am running this, I am getting an error which says, 因此,当我运行此程序时,出现错误消息,

org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [com.mysql.jdbc.JDBC4PreparedStatement@3a4eb50a: SELECT * FROM `channel_songs`]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'com.mysql.jdbc.JDBC4PreparedStatement@3a4eb50a: SELECT * FROM `channel_songs`' at line 1

So, I don't know what is wrong or where it is. 因此,我不知道哪里出了问题或问题在哪里。 When I am trying this query on phpmyadmin, it is working properly and returning the data. 当我在phpmyadmin上尝试此查询时,它正在正常工作并返回数据。 So can anyone help me here? 那么有人可以在这里帮助我吗?

Thanx in advance. 提前感谢。 ;) ;)

You are using the prepared statement wrong... The PreparedStatement is not simply a "convenient" device to build fully escaped SQL strings. 您使用的预处理语句错误... PreparedStatement不仅仅是构建完全转义的SQL字符串的“便捷”设备。 The result of String.valueOf(ps) is not actually defined to be a valid SQL command at all. 实际上根本没有将String.valueOf(ps)的结果定义为有效的SQL命令。 You get ps.toString() , whose value should be used for debugging only. 您将获得ps.toString() ,其值应仅用于调试。 From the exception you gave: 根据您给的例外:

check the manual that corresponds to your MySQL server version for the right syntax to use near 'com.mysql.jdbc.JDBC4PreparedStatement@3a4eb50a: SELECT * FROM channel_songs ' at line 1 检查与您的MySQL服务器版本相对应的手册,以在'com.mysql.jdbc.JDBC4PreparedStatement@3a4eb50a: SELECT * FROM channel_songs '的第1行附近使用正确的语法

You are supposed to do something like this: 您应该做这样的事情:

@Override
public List fetchMusic(String _uname, String _chnl_name) throws SQLException {

    String sql = "SELECT * FROM `channel_songs` WHERE `uid` = " +
                 " (SELECT `id` FROM `user` WHERE BINARY `uname` = ?)"+
                 " AND `chnl_id` = (SELECT `id` FROM `channel` WHERE BINARY `chnl_name` = ?);";

    Connection conn = dataSource.getConnection();

    PreparedStatement ps = conn.prepareStatement(sql);

    ps.setString(1, _uname);
    ps.setString(2, _chnl_name);

    // This is, where your original code falls apart....
    // List<Map<String, Object>> musicListsByChnl = jdbcTemplate.queryForList(String.valueOf(ps));


    List<Map<String,Object>> answer = new ArrayList<>();

    try (ResultSet rs = ps.executeQuery()) {

        while (rs.next()) {

            // read a single row of the result set using the
            // getters of the `rs` instance (rs.getString(...),
            // rs.getInt(...), ...)
        }
    }



    return musicListsByChnl;

}

If you do not want to write the boiler plate stuff (and as you already seem to have a jdbcTemplate at hand): spring's JdbcTemplate has a lot more convenience methods, which deal with assigning query parameters and the like. 如果您不想编写样板文件(并且您似乎已经手头有一个jdbcTemplate ):spring的JdbcTemplate具有许多方便的方法,这些方法可以处理分配查询参数等问题。 Just find the one closest to your needs. 只要找到最接近您需求的那一个即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM