2020-06-29 使用java连接HANA数据库并执行sql语句

简单测试是否可以建立连接

HanaConn.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class HanaConn {

        /**
 *  *       * @param args
 *   *               */
        public static void main(String[] args) {
       getConnection();
        }

        /**
 *  *       * @return
 *   *               */
        private static Connection getConnection(){
                String url = "jdbc:sap://192.168.144.18:30015?reconnect=true";
                Connection conn = null;
                try {
                        Class.forName("com.sap.db.jdbc.Driver");
                        conn = DriverManager.getConnection(url,"ESB","Esb123456");
                        System.out.println(conn);
                } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                }catch (SQLException e) {
                        e.printStackTrace();
                }
                return conn;
        }
}

测试建立连接后执行sql语句

JDBCDemoHANA.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

/**
* JDBC Sample.
*
* @author  wangbing
* @version CVS $Revision$ $Date$
*/
public class JDBCDemoHANA {


private static final String DRIVER = "com.sap.db.jdbc.Driver";  //jdbc 4.0
private static final String URL = "jdbc:sap://192.168.144.18:30015?reconnect=true";

public JDBCDemoHANA() {

}

public static void main(String[] args) {
  JDBCDemoHANA demo = new JDBCDemoHANA();
  try {
   demo.select();
  } catch (Exception e) {
   e.printStackTrace();
  }
}

public void select() throws Exception {
  Connection con = this.getConnection(DRIVER, URL, "ESB", "Esb123456");
  PreparedStatement pstmt = con.prepareStatement("select * from sapabap1.ekpo where bukrs like '6%'");
  //PreparedStatement pstmt = con.prepareStatement("select USER_ID,USER_NAME,USER_MODE from SYS.USERS");
  ResultSet rs = pstmt.executeQuery();
  this.processResult(rs);
  this.closeConnection(con, pstmt);

}

private void processResult(ResultSet rs) throws Exception {
  if (rs.next()) {
   ResultSetMetaData rsmd = rs.getMetaData();
   int colNum = rsmd.getColumnCount();
   for (int i = 1; i <= colNum; i++) {
    if (i == 1) {
     System.out.print(rsmd.getColumnName(i));
    } else {
     System.out.print("\t" + rsmd.getColumnName(i));
    }

   }
   System.out.print("\n");
   System.out.println("———————–");
   do {
    for (int i = 1; i <= colNum; i++) {
     if (i == 1) {
      System.out.print(rs.getString(i));
     } else {
      System.out.print("\t"
        + (rs.getString(i) == null ? "null" : rs
          .getString(i).trim()));
     }

    }
    System.out.print("\n");
   } while (rs.next());
  } else {
   System.out.println("query not result.");
  }

}

private Connection getConnection(String driver, String url, String user,
   String password) throws Exception {
  Class.forName(driver);
  return DriverManager.getConnection(url, user, password);

}

private void closeConnection(Connection con, Statement stmt)
   throws Exception {
  if (stmt != null) {
   stmt.close();
  }
  if (con != null) {
   con.close();
  }
}

}

CentOS服务器的命令行下测试上述java code执行sql语句的效果:

javac JDBCDemoHANA.java
java -cp $CLASSPATH:/data/orchsym/runtime/driver/hana/ngdbc.jar JDBCDemoHANA

你可能感兴趣的:(2020-06-29 使用java连接HANA数据库并执行sql语句)