0

该作业的规格如下:

  • 找到所有嵌入式 sql 语句并为尚未使用常量的嵌入式 sql 实现常量。
  • 用 String.format() 替换 Sql 语句创建中的任何字符串连接。
  • 对于任何 String.format 命令,您必须转义任何现有的 % 符号。这些符号经常用于 LIKE 操作。

如何将此 SQL 查询移动到“公共静态字符串 MY_QUERY”中,这样​​它就不会被前向引用,并且可以解析“匹配”和“使用For”变量。我将对 MY_QUERY 执行的操作是从 JUnit 测试访问它,然后针对源数据库和目标数据库执行查询,以确保查询在两个数据库上执行数据迁移所需的查询。

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 NEW QUERY THAT WILL BE ACCESSED FROM JUNIT TEST****/

public static String MY_QUERY = 

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 ******/
/*I NEED TO SOMEHOW MOVE THE BELOW STATEMENT USING THE SPECS INTO MY_QUERY**/
/**"select count(userfieldid) from tr_userfield where calcexpression like '%" + match + "%'and usefor like '%" + useFor +"%'"***/

    count = sql.executeGetInt(con, "select count(userfieldid) from tr_userfield where calcexpression like '%" + match + "%'and usefor like '%" + useFor +"%'" , 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

1 回答 1

1

您可以作弊并执行以下操作:

持续的:

private static final String FORMAT = "select count(userfieldid) " + 
    "from tr_userfield " + 
    "where calcexpression like '%c%s%c' and usefor like '%c%s%c'";

private static final char WILDCARD = '%';

创建查询:

String calcExpression = "a";
String useFor = "b";

String query = String.format(FORMAT, WILDCARD, calcExpression, WILDCARD, 
    WILDCARD, useFor, WILDCARD);
System.out.println(query);

这给了你:

select count(userfieldid) from tr_userfield where calcexpression like '%a%' and usefor like '%b%'

于 2012-12-06T05:37:37.420 回答