功能:建立连接,数据读取
package com.epoint.HadoopAPIDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class PostgreSQLTest {
// JDBC连接串
static String url = "jdbc:postgresql://192.168.186.14:5432/hive";
// PG用户名
static String usr = "hive";
// PG密码
static String psd = "h3cDataEngine";
public static void main(String args[]) {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
// 创建连接
conn = DriverManager.getConnection(url, usr, psd);
Statement st = conn.createStatement();
// 查询TEST表内容
ResultSet rs = st.executeQuery("SELECT * FROM \"DBS\"");
while (rs.next()) {
System.out.print(rs.getString(1));
System.out.print(" ");
System.out.println(rs.getString(2));
}
rs.close();
st.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接
if (conn != null) {
try {
conn.close();
conn = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}