SAP Hana 数据库编程接口 - JDBC
2017-12-30 19:23
459 查看
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; } }
相关文章推荐
- JDBC数据库编程:ResultSet接口
- SAP Hana 数据库编程接口 - ODBC
- Java学习笔记(JDBC数据库编程常用接口)
- JDBC数据库编程:callableStatement接口
- SAP Hana 数据库编程接口 - Python
- JDBC数据库编程:PreparedStatement接口
- SAP Hana 数据库编程接口 - Node.js
- JDBC数据库编程常用接口(转)
- JDBC:通过 Driver 接口获取数据库连接
- 数据库编程——intro to JDBC
- SQlite数据库的C编程接口(二) 数据库连接
- 22---数据库编程01(JDBC概述)
- 03.JDBC数据库编程之处理DML语句
- 自己对JDBC数据库编程的自己见解
- JDBC编程-----数据库的链接
- JAVA数据库编程——JDBC(一)
- 自学Java之Java编程(使用JDBC访问数据库)(018day)
- 数据库:JDBC编程
- Java的数据库连接编程(JDBC)技术
- 2.通过原始的Driver接口通过读取Properties属性文件获取JDBC数据库连接Connection