gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:56:44 25 4
gpt4 key购买 nike

我需要从另一个类读取“public static NEW_QUERY”,但它在查询内部有变量,这些变量在调用该方法时会发生更改。我如何将其称为“公共(public)静态”并从另一个类获取更新的查询。

这是 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);
}
}
}

最佳答案

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

如果你的 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 = 
"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

关于java - 如何从另一个类读取方法中的变量数据的公共(public)静态并获取更新的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13732430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com