微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JDBC PreparedStatement导致MySQL语法错误

我收到错误消息“您的sql语法有错误;请检查与您的MySQL服务器版本相对应的手册,以在第1行的“ orderr”附近使用正确的语法”,因此我认为错误是已经使用了两个’,但是在我的代码中我没有使用任何’.注意,该表实际上被命名为订购者.

public void insertIntoDatabase(String table, Object... entries) {       // take a table and 
    Connection con = connect();                                         //add entries
    PreparedStatement preparedStatement = null;
    PreparedStatement preparedStatement2 = null;
    ResultSet rs = null;

    StringBuffer columnNames = new StringBuffer();
    StringBuffer sbEntries = new StringBuffer();
    for (int i = 0; i < entries.length; i++) {
        if (entries[i] instanceof Integer)
            sbEntries.append((Integer) entries[i]); 
        else if (entries[i] instanceof String)      
            sbEntries.append((String) entries[i]); 

        if (i != entries.length - 1)//if not last entry add 
            sbEntries.append(" ,"); // a ' ,'.
    }
    try {
        preparedStatement = con.prepareStatement("select * from ? ;");
        preparedStatement.setString(1, table);
        preparedStatement2 = con
                .prepareStatement("Insert into ?( ? ) values ( ? );");
        ResultSet resultSet = preparedStatement.executeQuery(); // get the
                                                                // number of
                                                                // columns
        ResultSetMetaData rsmd; // for the table
        rsmd = resultSet.getMetaData();
        int columnCount = rsmd.getColumnCount();
        for (int i = 1; i < columnCount + 1; i++) { // get column names, add to 
            columnNames.append(rsmd.getColumnName(i)); // to sb
            if (i != columnCount)
                columnNames.append(" ,");
        }
        columnCount = rsmd.getColumnCount();
        preparedStatement2.setString(1, table);                     
        preparedStatement2.setString(2, columnNames.toString());    //add sb's to statement
        preparedStatement2.setString(3, sbEntries.toString());
        preparedStatement2.executeUpdate();

    } catch (sqlException e) {
        System.out.println("2" + e.getMessage());
    }
    finally{
        try {
            if (rs != null) {
                rs.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if(preparedStatement2 != null){
                preparedStatement2.close();
            }
            if (con != null) {
                con.close();
            }

        } catch (sqlException e) {
            System.out.print("3" +e.getMessage());
        }

    }

}

解决方法:

在大多数数据库中,您无法参数化表名称之类的对象名称,在MysqL中,理论上您可以做到,因为认情况下MysqL Connector / J不使用服务器端参数,而是在将查询发送到服务器之前重写查询.但是,该值将作为带引号的字符串插入,并且对象名称不能是带引号的字符串,因此仍然无法使用.

如此插入?还是SELECT … FROM?将不起作用,因为它会产生“插入到’theTable’或SELECT … FROM’theTable’的信息.

对象名称必须是实际查询的一部分.不要为它们使用参数.大多数其他数据库(或其驱动程序)在此位置具有参数都会引发异常.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐