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