美文网首页
SAP Hana 数据库编程接口 - JDBC

SAP Hana 数据库编程接口 - JDBC

作者: Stone0823 | 来源:发表于2017-12-30 18:57 被阅读196次

Hana JDBC 驱动

安装 SAP HANA Client 后,安装目录的 ngdbc.jar 就是 JDBC 数据库驱动。主要注意 url 的写法和 Driver 的名称 :

  • Driver: com.sap.db.jdbc.Driver
  • url: jdbc:sap://ip_addr:30015
  • 端口:3 + instance number + 15

代码示例

因为和其他数据库并没有区别,这里直接贴上代码。

实体类

package stone.hanatest;

public class EmployeeEntity {
    public String EmpId;
    public String Gender;
    public int Age;
    public String EMail;
    public String PhoneNr;
    public String Education;
    public String MaritalStat;
    public int NrOfChildren;
}

CRUD 代码

package stone.hanatest;

import java.sql.*;

public class HanaCRUD {
    private static final String DRIVER = "com.sap.db.jdbc.Driver";
    private static final String URL = "jdbc:sap://192.168.2.100:30015?reconnect=true";
    private String user = "STONE";
    private String pwd = "hanapwd";
    
    public Connection getConnection(String userid, String pwd) 
            throws ClassNotFoundException, SQLException{
        
        Class.forName(DRIVER);
        return DriverManager.getConnection(URL, userid, pwd);       
    }
    
    public void release(Connection conn, Statement stmt) throws SQLException{
        if (stmt != null){
            stmt.close();
        }
        
        if (conn != null){
            conn.close();
        }
    }
    
    public void printEmployees() throws ClassNotFoundException, SQLException{
        Connection conn = this.getConnection(user, pwd);
        String sql = "SELECT * FROM STONE.EMP_MASTER";
        PreparedStatement pStmt = conn.prepareStatement(sql);
        
        ResultSet rst = pStmt.executeQuery();
        
        while(rst.next()){
            System.out.print(rst.getString("EMP_ID") + "|");
            System.out.print(rst.getString("GENDER") + "|");
            System.out.print(rst.getString("EMAIL"));       
            
            // new line
            System.out.println();
        }   
        
        this.release(conn, pStmt);
    }
    
    public int insertEmployee(EmployeeEntity emp) 
            throws ClassNotFoundException, SQLException{
        
        Connection conn = this.getConnection(user, pwd);
        String sql = "INSERT INTO STONE.EMP_MASTER VALUES (?,?,?,?,?,?,?,?)";
        PreparedStatement pStmt = conn.prepareStatement(sql);
        
        pStmt.setString(1, emp.EmpId); // starts from 1 instead of 0
        pStmt.setString(2, emp.Gender);
        pStmt.setInt(3, emp.Age);
        pStmt.setString(4, emp.EMail);
        pStmt.setString(5, emp.PhoneNr);
        pStmt.setString(6, emp.Education);
        pStmt.setString(7, emp.MaritalStat);
        pStmt.setInt(8, emp.NrOfChildren);
        
        int count = pStmt.executeUpdate();
        
        this.release(conn, pStmt);
        
        return count;       
    }
    
    public int deleteEmployee(String empId) 
            throws ClassNotFoundException, SQLException{
        
        Connection conn = this.getConnection(user, pwd);
        String sql = "DELETE FROM STONE.EMP_MASTER WHERE EMP_ID=? ";
        PreparedStatement pStmt =  conn.prepareStatement(sql);
        
        pStmt.setString(1, empId);
        int count = pStmt.executeUpdate();
        
        this.release(conn, pStmt);
        
        return count;
    }
}

相关文章

网友评论

      本文标题:SAP Hana 数据库编程接口 - JDBC

      本文链接:https://www.haomeiwen.com/subject/gwrngxtx.html