简体   繁体   English

如何从一个方法,另一个类的变量数据中读取公共静态变量并获取更新的数据

[英]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: 除此之外,我在这里还没有尝试过,但是我应该按照规格说明进行操作:

  • Locate all embedded sql statements and implement constants for embedded sql which does not already use constants. 找到所有嵌入式sql语句,并为尚未使用常量的嵌入式sql实现常量。
  • Replace any string concatenation within Sql statement creation with String.format(). 用String.format()替换Sql语句创建过程中的任何字符串连接。
  • 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: 如果您的matchuseFor不是静态的(就是这种情况),那么您就不能有这样的静态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.

相关问题 如何从公共static void main到另一个类调用变量? - How do I call a variable from the public static void main to another class? 如何在另一个类中使用静态类中的变量? - How do I use a variable from a static class in another class? Java-如何将通过公共静态void方法读取到程序中的数据保存到String中? - Java- how do I save data that is being read into my program by a public static void method into a String? 从静态Java获取公共类的数据 - get data from public class from static java 如何从子类的静态方法获取类? - How do I get the class from a static method of a subclass? Java-如何获取从一个类中的方法生成到另一个类中的变量? - Java - How do I get a variable generated from within a method in one class to another class? 如何从另一个类的静态方法更改静态变量的值 - how to change the value of a static variable from a static method of another class 想要使用来自同一公共类的另一个公共方法中的所有数据成员调用一个公共方法 - Want to call a public method with all data members in another public method from same public class 如何在非静态方法中从另一个类调用非静态方法? (java) - How do I call a non-static method from another class in a non-static method? (java) 如何将更新的全局类变量从一种方法传递到另一种方法? - How do you pass an updated global class variable from one method into another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM