[英]How do I read in a public static, with variable data from a method, from another class and get updated data
I need to read in the 'public static NEW_QUERY' from another class, but it has variables inside of the query that is changed when the method is called. 我需要从另一个类中读取“公共静态NEW_QUERY”,但是它在查询内部具有变量,该变量在调用该方法时会更改。 How can I call this 'public static' and get the updated QUERY from another class.
我如何称其为“公共静态”并从另一个类中获取更新的QUERY。
Here is the Java Code; 这是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);
}
}
}
In addition to this, which I have not attempted here, but I am supposed to as it is in the specs: 除此之外,我在这里还没有尝试过,但是我应该按照规格说明进行操作:
For any String.format commands you must escape out any existing % symbols. 对于任何String.format命令,您都必须转义任何现有的%符号。 These symbols are often used in LIKE operations.
这些符号通常在LIKE操作中使用。
What I am doing is Unit Testing each Query, from another file, to make sure they are correct for data migration from a MSSQL Server 2005 to MYSQL 我正在做的是对另一个文件中的每个查询进行单元测试,以确保它们正确地将数据从MSSQL Server 2005迁移到MYSQL
IS THE BELOW THE BEST SOLUTION GIVEN THE STRING.FORMAT SPEC. 是STRING.FORMAT SPEC提供的最佳解决方案。 IS THIS CORRECT.
这个对吗。 THE TEST DOES PASS, BUT I THOUGHT I WAS SUPPOSED TO CHANGE AS LITTLE OF THE ORIGINAL CODE AS POSSIBLE
测试通过了,但是我想应该尽可能更改原始代码
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);
}
}
}
Static variables/methods are associated with lass definition and hence you can't use any non-static variable/method in them. 静态变量/方法与lass定义相关联,因此您不能在其中使用任何非静态变量/方法。
if your match
and useFor
are not static(that is the case) then you can't have a static qquery like this: 如果您的
match
和useFor
不是静态的(就是这种情况),那么您就不能有这样的静态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 =“从tr_userfield中选择count(userfieldid),其中calcexpression如'%'+ match +”%',而usefor如'%“ + useFor +”%'“;
Better to use a place holders in the query as: 最好在查询中使用占位符,如下所示:
public static String NEW_QUERY =
"select count(userfieldid) from tr_userfield "+
" where calcexpression like ? and usefor like ?";
and the place of use, pass the value using query Parameter set methods: 以及使用地点,请使用查询参数集方法传递值:
PreparedStatement stmt= con.prepareStatement(TRBaseScoreCalculator.NEW_QUERY);
stmt.setString(1,"%"+match+"%");
stmt.setString(1,"%"+useFor+"%");
EDIT : If you want to use String.format
then try below: 编辑 :如果要使用
String.format
尝试以下操作:
public static String NEW_QUERY = "select count(userfieldid) from tr_userfield "+
" where calcexpression like '%s' and usefor like '%s'";
and down the line where you are using the query string, do: 在您使用查询字符串的那一行下,执行以下操作:
String updatedQuery = String.format(TRBaseScoreCalculator.NEW_QUERY,
"%"+match+"%", "%"+useFor+"%");
count = sql.executeGetInt(con, updatedQuery, null);
But Still I prefer the PraparedStatement
route and in that case you don't need to unnecessary String.format
. 但是我仍然更喜欢
PraparedStatement
路由,在这种情况下,您不需要不必要的String.format
。
如果您使用PreparedStatement
,则不仅会避免从静态上下文引用非静态内容的问题,而且,您的应用程序不会轻易受到损害 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.