Java 连接SAP Hana数据库

最近项目有用到hana数据库的,记录一下相关资料。

hana连接可参考官网:Hana JDBC创建

整个步骤如下:

  1. 项目中增加ngdbc.jar,可以从该博客中链接下载com.sap.db.jdbc.Driver ngdbc-2.4.59.jar SAPHANA数据库驱动,或者电脑里有安装过hana的,可以在C:\Program Files\sap\hdbstudio\configuration\org.eclipse.osgi\149\0\.cp\lib 该目录下找到。

  2. 连接hana数据库,示例代码如下:

import java.sql.*;
public class jdemo {
   public static void main(String[] argv) {
      Connection connection = null;
      try {                  
         connection = DriverManager.getConnection(
            "jdbc:sap://myhdb:30715/?autocommit=false", "myName", "mySecret");                  
      } catch (SQLException e) {
         System.err.println("Connection Failed:");
         System.err.println(e);
         return;
      }
      if (connection != null) {
         try {
            System.out.println("Connection to HANA successful!");
            Statement stmt = connection.createStatement();
            ResultSet resultSet = stmt.executeQuery("Select 'hello world' from dummy");
            resultSet.next();
            String hello = resultSet.getString(1);
            System.out.println(hello);
       } catch (SQLException e) {
          System.err.println("Query failed!");
       }
     }
   }
}

其中,"jdbc:sap://myhdb:30715/?autocommit=false", "myName", "mySecret" 这一段myhdb:30715是hana数据库地址和端口,myName是用户名,mySecret是密码。

注:resultSet是一个java类类型,可以遍历查询,可参考该博客ResultSet的遍历方法

你可能感兴趣的:(JAVA工作日常)