以下是我的代码,我正在传递stateid,并且我正在从stateid中检索数据。
代码适用于第一次迭代,即对于stateid = 1,它返回一个结果,但是对于第二次迭代,即当state_id = 2时,它返回两个记录,即s_id = 1和2。
如何解决此问题并仅获取s_id = stateid的具体记录?
数据库结构是: -
s_id base_formid base_xpath
1 1 .//*[@id='collapse-text-dynamic-form-number-1']/div/fieldset/legend/span/a
2 2 .//*[@id='collapse-text-dynamic-form-number-1']/div/fieldset/legend/span/a
public static Map baseXpath(int stateid)
{
Statement s2 = null;
try {
con=null;
con = DriverManager.getConnection(conUrl, userName, password);
s2 = con.createStatement();
String query = "SELECT base_formid,base_xpath FROM base_path where base_path.s_id = " + stateid;
ResultSet rs2 = null;
rs2 = s2.executeQuery(query);
while (rs2.next()) {
int basepath_id = rs2.getInt(1);
String base_xpath = rs2.getString(2);
basex_path.put(basepath_id, base_xpath);
}
con.close();
return basex_path;
答案 0 :(得分:0)
由于您要将参数传递给select
查询,我建议您使用PreparedStatement而不是Statement。下面的代码可以帮助您实现所需的结果。
public static Map baseXpath(int stateid)
{
PreparedStatement s2 = null;
try {
con=null;
con = DriverManager.getConnection(conUrl, userName, password);
String query = "SELECT base_formid,base_xpath FROM base_path where base_path.s_id = ?";
s2 = con.prepareStatement(query);
s2.setInt(1, stateid);
ResultSet rs2 = null;
rs2 = s2.executeQuery();
while (rs2.next()) {
int basepath_id = rs2.getInt(1);
String base_xpath = rs2.getString(2);
basex_path.put(basepath_id, base_xpath);
}
con.close();
return basex_path;
}