0

我需要从另一个类中读取“公共静态 NEW_QUERY”,但它在查询中具有在调用该方法时更改的变量。我怎样才能调用这个“公共静态”并从另一个类获取更新的 QUERY。

这是Java代码;

package artemispm.autocalc;

import java.sql.*;
import java.util.*;
import a7.unittests.dao.UnitTestHelper;
import artemispm.serverutil.*;
import artemispm.trdo.*;
import artemispm.parser.*;

public abstract class TRBaseScoreCalculator implements ExpressionParserLookup {
                           
/******THIS IS THE PLACE I NEED TO HAVE CORRECTED**********/
/*HOW DO I FORCE THIS TO PUSH VALUES INTO 'match' and 'useFor'*/
/**SO THAT I CAN CALL NEW_QUERY FROM ANOTHER CLASS *****/
/****IT SAYS 'match' and 'useFor' cannot be resolved****/

public static String NEW_QUERY = "select count(userfieldid) from tr_userfield where calcexpression like '%" + match + "%'and usefor like '%" + useFor +"%'";

public Connection               m_con;
protected String                m_characName        = "";

public boolean isThisCharacInUse(Connection con, String characName,String useFor) 
throws SQLException, TRException {
    boolean result;        
    String match = "[" + TRBaseSql.rewrapQuotes(characName) + "]";
  
    if(TRBaseSql.getDatabaseType(con) == TRBaseSql.DBTYPESQLSERVER) {
        match = "[[]" + TRBaseSql.rewrapQuotes(characName) + "]";
    }
    
    TRSystemSQL sql = new TRSystemSQL();
    TRSystem sys=sql.getSystem(con);
    result =    (   sys.getScoreCalculation() != null 
                &&  sys.getScoreCalculation().indexOf(match) >= 0  );
    if (result) return(result);
    else {
        
        int count;

        /*****THIS IS THE PLACE I NEED HELP AT  12/5/2012 ******/

        count = sql.executeGetInt(con, NEW_QUERY , null);
        
        if (count > 0) return true;
        count = sql.executeGetInt(con, 
                "select count(characid) from tr_charac where calcexpression like '%" + match + "%'and usefor like '%" + useFor +"%'" , null);
        return (count > 0);
        }
}
}

除此之外,我在这里没有尝试过,但我应该按照规范中的内容:

  • 找到所有嵌入式 sql 语句并为尚未使用常量的嵌入式 sql 实现常量。

  • 用 String.format() 替换 Sql 语句创建中的任何字符串连接。

  • 对于任何 String.format 命令,您必须转义任何现有的 % 符号。这些符号经常用于 LIKE 操作。

    我正在做的是从另一个文件对每个查询进行单元测试,以确保它们对于从 MSSQL Server 2005 到 MYSQL 的数据迁移是正确的

给出了 STRING.FORMAT 规范,下面是最好的解决方案。它是否正确。测试通过了,但我认为我应该尽可能地更改原始代码

package artemispm.autocalc;

import java.sql.*;
import java.util.*;
import a7.unittests.dao.UnitTestHelper;
import artemispm.serverutil.*;
import artemispm.trdo.*;
import artemispm.parser.*;

public abstract class TRBaseScoreCalculator implements ExpressionParserLookup {
                           
/******THIS IS THE PLACE I NEED TO HAVE CORRECTED**********/
/*HOW DO I FORCE THIS TO PUSH VALUES INTO 'match' and 'useFor'*/
/**SO THAT I CAN CALL NEW_QUERY FROM ANOTHER CLASS *****/
/****IT SAYS 'match' and 'useFor' cannot be resolved****/

public static String NEW_QUERY = String.format( "select count(userfieldid) from tr_userfield where calcexpression like %s and usefor like %s", "?", "?");

public Connection               m_con;
protected String                m_characName        = "";

public boolean isThisCharacInUse(Connection con, String characName,String useFor) 
throws SQLException, TRException {
    boolean result;        
    String match = "[" + TRBaseSql.rewrapQuotes(characName) + "]";
  
    if(TRBaseSql.getDatabaseType(con) == TRBaseSql.DBTYPESQLSERVER) {
        match = "[[]" + TRBaseSql.rewrapQuotes(characName) + "]";
    }
    
    TRSystemSQL sql = new TRSystemSQL();
    TRSystem sys=sql.getSystem(con);
    result =    (   sys.getScoreCalculation() != null 
                &&  sys.getScoreCalculation().indexOf(match) >= 0  );
    if (result) return(result);
    else {
        
        int count;

        /*****THIS IS THE PLACE I NEED HELP AT  12/5/2012 ******/

        PreparedStatement stmt = con.prepareStatement(TRBaseScoreCalculator.NEW_QUERY);
        stmt.setString(1, "%"+match+"%");
        stmt.setString(2, "%"+useFor+"%");
        
        count = sql.executeGetInt(con, stmt.toString() , null);
        
        if (count > 0) return true;
        count = sql.executeGetInt(con, 
                "select count(characid) from tr_charac where calcexpression like '%" + match + "%'and usefor like '%" + useFor +"%'" , null);
        return (count > 0);
        }
}
}
4

2 回答 2

3

静态变量/方法与 lass 定义相关联,因此您不能在其中使用任何非静态变量/方法。

如果您的matchanduseFor不是静态的(就是这种情况),那么您不能有这样的静态 qquery:

public static String NEW_QUERY = "select count(userfieldid) from tr_userfield where calcexpression like '%" + match + "%' and usefor like '%" + useFor +"%'";

最好在查询中使用占位符:

 public static String NEW_QUERY = 
           "select count(userfieldid) from tr_userfield "+
            " where calcexpression like ? and usefor like ?";

以及使用的地方,使用查询参数设置方法传递值:

   PreparedStatement stmt= con.prepareStatement(TRBaseScoreCalculator.NEW_QUERY);
   stmt.setString(1,"%"+match+"%");
   stmt.setString(1,"%"+useFor+"%");

编辑:如果您想使用,请String.format尝试以下操作:

 public static String NEW_QUERY =  "select count(userfieldid) from tr_userfield "+
                           "  where calcexpression like '%s' and usefor like '%s'";

在您使用查询字符串的那一行,执行以下操作:

 String updatedQuery = String.format(TRBaseScoreCalculator.NEW_QUERY, 
                                      "%"+match+"%", "%"+useFor+"%");
 count = sql.executeGetInt(con, updatedQuery, null);

但我仍然更喜欢这PraparedStatement条路线,在这种情况下你不需要不必要String.format的。

于 2012-12-05T21:08:21.883 回答
1

如果您使用PreparedStatements,您不仅可以避免从静态上下文引用非静态内容的问题,而且您的应用程序也不会那么容易受到损害

于 2012-12-05T21:12:04.307 回答