[英]How do I call a variable from the public static void main to another class?
[英]How do I read in a public static, with variable data from a method, from another class and get updated data
我需要從另一個類中讀取“公共靜態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);
}
}
}
除此之外,我在這里還沒有嘗試過,但是我應該按照規格說明進行操作:
對於任何String.format命令,您都必須轉義任何現有的%符號。 這些符號通常在LIKE操作中使用。
我正在做的是對另一個文件中的每個查詢進行單元測試,以確保它們正確地將數據從MSSQL Server 2005遷移到MYSQL
是STRING.FORMAT SPEC提供的最佳解決方案。 這個對嗎。 測試通過了,但是我想應該盡可能更改原始代碼
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);
}
}
}
靜態變量/方法與lass定義相關聯,因此您不能在其中使用任何非靜態變量/方法。
如果您的match
和useFor
不是靜態的(就是這種情況),那么您就不能有這樣的靜態qquery:
public static String NEW_QUERY =“從tr_userfield中選擇count(userfieldid),其中calcexpression如'%'+ match +”%',而usefor如'%“ + 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
。
如果您使用PreparedStatement
,則不僅會避免從靜態上下文引用非靜態內容的問題,而且,您的應用程序不會輕易受到損害 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.