前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用jdbc连接本地postgreSQL的一个例子

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

作者头像
Jerry Wang
发布2020-04-29 16:34:29
1K0
发布2020-04-29 16:34:29
举报
代码语言:javascript
复制
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);
	}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档