Java 在准备好的语句中获取列名而不是值

Java 在准备好的语句中获取列名而不是值,java,sql,swing,jdbc,Java,Sql,Swing,Jdbc,在准备好的语句中,我得到的是列名而不是值 以下是我的代码: string q="select ? from EMPLOYEE where salary > ?"; Preparedstatement pst = connectionobject.preparedstatement(q); pst.setstring(1, "FIRST_NAME"); pst.setint(2, 10000); 当我在JTable中打印结果时,它会在所有行中显示FIRST\u NAME。您准备的语句必须

在准备好的语句中,我得到的是列名而不是值

以下是我的代码:

string q="select ? from EMPLOYEE where salary > ?";

Preparedstatement pst = connectionobject.preparedstatement(q);
pst.setstring(1, "FIRST_NAME");
pst.setint(2, 10000);

当我在
JTable
中打印结果时,它会在所有行中显示
FIRST\u NAME

您准备的语句必须生成查询:
从工资>10000的员工中选择“FIRST\u NAME”,而不是
从工资>10000的员工中选择FIRST\u NAME

因此,它为每一行返回字符串“FIRST_NAME”


您可以简单地使用StringBuilder将第一个“?”替换为您的列名。

您的preparedStatement必须生成以下查询:
从salary>10000的员工中选择“first\u name”,而不是
从salary>10000的员工中选择first\u name

因此,它为每一行返回字符串“FIRST_NAME”


您可以简单地使用StringBuilder将第一个“?”替换为您的列名。

这是不可能的,以您的方式解决您的问题

按如下方式更改代码:

String att = "FIRST_NAME";

string q="select " + att + " from EMPLOYEE where salary>?";

Preparedstatement pst=connectionobject.preparedstatement(q);
pst.setint(1,10000); 
如果您担心用户可以进行SQL注入,请使用此解决方案 您应该检查表中是否存在列:

SELECT * 
    FROM information_schema.COLUMNS 
    WHERE 
        TABLE_SCHEMA = 'db_name' 
    AND TABLE_NAME = 'table_name' 
    AND COLUMN_NAME = 'column_name'
像这样:

public boolean column_exist(String att) {
    boolean succes = false;
    CreerConnection con = new CreerConnection();
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultat = null;
    try {
        connection = con.getConnection();

        statement = connection.prepareStatement("SELECT * \n"
                + "FROM information_schema.COLUMNS "
                + " WHERE"
                + " TABLE_SCHEMA = 'db_name'"
                + " AND TABLE_NAME = 'table_name'"
                + " AND COLUMN_NAME = ?");
        statement.setString(1, att);
        resultat = statement.executeQuery();

        if (resultat.next()) {
            succes = true;
        }

    } catch (SQLException e) {
        System.out.println("Exception = " + e);
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException ex) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ex) {
            }
        }
    }
    return succes;
}
如果列存在,则可以继续这样操作:

if(column_exist(att)){
   string q="select " + att + " from EMPLOYEE where salary>?";

   Preparedstatement pst=connectionobject.preparedstatement(q);
   pst.setint(1,10000); 
}
在此处了解更多信息:


希望这能对你有所帮助。

这是不可能的,用你的方式来解决你的问题吧

按如下方式更改代码:

String att = "FIRST_NAME";

string q="select " + att + " from EMPLOYEE where salary>?";

Preparedstatement pst=connectionobject.preparedstatement(q);
pst.setint(1,10000); 
如果您担心用户可以进行SQL注入,请使用此解决方案 您应该检查表中是否存在列:

SELECT * 
    FROM information_schema.COLUMNS 
    WHERE 
        TABLE_SCHEMA = 'db_name' 
    AND TABLE_NAME = 'table_name' 
    AND COLUMN_NAME = 'column_name'
像这样:

public boolean column_exist(String att) {
    boolean succes = false;
    CreerConnection con = new CreerConnection();
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultat = null;
    try {
        connection = con.getConnection();

        statement = connection.prepareStatement("SELECT * \n"
                + "FROM information_schema.COLUMNS "
                + " WHERE"
                + " TABLE_SCHEMA = 'db_name'"
                + " AND TABLE_NAME = 'table_name'"
                + " AND COLUMN_NAME = ?");
        statement.setString(1, att);
        resultat = statement.executeQuery();

        if (resultat.next()) {
            succes = true;
        }

    } catch (SQLException e) {
        System.out.println("Exception = " + e);
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException ex) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ex) {
            }
        }
    }
    return succes;
}
如果列存在,则可以继续这样操作:

if(column_exist(att)){
   string q="select " + att + " from EMPLOYEE where salary>?";

   Preparedstatement pst=connectionobject.preparedstatement(q);
   pst.setint(1,10000); 
}
在此处了解更多信息:



希望这能对您有所帮助。

是的,这就是您的代码所做的
从中选择“FIRST_NAME”…
这样您的列
FIRST_NAME
是大写的,而薪水不是?您能否对表PLSY进行描述您不能使用
PreparedStatement
构建动态查询,查询本身必须是固定的,并且查询变量的参数必须是固定的。我建议编写一个DAO层,其中包含对
EMPLOYEE
表中每个属性的查询。这可以在
Hibernate
中使用
setParameter
完成。是的,这就是您的代码所做的
从….
中选择“FIRST_NAME”,那么您的列
FIRST_NAME
是大写的,而工资不是大写的?您能否对表PLSY进行描述您不能使用
PreparedStatement
构建动态查询,查询本身必须是固定的,并且查询变量的参数必须是固定的。我建议编写一个DAO层,从
EMPLOYEE
表中查询您想要的每个属性。这可以在
Hibernate
中使用
setParameter
完成。如果列不退出,这会对SQL注入造成问题吗?@YCF\u L:是的,我个人不会参数化select以避免SQL注入,然后选择我需要的每一列。但是我认为这是完成这个基本的PreparedStatement的唯一方法。所以这种方法也可以是true
String att=“FIRST\u NAME”;字符串q=“从薪资>的员工中选择“+att+”;Preparedstatement pst=连接对象Preparedstatement(q);pst.setint(110000)它可以工作,但效率不高。虽然您可以编写
选择FIRSTNAME,其中…
看起来您想要动态显示JTable,但您正在执行2个串联。然后,您可以始终选择所有列并在Java代码中动态显示。如果该列不退出,这会导致SQL注入出现问题吗?@YCF_L:是的,我个人不会参数化select以避免SQL注入,并选择我需要的每一列。但是我认为这是完成这个基本的PreparedStatement的唯一方法。所以这种方法也可以是true
String att=“FIRST\u NAME”;字符串q=“从薪资>的员工中选择“+att+”;Preparedstatement pst=连接对象Preparedstatement(q);pst.setint(110000)它可以工作,但效率不高。虽然您可以编写
选择FIRSTNAME,其中…
看起来您想要动态显示JTable,但您正在执行2个串联。然后,您可以始终选择所有列并在Java代码中动态显示。