SAP Hana 数据库编程接口 - JDBC

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;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值