JDBC驱动

用户下载 JDBC 驱动 并导入 jar 包后,即可以使用 JDBC 提供的 API。

示例

以下示例为通过 maven 工程使用 JDBC 进行简单的增删改查操作。

  1. pom.xml 中添加 PostgreSQL JDBC 驱动的依赖,以 postgresql-9.3-1104-jdbc41 为例

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.postgresql</groupId>
    4. <artifactId>postgresql</artifactId>
    5. <version>9.3-1104-jdbc41</version>
    6. </dependency>
    7. </dependencies>
  2. 假设本地有默认安装的 PostgreSQL 实例,连接到该实例并准备数据库 sample 和表 test,将其映射到 SequoiaDB 已存在的集合 sample.employee

    1. $ bin/psql -p 5432 sample
    2. psql (9.3.4)
    3. Type "help" for help.
    4. sample=# create foreign table test (name text, id numeric) server sdb_server options (collectionspace 'sample', collection 'employee', decimal 'on');
    5. CREATE FOREIGN TABLE
    6. sample=# \q
  3. 在工程的 src/main/java/com/sequoiadb/sample 目录下添加 Sample.java 文件

    1. package com.sequoiadb.sample;
    2. import java.sql.Connection;
    3. import java.sql.DriverManager;
    4. import java.sql.PreparedStatement;
    5. import java.sql.ResultSet;
    6. import java.sql.ResultSetMetaData;
    7. import java.sql.SQLException;
    8. public class Sample {
    9. static {
    10. try {
    11. Class.forName("org.postgresql.Driver");
    12. } catch (ClassNotFoundException e) {
    13. e.printStackTrace();
    14. }
    15. }
    16. public static void main(String[] args) throws SQLException {
    17. String pghost = "127.0.0.1";
    18. String port = "5432";
    19. String databaseName = "sample";
    20. // postgresql process is running in which user
    21. String pgUser = "sdbadmin";
    22. String url = "jdbc:postgresql://" + pghost + ":" + port + "/" + databaseName;
    23. Connection conn = DriverManager.getConnection(url, pgUser, null);
    24. // insert
    25. String sql = "INSERT INTO test(name, id) VALUES(?, ?)";
    26. PreparedStatement pstmt = conn.prepareStatement(sql);
    27. for (int i = 0; i < 5; i++) {
    28. pstmt.setString(1, "Jim" + i);
    29. pstmt.setLong(2, i);
    30. pstmt.addBatch();
    31. }
    32. pstmt.executeBatch();
    33. // select
    34. sql = "SELECT * FROM test";
    35. pstmt = conn.prepareStatement(sql);
    36. ResultSet rs = pstmt.executeQuery();
    37. boolean isHeaderPrint = false;
    38. while (rs.next()) {
    39. ResultSetMetaData md = rs.getMetaData();
    40. int col_num = md.getColumnCount();
    41. if (!isHeaderPrint) {
    42. System.out.print("|");
    43. for (int i = 1; i <= col_num; i++) {
    44. System.out.print(md.getColumnName(i) + "\t|");
    45. isHeaderPrint = true;
    46. }
    47. System.out.println();
    48. }
    49. System.out.print("|");
    50. for (int i = 1; i <= col_num; i++) {
    51. System.out.print(rs.getString(i) + "\t|");
    52. }
    53. System.out.println();
    54. }
    55. pstmt.close();
    56. conn.close();
    57. }
    58. }
  4. 使用 maven 编译及运行

    1. $ mvn compile
    2. $ mvn exec:java -Dexec.mainClass="com.sequoiadb.sample.Sample"

    得到运行结果如下:

    1. |name |id |
    2. |Jim0 |0 |
    3. |Jim1 |1 |
    4. |Jim2 |2 |
    5. |Jim3 |3 |
    6. |Jim4 |4 |