无法转换为内部表示JDBC

时间:2013-11-27 13:46:27

标签: java sql jdbc

好的,这是我的代码

public static ArrayList getMaterialerFraOrdreNr(String s_date, String e_date) throws SQLException, InterruptedException {
    int tal = 0;

    ArrayList nameOfColumns = getNameOfColumns();                     // name of columns
    ArrayList orderNumber = getOrdre_Nr_FromDB(s_date, e_date);           // order number 

    //første loop kører gennem number of columns
    //anden loop kører gennem name of column
    ResultSet rs = null;
    Connection con = null;

    try {
        Class.forName(DB.driver);
        con = DriverManager.getConnection(DB.URL, DB.ID, DB.PW);

        for (int i = 1; i < orderNumber.size(); i++) {
            for (int j = 1; j < nameOfColumns.size(); j++) {

                String nameOfColum = (String) nameOfColumns.get(i);
                int orderNr = (Integer) orderNumber.get(j);
                System.out.println("orderNr  " + orderNr);
                //SELECT v1001 FROM ORDRE_spec WHERE  ordre_nr = 1;
                String query = "SELECT ? AS ans FROM ordre_spec WHERE ordre_nr = ?";
                PreparedStatement prest = con.prepareStatement(query);

                prest.setString(1, nameOfColum);
                prest.setInt(2, orderNr);
                System.out.println("orderNr  "  + orderNr);
                System.out.println("nameOfColum  =   " + nameOfColum);
                rs = prest.executeQuery();
                if(rs.next()){


                    tal = rs.getInt("ans");

                    MaterialeNum.add(tal);
                    System.out.println("materiale num =    " + MaterialeNum);

                }
            }

        }

    } catch (ClassNotFoundException | SQLException ee) {
        System.out.println("fail og der er så her");
        System.err.println(ee);
    } finally {

        con.close();
    }
    System.out.println(kundeNum.toString());
    return kundeNum;

}

public static void main(String[] args) throws SQLException, InterruptedException {

    NewClass.getMaterialerFraOrdreNr("1990-10-10", "2020-10-10");

}

我的问题是我得到了一个java.sql.SQLException:无法转换为内部表示

我真的无法看到错误应该是什么..如果你能看到错误,请帮助PLZ:)

1 个答案:

答案 0 :(得分:5)

String query = "SELECT ? AS ans FROM ordre_spec WHERE ordre_nr = ?";

您无法参数化列名称。您只能参数化列值。

基本上你需要这样做:

String query = "SELECT " + nameOfColum + " AS ans FROM ordre_spec WHERE ordre_nr = ?";

请记住,如果nameOfColum可由最终用户控制,则很容易引入SQL。如果确实如此,您可能希望在例如字符串上执行字符串匹配。在继续之前\w+