【问题标题】:PreparedStatement setString doesn't work, there are still question marksPreparedStatement setString 不起作用,还有问号
【发布时间】:2018-03-29 06:31:36
【问题描述】:

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

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=?和 zustand='true' 的顺序是?升序;

例如按“dateiname”订购,用户名是 thoker 和 ASC。

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

附:对不起我的英语不好

【问题讨论】:

  • 您正在打印变量 READ_DATEN_PRIVATE 的内容 - 为什么您认为这会改变?
  • 对您的问题的简短回答“如何获取插入字符串的值的 SQL 语句?”是“你不能”。为什么需要这样做?

标签: sql postgresql jdbc prepared-statement setstring


【解决方案1】:

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

 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);

【讨论】:

  • SQL Daten auf 网站 angebenselect uploadid,dateityp, dateiname, autor, uploaddatum, dokumentdatum, status from uploaddaten where uploader= ?和 zustand='true' 的顺序是?升序;之后:选择uploadid、dateityp、dateiname、autor、uploaddatum、dokumentdatum、status from uploaddaten where uploader= ?和 zustand='true' 的顺序是?升序;我按你说的试过了,还是不行
  • PreparedStatement或者setString方法有什么问题
【解决方案2】:

长答案短:

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

原因:

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

  1. 列名或表名,
  2. 排序列或排序方向(您的情况)或到
  3. SQL 函数/子句。

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

请参阅 Using Prepared Statements 上的 Oracle 教程以供参考。

旁注

您应该尊重代码中参数占位符的顺序:

pstmt.setString(1, sortierparameter);
pstmt.setString(2, spalte);

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

我认为它必须如下pstmt.setString(1, spalte); 假设您想用它设置uploader= ?,并且您解决了上述方法的主要问题。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 2013-09-27
    • 1970-01-01
    相关资源
    最近更新 更多