概述
MySQL中获取数据
public RestMsg<Object> getZhen( HttpServletRequest request) {
RestMsg<Object> rm = new RestMsg<Object>();
List<String> list = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//第一步:加载MySQL的JDBC的驱动
Class.forName("com.mysql.jdbc.Driver");
//取得连接的 url,能访问MySQL数据库的用户名,密码;数据库名
String url = "jdbc:mysql://10.1.35.13:3306/tsinghuadaxue?useUnicode=true&characterEncoding=utf-8";
String user = "root";
String password = "root";
//第二步:创建与MySQL数据库的连接类的实例
conn = DriverManager.getConnection(url, user, password);
//第三步:用conn创建Statement对象类实例 stmt
stmt = conn.createStatement();
//第四步:执行查询,用ResultSet类的对象,返回查询的结果
String sql = "select DISTINCT xzname from zoning WHERE xzname like '%街道' or xzname LIKE '%镇' or xzname LIKE '%乡'";
rs = stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1)); //取得数据库中的数据
list.add(rs.getString("xzname"));
}
} catch (ClassNotFoundException e) {
//加载JDBC错误,所要用的驱动没有找到
System.out.println("驱动加载错误");
}catch (SQLException ex) {
//显示数据库连接错误或查询错误
System.err.println("SQLException:"+ex.getMessage());
} finally {
try{
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
}catch(SQLException e) {
System.err.println("SQLException:"+e.getMessage());
}
}
rm = rm.successMsg();
rm.setResult(list);
return rm;
}
hive中获取数据
@Override
public Map<String, Object> getResultBySelcet(String selectSql, String... field) {
Map<String,Object> result = new HashMap<>();
Connection con = null;
Statement stmt = null;
ResultSet datSet = null;
try{
Class.forName(jdbcHiveDriver);
con = DriverManager.getConnection(jdbcHiveUrl, hiveUser, hivePwd);
stmt = con.createStatement();
datSet = stmt.executeQuery(selectSql);
if(datSet != null){
while(datSet.next()){
for(String name : field) {
if (datSet.getString(name) != null) {
result.put(name,datSet.getString(name));
}
}
break;
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if (datSet != null) {
datSet.close();
}
if(stmt != null){
stmt.close();
}
if(con != null){
con.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
return result;
}
@Override
public List<Map<String, Object>> getAllResultBySelcet(String selectSql, String... field) {
List<Map<String,Object>> result = new ArrayList<>();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(jdbcHiveDriver);
con = DriverManager.getConnection(jdbcHiveUrl, hiveUser, hivePwd);
stmt = con.createStatement();
rs = stmt.executeQuery(selectSql);
while (rs.next()) {
Map<String,Object> temp = new HashMap<>();
for(String name : field) {
if (rs.getString(name) != null) {
temp.put(name,rs.getString(name));
}
}
result.add(temp);
}
rs.close();
}catch (Exception e){
e.printStackTrace();
}
finally {
try {
if (rs != null) {
rs.close();
}
if(stmt != null){
stmt.close();
}
if(con != null){
con.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
return result;
}
最后
以上就是美满指甲油为你收集整理的从hive中获取数据的全部内容,希望文章能够帮你解决从hive中获取数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复