文档搜索
通过JDBC SDK调用召回引擎版实例进行数据检索。
代码示例
import java.sql.*;
import java.util.Properties;
import com.aliyun.ha3engine.jdbc.Ha3ResultSet;
import com.aliyun.ha3engine.jdbc.common.exception.ErrorInfo;
import org.junit.Before;
import org.junit.Test;
public class Search {
private static Connection connection;
@Before
public void initConnection() throws ClassNotFoundException, SQLException {
// 设置驱动
String name = "com.aliyun.ha3engine.jdbc.Ha3Driver";
Properties props = new Properties();
// API域名,可在实例详情页>API入口 查看
props.setProperty("serviceName", "ha-cn-i7*****605.public.ha.aliyuncs.com");
// 用户名,可在实例详情页>API入口 查看
props.setProperty("username", "username");
// 密码,可在实例详情页>API入口 修改
props.setProperty("password", "password");
// 开启动态参数
props.setProperty("enableDynamicParams", "true");
props.setProperty("enableDetailLog", "true");
Class.forName(name);
connection = DriverManager.getConnection("jdbc:ha3://", props);
}
/**
* 搜索数据
* @throws SQLException
*/
@Test
public void search1() throws SQLException {
// 使用PreparedStatement
PreparedStatement preparedStatement = connection.prepareStatement(
"select * from `test1` where `id` in (select `id` from `test1` where `count` > ? and `id` > ?)");
preparedStatement.setInt(1, 14);
preparedStatement.setInt(2, 1);
Ha3ResultSet resultSet = (Ha3ResultSet) preparedStatement.executeQuery();
ErrorInfo errorInfo = resultSet.getErrorInfo();
if (errorInfo != null && errorInfo.getErrorCode() != 0) {
System.out.println("error exception.");
System.out.println(errorInfo.getErrorCode());
System.out.println(errorInfo.getError());
System.out.println(errorInfo.getMessage());
} else {
while (resultSet.next()) {
System.out.println(resultSet.getInt("id"));
System.out.println(resultSet.getString("title"));
System.out.println(resultSet.getString("body"));
System.out.println(resultSet.getInt("count"));
System.out.println(resultSet.getDouble("price"));
}
}
}
/**
* 搜索数据
* @throws SQLException
*/
@Test
public void search2() throws SQLException {
// 使用Statement
Statement statement = connection.createStatement();
Ha3ResultSet resultSet = (Ha3ResultSet) statement.executeQuery("select `id`,`title`,`body` from `test1` where `id` in (select `id` from `test1` where `count` > 9)");
ErrorInfo errorInfo = resultSet.getErrorInfo();
if (errorInfo != null && errorInfo.getErrorCode() != 0) {
System.out.println("error exception.");
System.out.println(errorInfo.getErrorCode());
System.out.println(errorInfo.getError());
System.out.println(errorInfo.getMessage());
} else {
while (resultSet.next()) {
System.out.println(resultSet.getLong("id"));
System.out.println(resultSet.getString("title"));
System.out.println(resultSet.getString("body"));
}
}
}
}
该文章对您有帮助吗?