我收到错误"错误:java.sql.SQLException:列索引无效"用下面的代码。能帮到我在哪里做错了吗?
String SEARCH_SQL_COUNT = "SELECT COUNT(*) FROM (
SELECT AD_TITLE,DESCRIPTION,STATE_NAME,CITY_NAME,CATEGORY,SUBCATEGORY
FROM PERSONAL_INFO UNION ALL
SELECT AD_TITLE,DESCRIPTION,STATE_NAME,CITY_NAME,CATEGORY,SUBCATEGORY
FROM ELEC_AUTO_MERC UNION ALL
SELECT AD_TITLE,DESCRIPTION,STATE_NAME,CITY_NAME,CATEGORY,SUBCATEGORY
FROM MISSING_PERSON_INFO UNION ALL
SELECT AD_TITLE,DESCRIPTION,STATE_NAME,CITY_NAME,CATEGORY,SUBCATEGORY
FROM PETS_INFO UNION ALL
SELECT AD_TITLE,DESCRIPTION,STATE_NAME,CITY_NAME,CATEGORY,SUBCATEGORY
FROM REAL_ESTATE UNION ALL
SELECT AD_TITLE,DESCRIPTION,STATE_NAME,CITY_NAME,CATEGORY,SUBCATEGORY
FROM SERVICES_INFO)
WHERE UPPER(AD_TITLE) LIKE UPPER('%?%')
OR UPPER(DESCRIPTION) LIKE UPPER('%?%')
OR UPPER(STATE_NAME) LIKE UPPER('%?%')
OR UPPER(CITY_NAME) LIKE UPPER('%?%')
OR UPPER(CATEGORY) LIKE UPPER('%?%')
OR UPPER(SUBCATEGORY) LIKE UPPER('%?%')";
int ad_count=0;
PreparedStatement st_fetch_product_count_1 = conn.prepareStatement(SEARCH_SQL_COUNT);
st_fetch_product_count_1.setString(1,qs_keyword);
st_fetch_product_count_1.setString(2,qs_keyword);
st_fetch_product_count_1.setString(3,qs_keyword);
st_fetch_product_count_1.setString(4,qs_keyword);
st_fetch_product_count_1.setString(5,qs_keyword);
st_fetch_product_count_1.setString(6,qs_keyword);
ResultSet rs_fetch_product_count_1 = st_fetch_product_count_1.executeQuery();
答案 0 :(得分:1)
您正在执行SELECT COUNT(*)
声明。这样的陈述只会产生 一行 ,其中包含 单列 ,即计数。您的setString()
调用预计会有6列,因此索引2已经超出范围。
此外,您尝试在只读结果集上使用setString()
,但这是一个不同的问题。