首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PreparedStatement setString不工作,仍有问号

PreparedStatement setString不工作,仍有问号
EN

Stack Overflow用户
提问于 2018-03-29 14:31:36
回答 2查看 1.2K关注 0票数 0

我目前正在做一个有数据库在后面的项目,我想用这种方法按列排序文件:由于这个原因,方法头中有4个不同的参数,第一个参数是连接,下一个参数是用户名,因为只有上传文件的人才能看到文件,下一个是数据库表的列,下一个是ASC或DESC。

代码语言:javascript
复制
public ArrayList<Daten> meineDaten(Connection conn,String sortierparameter,String spalte,String reihung)
    {
        //generieren einer ArrayList zum Zwischenspeichern von den Werten aus der Datenbank
        ArrayList<Daten> DatenSortiertPrivate = new ArrayList<>();
        String READ_DATEN_PRIVATE = null;

        //SQL-Abfrage
        if(reihung.equals("ASC"))
        {
            READ_DATEN_PRIVATE="select uploadid,dateityp, dateiname, autor, uploaddatum, dokumentdatum, status from uploaddaten where uploader= ? and zustand='true' order by ? ASC;";
        }
        else if(reihung.equals("DESC")){
            READ_DATEN_PRIVATE="select uploadid,dateityp, dateiname, autor, uploaddatum, dokumentdatum, status from uploaddaten where uploader= ? and zustand='true' order by ? DESC;";
        }

        //READ_DATEN_PRIVATE="select uploadid,dateityp, dateiname, autor, uploaddatum, dokumentdatum, status from uploaddaten where uploader=? and zustand='true' order by ? ?;";

        try {
            pstmt = conn.prepareStatement(READ_DATEN_PRIVATE);
            pstmt.setString(1, sortierparameter);
            pstmt.setString(2, spalte);
            rs = pstmt.executeQuery();
            System.out.println("SQL: "+READ_DATEN_PRIVATE);
            while(rs.next())
            {
                int uploadid = rs.getInt(1);
                String dateityp = rs.getString(2);
                String dateiname = rs.getString(3);
                String autor = rs.getString(4);
                String uploaddatum = rs.getString(5);
                String dokumentdatum = rs.getString(6);
                String status = rs.getString(7);

                Daten zeile = new Daten(uploadid,dateityp,dateiname, autor, uploaddatum, dokumentdatum, status);
                DatenSortiertPrivate.add(zeile);
            }

            pstmt.close(); pstmt=null;
            rs.close();rs=null;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return DatenSortiertPrivate;

    }

我不知道为什么结果是这样: SQL Daten auf网站angebenselect uploadid,dateityp,dateiname,autor,uploaddatum,dokumentdatum,status from uploaddaten where uploader=?and zustand='true‘order by?ASC;

例如,按"dateiname“排序,用户名为thoker和ASC。

此方法将通过单击按钮来使用。

附言:为我糟糕的英语道歉

EN

回答 2

Stack Overflow用户

发布于 2018-03-29 14:47:57

您正在打印READ_DATEN_PRIVATE。在prepareStatement之后打印pstmt,然后可以检查更新的查询

代码语言:javascript
复制
 System.out.println("SQL Daten auf Website angeben Before"+READ_DATEN_PRIVATE);
        try {
            pstmt = conn.prepareStatement(READ_DATEN_PRIVATE);
            pstmt.setString(1, sortierparameter);
            pstmt.setString(2, spalte);
            rs = pstmt.executeQuery();
            System.out.println("After Change:" + pstmt);
票数 1
EN

Stack Overflow用户

发布于 2018-04-14 18:30:50

详细答案简短:

您不能在这两个查询中都替换order by ?中的值。

原因:

占位符?可以应用于列的参数,但不能应用于not to

  1. 列或表名、
  2. 排序列或排序顺序方向(视情况而定)或
  3. SQL functions/clauses.

因此,要解决主要问题,请使用"... order by " + sortierparameter + "...交换order by ?。但是,应该仔细检查此值,以避免在运行时出现错误。通过枚举更好地定义允许的排序顺序参数。

有关参考,请参阅Using Prepared Statements上的Oracle教程。

旁注

您应该遵守代码中参数占位符的顺序:

代码语言:javascript
复制
pstmt.setString(1, sortierparameter);
pstmt.setString(2, spalte);

考虑到语义(以及“sortierparameter”的德语翻译),这是错误的。您错误地将pstmt.setString(2, spalte);设置为第二个参数。

假设你想用它设置uploader= ?,并且修复了前面提到的方法的主要问题,我认为它必须像下面这样读。

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49549429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档