使用jdbc连接本地postgreSQL的一个例子

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介: 使用jdbc连接本地postgreSQL的一个例子
package postgresql;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
public class PostgreSQLJDBC {
  private Connection connection = null;
  @SuppressWarnings("unused")
  private void select() {
    try {
      int index = 0;
      Class.forName("org.postgresql.Driver");
      connection = DriverManager.getConnection(
          "jdbc:postgresql://localhost:9812/zproduct", "postgres", "Saptest1");
      Statement stmt = connection.createStatement();
      String query = "SELECT * FROM public.comm_product;";
      ResultSet rs = stmt.executeQuery(query);
           while ( rs.next() ) {
            System.out.println("Row index: " + index++);
            String  client = rs.getString("client");
              System.out.println("Client: " + client);
              String  guid = rs.getString("product_guid");
              System.out.println("Product guid: " + guid);
              Timestamp validFrom = rs.getTimestamp("valid_from");
              System.out.println("Valid from: " + validFrom);
           }
           rs.close();
           stmt.close();
           connection.close();
    } catch (Exception e) {
      e.printStackTrace();
    } 
  }
  public static void main(String args[]) {
    PostgreSQLJDBC jdbcTest = new PostgreSQLJDBC();
    //jdbcTest.select();
    jdbcTest.clobTest();
  }
  @SuppressWarnings("unused")
  private void clobTest(){
    String description = null;
      Clob myClob = null;
      PreparedStatement pstmt = null;
      try {
        connection = DriverManager.getConnection(
          "jdbc:postgresql://localhost:9812/zproduct", "postgres", "Saptest1");
          String sql =
              "select text " +
              "from public.ztest2 " +
              "where key1 = ?";
          pstmt = this.connection.prepareStatement(sql);
          pstmt.setString(1, "1");
          ResultSet rs = pstmt.executeQuery();
          if (rs.next()) {
            System.out.println(rs.getString(1));
              //myClob = rs.getClob(1);
              //System.out.println("Length of retrieved Clob: " +
              //    myClob.length());
          }
          // description = myClob.getSubString(1, 10);
      } catch (SQLException sqlex) {
          sqlex.printStackTrace();
      } catch (Exception ex) {
          System.out.println("Unexpected exception: " + ex.toString());
      } finally {
          if (pstmt != null)
        try {
          pstmt.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
      }
      // System.out.println("Description: " + description);
  }
}


相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
2月前
|
Java 数据库连接
JDBC连接复习
JDBC连接复习
31 1
|
3月前
|
关系型数据库 Shell C#
PostgreSQL修改最大连接数
在使用PostgreSQL时,可能遇到“too many clients already”错误,这是由于默认最大连接数(100)不足。要增加此数值,需修改`postgresql.conf`中的`max_connections`参数
166 5
|
2月前
|
SQL Java 数据库连接
Java开发者必知:JDBC连接数据库的“三大法宝”
Java开发者必知:JDBC连接数据库的“三大法宝”
19 7
|
2月前
|
SQL 关系型数据库 数据库
PostgreSQL常用命令,启动连接,pg_dump导入导出
PostgreSQL常用命令,启动连接,pg_dump导入导出
|
2月前
|
SQL Java 数据库连接
JDBC连接SQL Server2008 完成增加、删除、查询、修改等基本信息基本格式及示例代码
这篇文章提供了使用JDBC连接SQL Server 2008数据库进行增加、删除、查询和修改操作的基本步骤和示例代码。
|
2月前
|
SQL 存储 Java
完整java开发中JDBC连接数据库代码和步骤
该博客文章详细介绍了使用JDBC连接数据库的完整步骤,包括加载JDBC驱动、提供连接URL、创建数据库连接、执行SQL语句、处理结果以及关闭JDBC对象的过程,并提供了相应的示例代码。
|
2月前
|
关系型数据库 MySQL 数据库
postgresql使用mysql_fdw连接mysql
通过以上步骤,你可以在PostgreSQL中访问和查询远程MySQL服务器的数据,这对于数据集成和多数据库管理非常有用。
95 0
|
3月前
|
分布式计算 DataWorks 关系型数据库
DataWorks操作报错合集之使用连接串模式新增PostgreSQL数据源时遇到了报错"not support data sync channel, error code: 0001",该怎么办
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
|
2月前
|
SQL 关系型数据库 数据库
EF Core连接PostgreSQL数据库
EF Core连接PostgreSQL数据库
28 0
|
2月前
|
Java 关系型数据库 MySQL
使用JDBC连接ADB
【8月更文挑战第6天】
110 0
下一篇
无影云桌面