public class TestQuery {
static Hits hits = null;
static Query query = null;
static String queryString = "放得开";
static DataOperator db = new DataOperator();
static String sql ="select * from person ";
static ResultSet r = db.executeQuery(sql);
public static void main(String[] args) throws IOException, ParseException, SQLException {
ResultSetMetaData data=r.getMetaData();
//获得所有列的数目及实际列数
int columnCount=data.getColumnCount();
System.out.println("列数/字段数:"+columnCount);
System.out.println("queryString;"+queryString);
// if(r.last()){
// //先rs.last(),跳到最后一行,然后获得的行数就是查询集的行数,在rs.beforefirst()
// System.out.println("行数/记录条数:"+r.getRow());
// }
//获得指定列的列名
String columnName = data.getColumnName(2);
while(r.next()){
// for(int i = 1 ; i<= data.getColumnCount() ; i++){ //循环表的字段
//获得指定列的列值
String columnValue = r.getString(2);
System.out.println("字段名:"+columnName+" 字段值:"+columnValue);
}
try {
GetHit(columnName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// }
public static Hits GetHit(String columnName) throws Exception{
Hits hits = null;
IndexSearcher searcher = new IndexSearcher("d:\\index");
// Analyzer analyzer = new StandardAnalyzer();
try {
// QueryParser qp = new QueryParser(columnName, analyzer); //不可查询到中文
Query query = new WildcardQuery(new Term(columnName,queryString));//可查询到中文
// query = qp.parse(queryString);
if (searcher != null) {
hits = searcher.search(query);
System.out.println("hits.length():"+hits.length());
if (hits.length()> 0) {
System.out.println("找到:" + hits.length() + " 个结果!");
}
}
} catch (Exception e) {
}
return hits;
}
}
————————————————————
字段名columnName和查询内容queryString要与查询的数据表一致