2024年3月10日发(作者:)

PostgreSQL JDBC Instruction

1. Download a JDBC driver

URL: /download/

File:

Version: 8.3-604

Notice: I strongly recommend you to download this version. Otherwise, the JDBC3 driver may require

using a different URL format. (thanks Arthur!)

2. Using the Driver

1) Importing JDBC:

import .*;

2) Loading the Driver:

e("");

3) Connecting to the Database:

Connection db = nection(url, username, password);

url = jdbc:postgresql://155.246.89.29: 5432/database

(database name is the same as your username by default)

username = the posgtresql database account username

password = the posgtresql database account password

Notice: Please use your postgresql database password here (a 10-digit random string). If you have

changed your password on the postgresql server, you’d better change your database password also.

4) Closing the Connection

();

For more information, please see /docs/7.4/static/

3. Here is an example:

import .*;

public class testdb

{

public static void main(String[] args) {

String usr ="yding";

String pwd ="######";

String url ="jdbc:postgresql://155.246.89.29:5432/yding";

try {

e("");

n("Success loading Driver!");

} catch(Exception e) {

n("Fail loading Driver!");

tackTrace();

}

try {

Connection db = nection(url, usr,

pwd);

n("Success connecting server!");

} catch(SQLException e) {

n("Connection URL or username or

password errors!");

tackTrace();

}

}

}

4. export your classpath

Put your JDBC driver and your program into the same directory and compile it. Before you run your

program, type the command down blew:

export CLASSPATH=/home/yding/java/:.

Notice: you should change your own directory where you put your JDBC driver.