scala连接postgre数据库

2021-04-28 11:41:23 浏览数 (1)

因为企业上使用postgre timescale数据库来保存实时数据,所以在做spark任务时需要去连接postgre数据库,demo如下:

代码语言:javascript复制
val conn_str = "jdbc:postgresql://ip:port/db"
        
          val conn: Connection = DriverManager.getConnection(conn_str, "用户名", "密码")
          
              try {
                     // Configure to be Read Only
            val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
                     // Execute Query
                     val rs = statement.executeQuery("SELECT * FROM spark_table")
                     // Iterate Over ResultSet
                     while (rs.next) {
                            println(rs.getString("xxx"))
                     }
              }
              finally {
                     conn.close
              }

pom文件中引入如下依赖:

代码语言:javascript复制
<dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.1.1</version>
        </dependency>

0 人点赞