1. 连接 oracle 使用 jdk 自带的包即可, 连接 HANA 需要另外下载 ngdbc.jar 包, 并添加到项目路径中.
2. 两者只在获取连接时代码不一致, 获取到 Connection 后, 进行 prepareStatement 等操作都是完全一致的, 所以这里只给出获取 Connection 的代码:
import java.sql.Connection;
import java.sql.DriverManager;
Connection connOra = null;
Connection connHana = null;
// Oracle 连接字符串
String connectionStringOra = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String usernameOra = "usernameOra";
String passwordOra = "passwordOra";
// HANA 连接字符串
String connectionStringHana = "jdbc:sap://127.0.0.1:30015?reconnect=true ";
String usernameHana = "usernameHana";
String passwordHana = "passwordHana";
try {
// 连接 Oracle
Class.forName("oracle.jdbc.OracleDriver");
connOra = DriverManager.getConnection(connectionStringOra,
usernameOra, passwordOra);
// 连接 HANA
Class.forName("com.sap.db.jdbc.Driver");
connHana = DriverManager.getConnection(connectionStringHana,
usernameHana, passwordHana);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
3. 环境: JDK1.6.0_30