Mysql 为SQL PreparedStatement动态添加列-仍然可以防止注入?

Mysql 为SQL PreparedStatement动态添加列-仍然可以防止注入?,mysql,jdbc,prepared-statement,sql-injection,Mysql,Jdbc,Prepared Statement,Sql Injection,我试图阻止Java程序中的SQL注入。我想使用PreparedStatements来实现这一点,但我事先不知道列的数量或它们的名称(该程序允许管理员从表中添加和删除列)。我是新手,所以这可能是一个愚蠢的问题,但我想知道这种方法是否安全: public static int executeInsert( String table, Vector<String> values) { Connection con; try { con = connect()

我试图阻止Java程序中的SQL注入。我想使用PreparedStatements来实现这一点,但我事先不知道列的数量或它们的名称(该程序允许管理员从表中添加和删除列)。我是新手,所以这可能是一个愚蠢的问题,但我想知道这种方法是否安全:

public static int executeInsert( String table, Vector<String> values)
{
    Connection con;
    try {
        con = connect();

        // Construct INSERT statement
        int numCols = values.size();
        String selectStatement = "INSERT INTO " + table + " VALUES  (?";
        for (int i=1; i<numCols; i++) {
            selectStatement += ", ?";
        }
        selectStatement += ")";     
        PreparedStatement prepStmt = con.prepareStatement(selectStatement);

        // Set the parameters for the statement
        for (int j=0; j<numCols; j++) {
            prepStmt.setString(j, values.get(j));
        }

        System.out.println( "SQL: " + prepStmt) ;
        int result = prepStmt.executeUpdate();
        con.close() ;
        return( result) ;
    } catch (SQLException e) {
        System.err.println( "SQL EXCEPTION" ) ;
        System.err.println( "Inserting values " + values + " into " + table);
        e.printStackTrace();
    }
    return -1;
}
publicstaticintexecuteinsert(字符串表,向量值)
{
连接con;
试一试{
con=connect();
//构造插入语句
int numCols=values.size();
String selectStatement=“插入“+表+”值(?”;

对于(int i=1;i任何时候,如果在查询中插入了
table
之类的值而没有转义,则应针对已知良好值的白名单进行测试。这可以防止人们的创造性和造成麻烦。一个简单的字典或有效条目数组通常就足够了

使用准备好的语句是一个好主意,但要确保您准备的语句从一开始就不允许注入