JDBC 快速入门

当前版本:v3.0, 更新时间:2020-09-16

在项目中导入jar包 “pinusdb_jdbc_3.x.x.jar” 即可。

  1. public class Test {
  2. public static void printResultSet(ResultSet rs) throws SQLException {
  3. ResultSetMetaData rsMeta = rs.getMetaData();
  4. int colCnt = rsMeta.getColumnCount();
  5. for(int i = 1; i <= colCnt; i++) {
  6. System.out.print("--");
  7. System.out.print(rsMeta.getColumnName(i));
  8. System.out.print(",");
  9. System.out.print(rsMeta.getColumnTypeName(i));
  10. System.out.print("--|");
  11. }
  12. System.out.println();
  13. while(rs.next()) {
  14. for(int i = 1; i <= colCnt; i++) {
  15. System.out.print(rs.getObject(i).toString());
  16. System.out.print(" | ");
  17. }
  18. System.out.println();
  19. }
  20. }
  21. public static void Query(String connStr, String querySql) {
  22. try {
  23. Connection conn = DriverManager.getConnection(connStr);
  24. Statement statement = conn.createStatement();
  25. ResultSet rs = statement.executeQuery(querySql);
  26. printResultSet(rs);
  27. rs.close();
  28. conn.close();
  29. } catch (SQLException ex) {
  30. System.out.println("Exception:");
  31. System.out.println(ex.getMessage());
  32. }
  33. }
  34. public static void main(String[] args) throws ClassNotFoundException {
  35. Class.forName("cn.pinusdb.jdbc.PDBDriver");
  36. String connStr = "jdbc:pinusdb://127.0.0.1:8105?user=sa&password=pinusdb";
  37. Query(connStr, "select * from sys_table");
  38. }
  39. }