SAP Hana 資料庫編程介面 - JDBC__.NET

來源:互聯網
上載者:User
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;    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.