forked from docs/doc-exports
Reviewed-by: Wagner, Fabian <fabian.wagner@t-systems.com> Co-authored-by: Ru, Li Yi <liyiru7@huawei.com> Co-committed-by: Ru, Li Yi <liyiru7@huawei.com>
167 lines
14 KiB
HTML
167 lines
14 KiB
HTML
<a name="rds_faq_0050"></a><a name="rds_faq_0050"></a>
|
|
|
|
<h1 class="topictitle1">How Can I Connect to a PostgreSQL Database Through JDBC?</h1>
|
|
<div id="body8662426"><p id="rds_faq_0050__a7e744e76e1eb499c80c3c879a3bc8fa4">Although the SSL certificate is optional if you choose to connect to a database through Java database connectivity (JDBC), download an SSL certificate to encrypt the connections for security.</p>
|
|
<div class="section" id="rds_faq_0050__sea9673f8b08a450386506c34a14adf87"><h4 class="sectiontitle">Prerequisites</h4><p id="rds_faq_0050__p9618447172218">Familiarize yourself with:</p>
|
|
<ul id="rds_faq_0050__ul4492115518916"><li id="rds_faq_0050__li082513571097">Computer basics.</li><li id="rds_faq_0050__li1169814596918">Java programming language.</li><li id="rds_faq_0050__li19492105517915">JDBC knowledge.</li></ul>
|
|
</div>
|
|
<div class="section" id="rds_faq_0050__section1345145464117"><h4 class="sectiontitle">Obtaining and Using JDBC</h4><ul id="rds_faq_0050__ul6525407432"><li id="rds_faq_0050__li1452517016430">JDBC driver download address: <a href="https://jdbc.postgresql.org/download/" target="_blank" rel="noopener noreferrer">https://jdbc.postgresql.org/download/</a></li><li id="rds_faq_0050__li1152513054316">JDBC Interface: <a href="https://jdbc.postgresql.org/documentation/" target="_blank" rel="noopener noreferrer">https://jdbc.postgresql.org/documentation/</a></li></ul>
|
|
</div>
|
|
<div class="section" id="rds_faq_0050__section17811586410"><h4 class="sectiontitle">Connection with the SSL Certificate</h4><div class="note" id="rds_faq_0050__n75485520a4e04980938095216f9b234e"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="rds_faq_0050__en-us_topic_0086557099_p078120154817">Download the SSL certificate and verify the certificate before connecting to databases.</p>
|
|
<p id="rds_faq_0050__p6563591457">In the <span class="uicontrol" id="rds_faq_0050__uicontrol6711112572611"><b>DB Information</b></span> area on the <strong id="rds_faq_0050__b47120255266">Basic Information</strong> page, click <span><img id="rds_faq_0050__en-us_topic_0154555358_image197254186415" src="en-us_image_0000001191131453.png"></span> in the <strong id="rds_faq_0050__b47141225182613">SSL</strong> field to download the root certificate or certificate bundle.</p>
|
|
</div></div>
|
|
<ol id="rds_faq_0050__oec0c6aa47de24247977eced1d7f48033"><li id="rds_faq_0050__l66b2e2a41a224a84ba2c243cb283747d"><span>Connect to the RDS PostgreSQL DB instance through JDBC.</span><p><pre class="screen" id="rds_faq_0050__sce978ce9157548b2b3b7d66d0d5af3ea"><strong id="rds_faq_0050__a7c100d2ced73431fb754268f76221944">jdbc:postgresql://</strong><em id="rds_faq_0050__a3a85af8c2dd34bf0806d30d092959b2c"><instance_ip></em><strong id="rds_faq_0050__ade4001897b434064a91c5a01262690a3">:</strong><em id="rds_faq_0050__ab7f39daea2ad48109748dae2375cf256"><instance_port></em><strong id="rds_faq_0050__en-us_topic_0086557099_b897840182342">/</strong><em id="rds_faq_0050__a551874f95b71497f8190452e873f7281"><database_name></em><strong id="rds_faq_0050__a20bb36f7a534467eac06488c2f742c51">?sslmode=verify-full&sslrootcert=</strong><em id="rds_faq_0050__abcebc9731ee04caebc7b0b8169669682"><ca.pem></em></pre>
|
|
|
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="rds_faq_0050__table793953017457" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Parameter description</caption><thead align="left"><tr id="rds_faq_0050__row693919300454"><th align="left" class="cellrowborder" valign="top" width="24.47%" id="mcps1.3.4.3.1.2.2.2.3.1.1"><p id="rds_faq_0050__p9940730194518"><strong id="rds_faq_0050__b101311025165717">Parameter</strong></p>
|
|
</th>
|
|
<th align="left" class="cellrowborder" valign="top" width="75.53%" id="mcps1.3.4.3.1.2.2.2.3.1.2"><p id="rds_faq_0050__p59406301451"><strong id="rds_faq_0050__b93901932115711">Description</strong></p>
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody><tr id="rds_faq_0050__row69401301452"><td class="cellrowborder" rowspan="2" valign="top" width="24.47%" headers="mcps1.3.4.3.1.2.2.2.3.1.1 "><p id="rds_faq_0050__p69401305450"><em id="rds_faq_0050__i5228536185018"><instance_ip></em></p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="75.53%" headers="mcps1.3.4.3.1.2.2.2.3.1.2 "><p id="rds_faq_0050__p6940133015452">If you attempt to access the RDS DB instance through an <span id="rds_faq_0050__text1538316415553">ECS</span>, set <em id="rds_faq_0050__i390973717478">instance_ip</em> to the floating IP address displayed on the <strong id="rds_faq_0050__b39101637184715">Basic Information</strong> page of the DB instance to which you intend to connect.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="rds_faq_0050__row17940173019459"><td class="cellrowborder" valign="top" headers="mcps1.3.4.3.1.2.2.2.3.1.1 "><p id="rds_faq_0050__p2044012409545">If you attempt to access the RDS DB instance through an EIP, set <em id="rds_faq_0050__i5348103216522">instance_ip</em> to the EIP that has been bound to the DB instance.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="rds_faq_0050__row89406308455"><td class="cellrowborder" valign="top" width="24.47%" headers="mcps1.3.4.3.1.2.2.2.3.1.1 "><p id="rds_faq_0050__p109407302452"><em id="rds_faq_0050__i13432124114503"><instance_port></em></p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="75.53%" headers="mcps1.3.4.3.1.2.2.2.3.1.2 "><p id="rds_faq_0050__p294013013459">Enter the database port displayed on the <strong id="rds_faq_0050__b1569311563248">Basic Information</strong> page. Default value: <strong id="rds_faq_0050__b16694165618240">5432</strong></p>
|
|
</td>
|
|
</tr>
|
|
<tr id="rds_faq_0050__row15940203014452"><td class="cellrowborder" valign="top" width="24.47%" headers="mcps1.3.4.3.1.2.2.2.3.1.1 "><p id="rds_faq_0050__p594043016454"><em id="rds_faq_0050__i1798184818501"><database_name></em></p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="75.53%" headers="mcps1.3.4.3.1.2.2.2.3.1.2 "><p id="rds_faq_0050__p9940530164511">Enter the name of the database to which you intend to connect. Default value: <strong id="rds_faq_0050__b84235270610929">postgres</strong></p>
|
|
</td>
|
|
</tr>
|
|
<tr id="rds_faq_0050__row99401430134513"><td class="cellrowborder" valign="top" width="24.47%" headers="mcps1.3.4.3.1.2.2.2.3.1.1 "><p id="rds_faq_0050__p994073004512">sslmode</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="75.53%" headers="mcps1.3.4.3.1.2.2.2.3.1.2 "><p id="rds_faq_0050__p794083019454">Enter the SSL connection mode. Default value: <strong id="rds_faq_0050__b57791859204310">verify-full</strong></p>
|
|
</td>
|
|
</tr>
|
|
<tr id="rds_faq_0050__row2940143020452"><td class="cellrowborder" valign="top" width="24.47%" headers="mcps1.3.4.3.1.2.2.2.3.1.1 "><p id="rds_faq_0050__p1794093019452">sslrootcert</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="75.53%" headers="mcps1.3.4.3.1.2.2.2.3.1.2 "><p id="rds_faq_0050__p19940330114516">Enter the directory of the CA certificate for the SSL connection. The certificate should be stored in the directory where the command is executed.</p>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<p id="rds_faq_0050__a89d34686f52a4e1b830acbc0a5b3baea">Example script in Java:</p>
|
|
<pre class="codeblock" id="rds_faq_0050__codeblock12608143620112">import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.ResultSet;
|
|
import java.sql.Statement;
|
|
|
|
public class MyConnTest {
|
|
final public static void main(String[] args) {
|
|
Connection conn = null;
|
|
<strong id="rds_faq_0050__b109331044415">// set sslmode here.</strong>
|
|
<strong id="rds_faq_0050__b39346442115"> // with ssl certificate and path.</strong>
|
|
<strong id="rds_faq_0050__b159351844815"> String url = "jdbc:postgresql://192.168.0.225:5432/my_db_test?sslmode=verify-full&sslrootcert=/home/Ruby/ca.pem";</strong>
|
|
|
|
try {
|
|
Class.forName("org.postgresql.Driver");
|
|
conn = DriverManager.getConnection(url, "root", "password");
|
|
System.out.println("Database connected");
|
|
|
|
Statement stmt = conn.createStatement();
|
|
ResultSet rs = stmt.executeQuery("SELECT * FROM mytable WHERE columnfoo = 500");
|
|
while (rs.next()) {
|
|
System.out.println(rs.getString(1));
|
|
}
|
|
|
|
rs.close();
|
|
stmt.close();
|
|
conn.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
System.out.println("Test failed");
|
|
} finally {
|
|
// release resource ....
|
|
}
|
|
}
|
|
}</pre>
|
|
</p></li></ol>
|
|
</div>
|
|
<div class="section" id="rds_faq_0050__s12a6f787675c4a6789f8c302301c2a74"><h4 class="sectiontitle">Connection Without the SSL Certificate</h4><div class="note" id="rds_faq_0050__n0922d0006b0741fd8e4a22514265d343"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="rds_faq_0050__a911e5f488fa74787918c56e647648e22">You do not need to download the SSL certificate because the certificate verification on the server is not required.</p>
|
|
</div></div>
|
|
<ol id="rds_faq_0050__o271ed8ddb2b8431e9d4a6cef63cabb5f"><li id="rds_faq_0050__l60162e570f814650a8fb0f0f968ad0d6"><span>Connect to the RDS PostgreSQL DB instance through JDBC.</span><p><pre class="screen" id="rds_faq_0050__s2ce1006771b84ddc92aa0b02952e1691"><strong id="rds_faq_0050__en-us_topic_0086557099_b91289518610">jdbc:postgresql://</strong><em id="rds_faq_0050__ad991d2ca18ce468497f36f105125a2a5"><instance_ip></em><strong id="rds_faq_0050__a95984bdcbc8246ac98200a9ac4b7a4c9">:</strong><em id="rds_faq_0050__en-us_topic_0086557099_i578757561876"><instance_port></em><strong id="rds_faq_0050__en-us_topic_0086557099_b424280831878">/</strong><em id="rds_faq_0050__en-us_topic_0086557099_i511197581876"><database_name></em><strong id="rds_faq_0050__a544df100a617466eb287e5594b180d6e">?sslmode=disable</strong></pre>
|
|
|
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="rds_faq_0050__table13983205311910" frame="border" border="1" rules="all"><caption><b>Table 2 </b>Parameter description</caption><thead align="left"><tr id="rds_faq_0050__row1398418533917"><th align="left" class="cellrowborder" valign="top" width="24.47%" id="mcps1.3.5.3.1.2.2.2.3.1.1"><p id="rds_faq_0050__p139842531494"><strong id="rds_faq_0050__b134217402017">Parameter</strong></p>
|
|
</th>
|
|
<th align="left" class="cellrowborder" valign="top" width="75.53%" id="mcps1.3.5.3.1.2.2.2.3.1.2"><p id="rds_faq_0050__p139848531597"><strong id="rds_faq_0050__b1491119409015">Description</strong></p>
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody><tr id="rds_faq_0050__row49841538915"><td class="cellrowborder" rowspan="2" valign="top" width="24.47%" headers="mcps1.3.5.3.1.2.2.2.3.1.1 "><p id="rds_faq_0050__p17984653296"><em id="rds_faq_0050__i59845531192"><instance_ip></em></p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="75.53%" headers="mcps1.3.5.3.1.2.2.2.3.1.2 "><p id="rds_faq_0050__p13984115312911">If you attempt to access the RDS DB instance through an <span id="rds_faq_0050__text10841153625718">ECS</span>, set <em id="rds_faq_0050__i93681278494">instance_ip</em> to the floating IP address displayed on the <strong id="rds_faq_0050__b1336816277494">Basic Information</strong> page of the DB instance to which you intend to connect.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="rds_faq_0050__row14985155310915"><td class="cellrowborder" valign="top" headers="mcps1.3.5.3.1.2.2.2.3.1.1 "><p id="rds_faq_0050__p698518532913">If you attempt to access the RDS DB instance through an EIP, set <em id="rds_faq_0050__i178632416543">instance_ip</em> to the EIP that has been bound to the DB instance.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="rds_faq_0050__row12985145310920"><td class="cellrowborder" valign="top" width="24.47%" headers="mcps1.3.5.3.1.2.2.2.3.1.1 "><p id="rds_faq_0050__p1398510531395"><em id="rds_faq_0050__i12985953097"><instance_port></em></p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="75.53%" headers="mcps1.3.5.3.1.2.2.2.3.1.2 "><p id="rds_faq_0050__p149850537915">Enter the database port displayed on the <strong id="rds_faq_0050__b152771859204">Basic Information</strong> page. Default value: <strong id="rds_faq_0050__b2027810599011">5432</strong></p>
|
|
</td>
|
|
</tr>
|
|
<tr id="rds_faq_0050__row1698513531894"><td class="cellrowborder" valign="top" width="24.47%" headers="mcps1.3.5.3.1.2.2.2.3.1.1 "><p id="rds_faq_0050__p179856535912"><em id="rds_faq_0050__i1198518531391"><database_name></em></p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="75.53%" headers="mcps1.3.5.3.1.2.2.2.3.1.2 "><p id="rds_faq_0050__p119851531594">Enter the name of the database to which you intend to connect. Default value: <strong id="rds_faq_0050__b426641811">postgres</strong></p>
|
|
</td>
|
|
</tr>
|
|
<tr id="rds_faq_0050__row1498555318919"><td class="cellrowborder" valign="top" width="24.47%" headers="mcps1.3.5.3.1.2.2.2.3.1.1 "><p id="rds_faq_0050__p098585312915">sslmode</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="75.53%" headers="mcps1.3.5.3.1.2.2.2.3.1.2 "><p id="rds_faq_0050__p2985125311915">Enter the SSL connection mode. <strong id="rds_faq_0050__b8732171712277">disable</strong> means data is not encrypted.</p>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<p id="rds_faq_0050__acfcd415f242f4ba599cfb1c0e20ef77f">Example script in Java:</p>
|
|
<pre class="codeblock" id="rds_faq_0050__codeblock12339191134610">import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.ResultSet;
|
|
import java.sql.Statement;
|
|
|
|
public class MyConnTest {
|
|
final public static void main(String[] args) {
|
|
Connection conn = null;
|
|
<strong id="rds_faq_0050__b788245364618">// set sslmode here.</strong>
|
|
<strong id="rds_faq_0050__b118851153154616"> // no ssl certificate, so do not specify path.</strong>
|
|
<strong id="rds_faq_0050__b14886115312468"> String url = "jdbc:postgresql://192.168.0.225:5432/my_db_test?sslmode=disable";</strong>
|
|
try {
|
|
Class.forName("org.postgresql.Driver");
|
|
conn = DriverManager.getConnection(url, "root", "password");
|
|
System.out.println("Database connected");
|
|
|
|
Statement stmt = conn.createStatement();
|
|
ResultSet rs = stmt.executeQuery("SELECT * FROM mytable WHERE columnfoo = 500");
|
|
while (rs.next()) {
|
|
System.out.println(rs.getString(1));
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
conn.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
System.out.println("Test failed");
|
|
} finally {
|
|
// release resource ....
|
|
}
|
|
}
|
|
}</pre>
|
|
</p></li></ol>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rds_faq_0141.html">Database Connection</a></div>
|
|
</div>
|
|
</div>
|
|
|