Java连接HANA数据库

package hs.hsapp.util;

import java.sql.*;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class HanaJdbc {

private static final String DRIVER = "com.sap.db.jdbc.Driver"; //jdbc 4.0

private static final String URL = "jdbc:sap://IP:30041?reconnect=true";

复制代码
public List<Map<Object, Object>> select(String sql) {
    List<Map<Object, Object>> list = new ArrayList<>();
    try{
        Connection con = this.getConnection(DRIVER, URL, "USR", "PWD");
        PreparedStatement pstmt = con.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        //this.processResult(rs);
        ResultSetMetaData rsmd = rs.getMetaData();
        int colNum = rsmd.getColumnCount();
        while (rs.next()) {
            Map<Object,Object> map = new HashMap<>();
            for(int i = 1; i <= colNum; i++) {
                //System.out.println("col: " + rsmd.getColumnName(i) + ",val: " + rs.getObject(i));
                map.put(rsmd.getColumnName(i),rs.getObject(i));
            }
            list.add(map);
        }
        this.closeConnection(con, pstmt);
    }catch (Exception ex) {
        ex.printStackTrace();
    }
    return list;
}

private void processResult(ResultSet rs) throws Exception {
    if (rs.next()) {
        ResultSetMetaData rsmd = rs.getMetaData();
        int colNum = rsmd.getColumnCount();
        for (int i = 1; i <= colNum; i++) {
            if (i == 1) {
                System.out.print(rsmd.getColumnName(i));
            } else {
                System.out.print("\t" + rsmd.getColumnName(i));
            }

        }
        System.out.print("\n");
        System.out.println("-----------------------");
        do {
            for (int i = 1; i <= colNum; i++) {
                if (i == 1) {
                    System.out.print(rs.getString(i));
                } else {
                    System.out.print("\t"
                            + (rs.getString(i) == null ? "null" : rs
                            .getString(i).trim()));
                }

            }
            System.out.print("\n");
        } while (rs.next());
    } else {
        System.out.println("query not result.");
    }

}

private Connection getConnection(String driver, String url, String user,
                                 String password) throws Exception {
    Class.forName(driver);
    return DriverManager.getConnection(url, user, password);

}

private void closeConnection(Connection con, Statement stmt)
        throws Exception {
    if (stmt != null) {
        stmt.close();
    }
    if (con != null) {
        con.close();
    }
}

}

相关推荐
好奇的菜鸟5 小时前
MySQL 8 开启远程登录
数据库·mysql·adb
代码or搬砖5 小时前
Java Lambda 表达式全面详解
java·开发语言·python
okseekw5 小时前
Java初学者的static探险记:原来“静态”是这么个省心玩意儿!
java
这周也會开心5 小时前
JDK1.8新增语法
java·开发语言
心随雨下5 小时前
TypeScript泛型开发常见错误解析
java·开发语言·typescript
DonaldCen6665 小时前
Java 王者修炼手册【Mysql篇 - SQL执行存储流程】:拆解 InnoDB 存储结构与 SQL 执行流程,吃透 Buffer Pool 和 Change
java
旺仔Sec5 小时前
2025年广东省职业院校技能大赛高职组“区块链技术应用”竞赛试题(二)
java·区块链
Boop_wu5 小时前
[Java EE] 多线程编程进阶
java·数据库·java-ee
w***37516 小时前
SpringBoot【实用篇】- 测试
java·spring boot·后端
深瞳智检6 小时前
学习应用 第001期-Windows 10 用 CMD 安装 MySQL 全流程解析(免安装版)
数据库·windows·mysql·压缩包·环境安装